#include <bits/stdc++.h>
#define eb emplace_back
#define mp make_pair
#define F first
#define S second
#define all(x) x.begin(), x.end()
#define svec(x) sort(x.begin(), x.end())
#define press(x) x.erase(unique(x.begin(), x.end()), x.end())
#define lb(x, v) lower_bound(x.begin(), x.end(), v)
#define ub(x, v) upper_bound(x.begin(), x.end(), v)
using namespace std;
typedef long long LL;
typedef pair<int, int> pii;
typedef pair<LL, LL> pll;
typedef pair<int, LL> pil;
typedef pair<LL, int> pli;
const LL llinf=10000000000000;
const LL mod1=1000000007;
const LL mod2=998244353;
const int inf=2000000000;
int n, k;
LL arr[300010], dp[300010][2], cnt[300010][2];
int solve(LL x){
dp[0][0]=-llinf;
dp[0][1]=0;
for(int i=1; i<=n; i++){
if(dp[i-1][0]>dp[i-1][1]-x)dp[i][0]=dp[i-1][0]+arr[i], cnt[i][0]=cnt[i-1][0];
else dp[i][0]=dp[i-1][1]+arr[i]-x, cnt[i][0]=cnt[i-1][1]+1;
if(pll(dp[i-1][0], -cnt[i-1][0])>pll(dp[i-1][1], -cnt[i-1][1]))dp[i][1]=dp[i-1][0], cnt[i][1]=cnt[i-1][0];
else dp[i][1]=dp[i-1][1], cnt[i][1]=cnt[i-1][1];
}
if(pll(dp[n][0], -cnt[n][0])>pll(dp[n][1], -cnt[n][1]))return cnt[n][0];
return cnt[n][1];
}
int main(){
scanf("%d %d", &n, &k);
for(int i=1; i<=n; i++){
scanf("%lld", &arr[i]);
arr[i]*=2;
}
LL l=0, r=llinf;
while(l<r){
LL mid=(l+r)/2;
if(solve(mid*2+1)>=k)l=mid+1;
else r=mid;
}
solve(l*2);
printf("%lld", max(dp[n][0], dp[n][1])/2+l*k);
}
Compilation message
feast.cpp: In function 'int main()':
feast.cpp:39:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d", &n, &k);
~~~~~^~~~~~~~~~~~~~~~~
feast.cpp:41:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%lld", &arr[i]);
~~~~~^~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
110 ms |
11640 KB |
Output is correct |
2 |
Correct |
119 ms |
11916 KB |
Output is correct |
3 |
Correct |
117 ms |
12024 KB |
Output is correct |
4 |
Correct |
116 ms |
12024 KB |
Output is correct |
5 |
Correct |
111 ms |
11896 KB |
Output is correct |
6 |
Correct |
107 ms |
11640 KB |
Output is correct |
7 |
Correct |
117 ms |
11768 KB |
Output is correct |
8 |
Correct |
115 ms |
11988 KB |
Output is correct |
9 |
Correct |
107 ms |
11768 KB |
Output is correct |
10 |
Correct |
117 ms |
11896 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
94 ms |
11768 KB |
Output is correct |
2 |
Correct |
95 ms |
13304 KB |
Output is correct |
3 |
Correct |
95 ms |
12920 KB |
Output is correct |
4 |
Correct |
91 ms |
12920 KB |
Output is correct |
5 |
Correct |
112 ms |
14456 KB |
Output is correct |
6 |
Correct |
94 ms |
12920 KB |
Output is correct |
7 |
Correct |
98 ms |
13176 KB |
Output is correct |
8 |
Correct |
113 ms |
14840 KB |
Output is correct |
9 |
Correct |
108 ms |
14456 KB |
Output is correct |
10 |
Correct |
91 ms |
13176 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
135 ms |
11896 KB |
Output is correct |
2 |
Correct |
134 ms |
14844 KB |
Output is correct |
3 |
Correct |
132 ms |
14840 KB |
Output is correct |
4 |
Correct |
133 ms |
14968 KB |
Output is correct |
5 |
Correct |
133 ms |
14968 KB |
Output is correct |
6 |
Correct |
134 ms |
14968 KB |
Output is correct |
7 |
Correct |
133 ms |
15096 KB |
Output is correct |
8 |
Correct |
130 ms |
14968 KB |
Output is correct |
9 |
Correct |
148 ms |
15096 KB |
Output is correct |
10 |
Correct |
132 ms |
15096 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
5 ms |
384 KB |
Output is correct |
3 |
Correct |
5 ms |
384 KB |
Output is correct |
4 |
Correct |
5 ms |
384 KB |
Output is correct |
5 |
Correct |
5 ms |
384 KB |
Output is correct |
6 |
Correct |
5 ms |
512 KB |
Output is correct |
7 |
Correct |
5 ms |
384 KB |
Output is correct |
8 |
Correct |
5 ms |
384 KB |
Output is correct |
9 |
Correct |
5 ms |
384 KB |
Output is correct |
10 |
Correct |
5 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
5 ms |
384 KB |
Output is correct |
3 |
Correct |
5 ms |
384 KB |
Output is correct |
4 |
Correct |
5 ms |
384 KB |
Output is correct |
5 |
Correct |
5 ms |
384 KB |
Output is correct |
6 |
Correct |
5 ms |
512 KB |
Output is correct |
7 |
Correct |
5 ms |
384 KB |
Output is correct |
8 |
Correct |
5 ms |
384 KB |
Output is correct |
9 |
Correct |
5 ms |
384 KB |
Output is correct |
10 |
Correct |
5 ms |
384 KB |
Output is correct |
11 |
Correct |
5 ms |
384 KB |
Output is correct |
12 |
Correct |
5 ms |
384 KB |
Output is correct |
13 |
Correct |
6 ms |
384 KB |
Output is correct |
14 |
Correct |
5 ms |
384 KB |
Output is correct |
15 |
Correct |
5 ms |
384 KB |
Output is correct |
16 |
Correct |
5 ms |
384 KB |
Output is correct |
17 |
Correct |
5 ms |
384 KB |
Output is correct |
18 |
Correct |
5 ms |
384 KB |
Output is correct |
19 |
Correct |
5 ms |
384 KB |
Output is correct |
20 |
Correct |
5 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
5 ms |
384 KB |
Output is correct |
3 |
Correct |
5 ms |
384 KB |
Output is correct |
4 |
Correct |
5 ms |
384 KB |
Output is correct |
5 |
Correct |
5 ms |
384 KB |
Output is correct |
6 |
Correct |
5 ms |
512 KB |
Output is correct |
7 |
Correct |
5 ms |
384 KB |
Output is correct |
8 |
Correct |
5 ms |
384 KB |
Output is correct |
9 |
Correct |
5 ms |
384 KB |
Output is correct |
10 |
Correct |
5 ms |
384 KB |
Output is correct |
11 |
Correct |
5 ms |
384 KB |
Output is correct |
12 |
Correct |
5 ms |
384 KB |
Output is correct |
13 |
Correct |
6 ms |
384 KB |
Output is correct |
14 |
Correct |
5 ms |
384 KB |
Output is correct |
15 |
Correct |
5 ms |
384 KB |
Output is correct |
16 |
Correct |
5 ms |
384 KB |
Output is correct |
17 |
Correct |
5 ms |
384 KB |
Output is correct |
18 |
Correct |
5 ms |
384 KB |
Output is correct |
19 |
Correct |
5 ms |
384 KB |
Output is correct |
20 |
Correct |
5 ms |
384 KB |
Output is correct |
21 |
Correct |
6 ms |
384 KB |
Output is correct |
22 |
Correct |
5 ms |
384 KB |
Output is correct |
23 |
Correct |
6 ms |
512 KB |
Output is correct |
24 |
Correct |
6 ms |
384 KB |
Output is correct |
25 |
Correct |
6 ms |
512 KB |
Output is correct |
26 |
Correct |
5 ms |
384 KB |
Output is correct |
27 |
Correct |
6 ms |
384 KB |
Output is correct |
28 |
Correct |
5 ms |
384 KB |
Output is correct |
29 |
Correct |
6 ms |
384 KB |
Output is correct |
30 |
Correct |
5 ms |
512 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
110 ms |
11640 KB |
Output is correct |
2 |
Correct |
119 ms |
11916 KB |
Output is correct |
3 |
Correct |
117 ms |
12024 KB |
Output is correct |
4 |
Correct |
116 ms |
12024 KB |
Output is correct |
5 |
Correct |
111 ms |
11896 KB |
Output is correct |
6 |
Correct |
107 ms |
11640 KB |
Output is correct |
7 |
Correct |
117 ms |
11768 KB |
Output is correct |
8 |
Correct |
115 ms |
11988 KB |
Output is correct |
9 |
Correct |
107 ms |
11768 KB |
Output is correct |
10 |
Correct |
117 ms |
11896 KB |
Output is correct |
11 |
Correct |
94 ms |
11768 KB |
Output is correct |
12 |
Correct |
95 ms |
13304 KB |
Output is correct |
13 |
Correct |
95 ms |
12920 KB |
Output is correct |
14 |
Correct |
91 ms |
12920 KB |
Output is correct |
15 |
Correct |
112 ms |
14456 KB |
Output is correct |
16 |
Correct |
94 ms |
12920 KB |
Output is correct |
17 |
Correct |
98 ms |
13176 KB |
Output is correct |
18 |
Correct |
113 ms |
14840 KB |
Output is correct |
19 |
Correct |
108 ms |
14456 KB |
Output is correct |
20 |
Correct |
91 ms |
13176 KB |
Output is correct |
21 |
Correct |
135 ms |
11896 KB |
Output is correct |
22 |
Correct |
134 ms |
14844 KB |
Output is correct |
23 |
Correct |
132 ms |
14840 KB |
Output is correct |
24 |
Correct |
133 ms |
14968 KB |
Output is correct |
25 |
Correct |
133 ms |
14968 KB |
Output is correct |
26 |
Correct |
134 ms |
14968 KB |
Output is correct |
27 |
Correct |
133 ms |
15096 KB |
Output is correct |
28 |
Correct |
130 ms |
14968 KB |
Output is correct |
29 |
Correct |
148 ms |
15096 KB |
Output is correct |
30 |
Correct |
132 ms |
15096 KB |
Output is correct |
31 |
Correct |
5 ms |
384 KB |
Output is correct |
32 |
Correct |
5 ms |
384 KB |
Output is correct |
33 |
Correct |
5 ms |
384 KB |
Output is correct |
34 |
Correct |
5 ms |
384 KB |
Output is correct |
35 |
Correct |
5 ms |
384 KB |
Output is correct |
36 |
Correct |
5 ms |
512 KB |
Output is correct |
37 |
Correct |
5 ms |
384 KB |
Output is correct |
38 |
Correct |
5 ms |
384 KB |
Output is correct |
39 |
Correct |
5 ms |
384 KB |
Output is correct |
40 |
Correct |
5 ms |
384 KB |
Output is correct |
41 |
Correct |
5 ms |
384 KB |
Output is correct |
42 |
Correct |
5 ms |
384 KB |
Output is correct |
43 |
Correct |
6 ms |
384 KB |
Output is correct |
44 |
Correct |
5 ms |
384 KB |
Output is correct |
45 |
Correct |
5 ms |
384 KB |
Output is correct |
46 |
Correct |
5 ms |
384 KB |
Output is correct |
47 |
Correct |
5 ms |
384 KB |
Output is correct |
48 |
Correct |
5 ms |
384 KB |
Output is correct |
49 |
Correct |
5 ms |
384 KB |
Output is correct |
50 |
Correct |
5 ms |
384 KB |
Output is correct |
51 |
Correct |
6 ms |
384 KB |
Output is correct |
52 |
Correct |
5 ms |
384 KB |
Output is correct |
53 |
Correct |
6 ms |
512 KB |
Output is correct |
54 |
Correct |
6 ms |
384 KB |
Output is correct |
55 |
Correct |
6 ms |
512 KB |
Output is correct |
56 |
Correct |
5 ms |
384 KB |
Output is correct |
57 |
Correct |
6 ms |
384 KB |
Output is correct |
58 |
Correct |
5 ms |
384 KB |
Output is correct |
59 |
Correct |
6 ms |
384 KB |
Output is correct |
60 |
Correct |
5 ms |
512 KB |
Output is correct |
61 |
Correct |
187 ms |
14712 KB |
Output is correct |
62 |
Correct |
183 ms |
15096 KB |
Output is correct |
63 |
Correct |
141 ms |
14712 KB |
Output is correct |
64 |
Correct |
180 ms |
15096 KB |
Output is correct |
65 |
Correct |
175 ms |
15096 KB |
Output is correct |
66 |
Correct |
176 ms |
14968 KB |
Output is correct |
67 |
Correct |
178 ms |
14840 KB |
Output is correct |
68 |
Correct |
132 ms |
14972 KB |
Output is correct |
69 |
Correct |
134 ms |
14712 KB |
Output is correct |
70 |
Correct |
130 ms |
14628 KB |
Output is correct |