#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll INF=1e17;
ll n,k,v[600005];
struct date
{
ll val,l,r;
};
date dp[600005][2];
date combine(date a, date b)
{
if(a.val>b.val)
return a;
if(a.val<b.val)
return b;
a.l=min(a.l,b.l);
a.r=max(a.r,b.r);
return a;
}
date calc(ll cost)
{
dp[0][0]={0,0,0};
dp[0][1]={-INF,0,0};
for(int i=1;i<=n;i++)
{
dp[i][0]=combine(dp[i-1][0],dp[i-1][1]);
date a=dp[i-1][0];
a.l++;
a.r++;
a.val+=v[i]-cost;
dp[i][1]=a;
a=dp[i-1][1];
a.val+=v[i];
dp[i][1]=combine(dp[i][1],a);
a=dp[i-1][1];
a.l++;
a.r++;
a.val+=v[i]-cost;
dp[i][1]=combine(dp[i][1],a);
}
return combine(dp[n][0],dp[n][1]);
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
cin>>n>>k;
for(ll i=1;i<=n;i++)
cin>>v[i];
n+=k;
ll st=-1e12;
ll dr=1e12;
while(st<=dr)
{
ll mij=(st+dr)/2;
date rez=calc(mij);
if(rez.l<=k&&rez.r>=k)
{
cout<<rez.val+k*mij;
return 0;
}
if(rez.l>k)
st=mij+1;
else
dr=mij-1;
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
24 ms |
21084 KB |
Output is correct |
2 |
Correct |
26 ms |
27224 KB |
Output is correct |
3 |
Correct |
26 ms |
21084 KB |
Output is correct |
4 |
Correct |
25 ms |
21084 KB |
Output is correct |
5 |
Correct |
25 ms |
21084 KB |
Output is correct |
6 |
Correct |
24 ms |
20952 KB |
Output is correct |
7 |
Correct |
24 ms |
21080 KB |
Output is correct |
8 |
Correct |
24 ms |
18896 KB |
Output is correct |
9 |
Correct |
25 ms |
19028 KB |
Output is correct |
10 |
Correct |
24 ms |
19036 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
43 ms |
19036 KB |
Output is correct |
2 |
Correct |
33 ms |
19092 KB |
Output is correct |
3 |
Correct |
33 ms |
19032 KB |
Output is correct |
4 |
Correct |
19 ms |
25180 KB |
Output is correct |
5 |
Correct |
25 ms |
18896 KB |
Output is correct |
6 |
Correct |
31 ms |
19036 KB |
Output is correct |
7 |
Correct |
19 ms |
29144 KB |
Output is correct |
8 |
Correct |
26 ms |
18888 KB |
Output is correct |
9 |
Correct |
25 ms |
19028 KB |
Output is correct |
10 |
Correct |
18 ms |
23128 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
45 ms |
18880 KB |
Output is correct |
2 |
Correct |
39 ms |
19032 KB |
Output is correct |
3 |
Correct |
37 ms |
18896 KB |
Output is correct |
4 |
Correct |
37 ms |
19024 KB |
Output is correct |
5 |
Correct |
37 ms |
19028 KB |
Output is correct |
6 |
Correct |
43 ms |
19076 KB |
Output is correct |
7 |
Correct |
45 ms |
18892 KB |
Output is correct |
8 |
Correct |
42 ms |
19032 KB |
Output is correct |
9 |
Correct |
37 ms |
19084 KB |
Output is correct |
10 |
Correct |
44 ms |
19084 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
0 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
2392 KB |
Output is correct |
9 |
Correct |
1 ms |
2392 KB |
Output is correct |
10 |
Correct |
1 ms |
2392 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
0 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
2392 KB |
Output is correct |
9 |
Correct |
1 ms |
2392 KB |
Output is correct |
10 |
Correct |
1 ms |
2392 KB |
Output is correct |
11 |
Correct |
1 ms |
2392 KB |
Output is correct |
12 |
Correct |
1 ms |
2396 KB |
Output is correct |
13 |
Correct |
1 ms |
2396 KB |
Output is correct |
14 |
Correct |
1 ms |
2396 KB |
Output is correct |
15 |
Correct |
1 ms |
2396 KB |
Output is correct |
16 |
Correct |
1 ms |
2396 KB |
Output is correct |
17 |
Correct |
1 ms |
2408 KB |
Output is correct |
18 |
Correct |
1 ms |
2396 KB |
Output is correct |
19 |
Correct |
1 ms |
2396 KB |
Output is correct |
20 |
Correct |
1 ms |
2396 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
0 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
2392 KB |
Output is correct |
9 |
Correct |
1 ms |
2392 KB |
Output is correct |
10 |
Correct |
1 ms |
2392 KB |
Output is correct |
11 |
Correct |
1 ms |
2392 KB |
Output is correct |
12 |
Correct |
1 ms |
2396 KB |
Output is correct |
13 |
Correct |
1 ms |
2396 KB |
Output is correct |
14 |
Correct |
1 ms |
2396 KB |
Output is correct |
15 |
Correct |
1 ms |
2396 KB |
Output is correct |
16 |
Correct |
1 ms |
2396 KB |
Output is correct |
17 |
Correct |
1 ms |
2408 KB |
Output is correct |
18 |
Correct |
1 ms |
2396 KB |
Output is correct |
19 |
Correct |
1 ms |
2396 KB |
Output is correct |
20 |
Correct |
1 ms |
2396 KB |
Output is correct |
21 |
Correct |
1 ms |
2396 KB |
Output is correct |
22 |
Correct |
1 ms |
2652 KB |
Output is correct |
23 |
Correct |
2 ms |
2392 KB |
Output is correct |
24 |
Correct |
1 ms |
2392 KB |
Output is correct |
25 |
Correct |
1 ms |
2396 KB |
Output is correct |
26 |
Correct |
1 ms |
2396 KB |
Output is correct |
27 |
Correct |
1 ms |
2396 KB |
Output is correct |
28 |
Correct |
1 ms |
2392 KB |
Output is correct |
29 |
Correct |
1 ms |
2396 KB |
Output is correct |
30 |
Correct |
1 ms |
2396 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
24 ms |
21084 KB |
Output is correct |
2 |
Correct |
26 ms |
27224 KB |
Output is correct |
3 |
Correct |
26 ms |
21084 KB |
Output is correct |
4 |
Correct |
25 ms |
21084 KB |
Output is correct |
5 |
Correct |
25 ms |
21084 KB |
Output is correct |
6 |
Correct |
24 ms |
20952 KB |
Output is correct |
7 |
Correct |
24 ms |
21080 KB |
Output is correct |
8 |
Correct |
24 ms |
18896 KB |
Output is correct |
9 |
Correct |
25 ms |
19028 KB |
Output is correct |
10 |
Correct |
24 ms |
19036 KB |
Output is correct |
11 |
Correct |
43 ms |
19036 KB |
Output is correct |
12 |
Correct |
33 ms |
19092 KB |
Output is correct |
13 |
Correct |
33 ms |
19032 KB |
Output is correct |
14 |
Correct |
19 ms |
25180 KB |
Output is correct |
15 |
Correct |
25 ms |
18896 KB |
Output is correct |
16 |
Correct |
31 ms |
19036 KB |
Output is correct |
17 |
Correct |
19 ms |
29144 KB |
Output is correct |
18 |
Correct |
26 ms |
18888 KB |
Output is correct |
19 |
Correct |
25 ms |
19028 KB |
Output is correct |
20 |
Correct |
18 ms |
23128 KB |
Output is correct |
21 |
Correct |
45 ms |
18880 KB |
Output is correct |
22 |
Correct |
39 ms |
19032 KB |
Output is correct |
23 |
Correct |
37 ms |
18896 KB |
Output is correct |
24 |
Correct |
37 ms |
19024 KB |
Output is correct |
25 |
Correct |
37 ms |
19028 KB |
Output is correct |
26 |
Correct |
43 ms |
19076 KB |
Output is correct |
27 |
Correct |
45 ms |
18892 KB |
Output is correct |
28 |
Correct |
42 ms |
19032 KB |
Output is correct |
29 |
Correct |
37 ms |
19084 KB |
Output is correct |
30 |
Correct |
44 ms |
19084 KB |
Output is correct |
31 |
Correct |
1 ms |
2396 KB |
Output is correct |
32 |
Correct |
0 ms |
2396 KB |
Output is correct |
33 |
Correct |
1 ms |
2396 KB |
Output is correct |
34 |
Correct |
1 ms |
2396 KB |
Output is correct |
35 |
Correct |
1 ms |
2396 KB |
Output is correct |
36 |
Correct |
1 ms |
2396 KB |
Output is correct |
37 |
Correct |
1 ms |
2396 KB |
Output is correct |
38 |
Correct |
1 ms |
2392 KB |
Output is correct |
39 |
Correct |
1 ms |
2392 KB |
Output is correct |
40 |
Correct |
1 ms |
2392 KB |
Output is correct |
41 |
Correct |
1 ms |
2392 KB |
Output is correct |
42 |
Correct |
1 ms |
2396 KB |
Output is correct |
43 |
Correct |
1 ms |
2396 KB |
Output is correct |
44 |
Correct |
1 ms |
2396 KB |
Output is correct |
45 |
Correct |
1 ms |
2396 KB |
Output is correct |
46 |
Correct |
1 ms |
2396 KB |
Output is correct |
47 |
Correct |
1 ms |
2408 KB |
Output is correct |
48 |
Correct |
1 ms |
2396 KB |
Output is correct |
49 |
Correct |
1 ms |
2396 KB |
Output is correct |
50 |
Correct |
1 ms |
2396 KB |
Output is correct |
51 |
Correct |
1 ms |
2396 KB |
Output is correct |
52 |
Correct |
1 ms |
2652 KB |
Output is correct |
53 |
Correct |
2 ms |
2392 KB |
Output is correct |
54 |
Correct |
1 ms |
2392 KB |
Output is correct |
55 |
Correct |
1 ms |
2396 KB |
Output is correct |
56 |
Correct |
1 ms |
2396 KB |
Output is correct |
57 |
Correct |
1 ms |
2396 KB |
Output is correct |
58 |
Correct |
1 ms |
2392 KB |
Output is correct |
59 |
Correct |
1 ms |
2396 KB |
Output is correct |
60 |
Correct |
1 ms |
2396 KB |
Output is correct |
61 |
Correct |
96 ms |
23900 KB |
Output is correct |
62 |
Correct |
30 ms |
28240 KB |
Output is correct |
63 |
Correct |
74 ms |
22056 KB |
Output is correct |
64 |
Correct |
111 ms |
24172 KB |
Output is correct |
65 |
Correct |
87 ms |
24172 KB |
Output is correct |
66 |
Correct |
94 ms |
24148 KB |
Output is correct |
67 |
Correct |
83 ms |
23892 KB |
Output is correct |
68 |
Correct |
51 ms |
21936 KB |
Output is correct |
69 |
Correct |
51 ms |
21956 KB |
Output is correct |
70 |
Correct |
39 ms |
21840 KB |
Output is correct |