Pagini recente » Cod sursa (job #189374) | Cod sursa (job #458761) | Cod sursa (job #12953) | Cod sursa (job #2480771) | Cod sursa (job #2223716)
#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; }