Cod sursa(job #2223716)

Utilizator oldatlantianSerban Cercelescu oldatlantian Data 21 iulie 2018 11:28:30
Problema Euro Scor 50
Compilator cpp Status done
Runda Arhiva de probleme Marime 0.94 kb
#include <bits/stdc++.h>
using namespace std;

ifstream fi("euro.in");
ofstream fo("euro.out");

using i64 = long long;
using f64 = double;

const int N = 40005;


struct Line {
	i64 a, b;

	inline f64 operator [] (const f64 &x) const {
		return a * x + b; }

	inline bool operator < (const Line &arg) {
		return a < arg.a; } };

i64 s[N], dp[N];


deque<Line> batch;
int n, comision;


int main() {
	Line now;

	fi >> n >> comision;
	for (int i = 1; i <= n; ++i) {
		fi >> s[i];
		s[i]+= s[i - 1]; }

	batch.push_back({0, 0});
	for (int i = 1; i <= n; ++i) {
		while (batch.size() >= 2 && batch[0][i] <= batch[1][i])
			batch.pop_front();

		dp[i] = s[i] * i + batch[0][i] - comision;
		now = {-s[i], dp[i]};

		while (!batch.empty() && now[i] >= batch.back()[i] && now.a >= batch.back().a)
			batch.pop_back();

		if (batch.empty() || !(batch.back().a >= now.a && batch.back()[i] >= now[i]))
			batch.push_back(now); }

	fo << dp[n] << endl;

	return 0; }