-
Notifications
You must be signed in to change notification settings - Fork 59
/
Copy patheuro.cpp
42 lines (35 loc) · 909 Bytes
/
euro.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# include <bits/stdc++.h>
# define NR 40005
using namespace std;
ifstream f("euro.in");
ofstream g("euro.out");
int i,j,n,m,VV,IND[NR],T,x,L,R,deq[NR];
long long suma[NR], S, dp[NR], aux;
double G(int a, int b) {
return (double)(dp[a] - dp[b]) / (suma[a] - suma[b]);
}
int main ()
{
f>>n>>T;
for (i=1; i<=n; ++i) {
f>>x; S+=x;
if (S<0) {
IND[++VV]=i;
suma[VV]=suma[VV-1] + S;
S=0;
}
}
if (IND[VV]!=n) {IND[++VV]=n; suma[VV]=suma[VV-1] + S;}
for (i=1; i<=VV; ++i) {
while (L < R && G(deq[L], deq[L+1]) < (double)(IND[i]))
++L;
dp[i]=suma[i] * IND[i] - T;
aux=dp[deq[L]] + (suma[i] - suma[deq[L]]) * IND[i] - T;
dp[i]=max(dp[i], aux);
while (L < R && G(deq[R], i) < G(deq[R-1], deq[R]))
--R;
deq[++R]=i;
}
g<<dp[VV]<<"\n";
return 0;
}