← Real problems

dmopc21c2p1

easymathprefix-sumsconvexityStatement on DMOJ

CP Lang

6 lineschecks
// dmopc21c2p1: pay a per unit to lower the water level to i, then b per unit for every column
// still above it. The cost is a sum of convex functions of i, so ternary search finds the best.
let (n, a, b) = read<(1..=200_000, 0..=1_000_000, 0..=1_000_000)>()
let h = read<[n]0..=1_000_000>()
fn cost(i: Int) -> Int = a * i + b * h.sum(of: x => max(x - i, 0))
print(cost(ternary_min(0..=h.max()!, cost)!))

Accepted C++

67 lines · 1.01 s on the judge
#include "bits/extc++.h"

using namespace std;

template <typename T>
void dbgh(const T& t) {
	cerr << t << endl;
}

template <typename T, typename... U>
void dbgh(const T& t, const U&... u) {
	cerr << t << " | ";
	dbgh(u...);
}

#ifdef DEBUG
#define dbg(...)                                           \
	cerr << "L" << __LINE__ << " [" << #__VA_ARGS__ << "]" \
		 << ": ";                                          \
	dbgh(__VA_ARGS__)
#else
#define cerr   \
	if (false) \
	cerr
#define dbg(...) 1412
#endif

#define endl "\n"
#define long int64_t
#define sz(x) int((x).size())

void solve() {
	int n;
	long a, b;
	cin >> n >> a >> b;
	int arr[n];
	for (int i = 0; i < n; i++) {
		cin >> arr[i];
	}
	sort(arr, arr + n);
	long cur = accumulate(arr, arr + n, long(0)), ans = 4e18, cnt = n;
	int j = 0;
	for (int i = 0; i < int(1e6 + 5); i++) {
		ans = min(ans, i * a + b * cur);
		while(j < n && i == arr[j]) {
			cnt--;
			j++;
		}
		cur -= cnt;
	}
	cout << ans << endl;
}

int main() {
	cin.tie(0)->sync_with_stdio(0);
	cin.exceptions(ios::failbit);
#ifdef LOCAL
	freopen("input.txt", "r", stdin);
#endif
	int t = 1;
	// cin >> t;
	for (int _ = 1; _ <= t; _++) {
		dbg(_);
		// cout << "Case #" << _ << ": ";
		solve();
	}
}