#include <bits/stdc++.h>
using namespace std;
#define rep(i,a,n) for (int i=a;i<n;i++)
#define per(i,a,n) for (int i=n-1;i>=a;i--)
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define SZ(x) ((int)(x).size())
typedef vector<int> VI;
typedef basic_string<int> BI;
typedef long long ll;
typedef pair<int,int> PII;
typedef double db;
mt19937 mrand(random_device{}());
const ll mod=1000000007;
int rnd(int x) { return mrand() % x;}
ll powmod(ll a,ll b) {ll res=1;a%=mod; assert(b>=0); for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;}
ll gcd(ll a,ll b) { return b?gcd(b,a%b):a;}
// head
const int N=303000;
int n,k,a[N];
pair<ll,int> dp[N][2];
pair<ll,int> solve(ll cst) {
dp[0][0]=mp(0,0);
dp[0][1]=mp(-1e17,0);
rep(i,1,n+1) {
dp[i][0]=max(dp[i-1][0],dp[i-1][1]);
dp[i][1]=max(mp(dp[i-1][1].fi+a[i],dp[i-1][1].se),mp(dp[i-1][0].fi+a[i]-cst,dp[i-1][0].se-1));
}
pair<ll,int> ans=max(dp[n][0],dp[n][1]);
return {ans.fi,-ans.se};
}
int main() {
scanf("%d%d",&n,&k);
rep(i,1,n+1) scanf("%d",a+i);
ll l=0,r=1e15,lmb=0;
while (l<=r) {
ll mid=l+r>>1;
if (solve(mid).se<=k) {
lmb=mid;
r=mid-1;
} else {
l=mid+1;
}
}
pair<ll,int> ans=solve(lmb);
printf("%lld\n",ans.fi+ans.se*lmb);
}
Compilation message
feast.cpp: In function 'int main()':
feast.cpp:44:17: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
44 | ll mid=l+r>>1;
| ~^~
feast.cpp:40:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
40 | scanf("%d%d",&n,&k);
| ~~~~~^~~~~~~~~~~~~~
feast.cpp:41:23: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
41 | rep(i,1,n+1) scanf("%d",a+i);
| ~~~~~^~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
72 ms |
13736 KB |
Output is correct |
2 |
Correct |
70 ms |
13908 KB |
Output is correct |
3 |
Correct |
75 ms |
13960 KB |
Output is correct |
4 |
Correct |
74 ms |
13940 KB |
Output is correct |
5 |
Correct |
70 ms |
13852 KB |
Output is correct |
6 |
Correct |
69 ms |
13660 KB |
Output is correct |
7 |
Correct |
72 ms |
13848 KB |
Output is correct |
8 |
Correct |
71 ms |
13908 KB |
Output is correct |
9 |
Correct |
72 ms |
13652 KB |
Output is correct |
10 |
Correct |
73 ms |
13744 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
60 ms |
12124 KB |
Output is correct |
2 |
Correct |
62 ms |
12224 KB |
Output is correct |
3 |
Correct |
61 ms |
12116 KB |
Output is correct |
4 |
Correct |
64 ms |
12116 KB |
Output is correct |
5 |
Correct |
72 ms |
13648 KB |
Output is correct |
6 |
Correct |
62 ms |
12112 KB |
Output is correct |
7 |
Correct |
62 ms |
12228 KB |
Output is correct |
8 |
Correct |
74 ms |
13992 KB |
Output is correct |
9 |
Correct |
70 ms |
13648 KB |
Output is correct |
10 |
Correct |
62 ms |
12204 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
86 ms |
13864 KB |
Output is correct |
2 |
Correct |
86 ms |
13980 KB |
Output is correct |
3 |
Correct |
93 ms |
14080 KB |
Output is correct |
4 |
Correct |
84 ms |
13904 KB |
Output is correct |
5 |
Correct |
84 ms |
14084 KB |
Output is correct |
6 |
Correct |
85 ms |
13904 KB |
Output is correct |
7 |
Correct |
89 ms |
14164 KB |
Output is correct |
8 |
Correct |
84 ms |
13904 KB |
Output is correct |
9 |
Correct |
86 ms |
14124 KB |
Output is correct |
10 |
Correct |
86 ms |
14132 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
1 ms |
2392 KB |
Output is correct |
3 |
Correct |
0 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2504 KB |
Output is correct |
6 |
Correct |
1 ms |
2392 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
0 ms |
2396 KB |
Output is correct |
10 |
Correct |
0 ms |
2396 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
1 ms |
2392 KB |
Output is correct |
3 |
Correct |
0 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2504 KB |
Output is correct |
6 |
Correct |
1 ms |
2392 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
0 ms |
2396 KB |
Output is correct |
10 |
Correct |
0 ms |
2396 KB |
Output is correct |
11 |
Correct |
1 ms |
2648 KB |
Output is correct |
12 |
Correct |
1 ms |
2392 KB |
Output is correct |
13 |
Correct |
1 ms |
2392 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 |
2392 KB |
Output is correct |
17 |
Correct |
1 ms |
2396 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 |
2392 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
1 ms |
2392 KB |
Output is correct |
3 |
Correct |
0 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2504 KB |
Output is correct |
6 |
Correct |
1 ms |
2392 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
0 ms |
2396 KB |
Output is correct |
10 |
Correct |
0 ms |
2396 KB |
Output is correct |
11 |
Correct |
1 ms |
2648 KB |
Output is correct |
12 |
Correct |
1 ms |
2392 KB |
Output is correct |
13 |
Correct |
1 ms |
2392 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 |
2392 KB |
Output is correct |
17 |
Correct |
1 ms |
2396 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 |
2392 KB |
Output is correct |
21 |
Correct |
1 ms |
2396 KB |
Output is correct |
22 |
Correct |
1 ms |
2512 KB |
Output is correct |
23 |
Correct |
1 ms |
2396 KB |
Output is correct |
24 |
Correct |
1 ms |
2556 KB |
Output is correct |
25 |
Correct |
2 ms |
2396 KB |
Output is correct |
26 |
Correct |
1 ms |
2392 KB |
Output is correct |
27 |
Correct |
1 ms |
2396 KB |
Output is correct |
28 |
Correct |
1 ms |
2396 KB |
Output is correct |
29 |
Correct |
1 ms |
2396 KB |
Output is correct |
30 |
Correct |
1 ms |
2512 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
72 ms |
13736 KB |
Output is correct |
2 |
Correct |
70 ms |
13908 KB |
Output is correct |
3 |
Correct |
75 ms |
13960 KB |
Output is correct |
4 |
Correct |
74 ms |
13940 KB |
Output is correct |
5 |
Correct |
70 ms |
13852 KB |
Output is correct |
6 |
Correct |
69 ms |
13660 KB |
Output is correct |
7 |
Correct |
72 ms |
13848 KB |
Output is correct |
8 |
Correct |
71 ms |
13908 KB |
Output is correct |
9 |
Correct |
72 ms |
13652 KB |
Output is correct |
10 |
Correct |
73 ms |
13744 KB |
Output is correct |
11 |
Correct |
60 ms |
12124 KB |
Output is correct |
12 |
Correct |
62 ms |
12224 KB |
Output is correct |
13 |
Correct |
61 ms |
12116 KB |
Output is correct |
14 |
Correct |
64 ms |
12116 KB |
Output is correct |
15 |
Correct |
72 ms |
13648 KB |
Output is correct |
16 |
Correct |
62 ms |
12112 KB |
Output is correct |
17 |
Correct |
62 ms |
12228 KB |
Output is correct |
18 |
Correct |
74 ms |
13992 KB |
Output is correct |
19 |
Correct |
70 ms |
13648 KB |
Output is correct |
20 |
Correct |
62 ms |
12204 KB |
Output is correct |
21 |
Correct |
86 ms |
13864 KB |
Output is correct |
22 |
Correct |
86 ms |
13980 KB |
Output is correct |
23 |
Correct |
93 ms |
14080 KB |
Output is correct |
24 |
Correct |
84 ms |
13904 KB |
Output is correct |
25 |
Correct |
84 ms |
14084 KB |
Output is correct |
26 |
Correct |
85 ms |
13904 KB |
Output is correct |
27 |
Correct |
89 ms |
14164 KB |
Output is correct |
28 |
Correct |
84 ms |
13904 KB |
Output is correct |
29 |
Correct |
86 ms |
14124 KB |
Output is correct |
30 |
Correct |
86 ms |
14132 KB |
Output is correct |
31 |
Correct |
1 ms |
2392 KB |
Output is correct |
32 |
Correct |
1 ms |
2392 KB |
Output is correct |
33 |
Correct |
0 ms |
2396 KB |
Output is correct |
34 |
Correct |
1 ms |
2396 KB |
Output is correct |
35 |
Correct |
1 ms |
2504 KB |
Output is correct |
36 |
Correct |
1 ms |
2392 KB |
Output is correct |
37 |
Correct |
1 ms |
2396 KB |
Output is correct |
38 |
Correct |
1 ms |
2396 KB |
Output is correct |
39 |
Correct |
0 ms |
2396 KB |
Output is correct |
40 |
Correct |
0 ms |
2396 KB |
Output is correct |
41 |
Correct |
1 ms |
2648 KB |
Output is correct |
42 |
Correct |
1 ms |
2392 KB |
Output is correct |
43 |
Correct |
1 ms |
2392 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 |
2392 KB |
Output is correct |
47 |
Correct |
1 ms |
2396 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 |
2392 KB |
Output is correct |
51 |
Correct |
1 ms |
2396 KB |
Output is correct |
52 |
Correct |
1 ms |
2512 KB |
Output is correct |
53 |
Correct |
1 ms |
2396 KB |
Output is correct |
54 |
Correct |
1 ms |
2556 KB |
Output is correct |
55 |
Correct |
2 ms |
2396 KB |
Output is correct |
56 |
Correct |
1 ms |
2392 KB |
Output is correct |
57 |
Correct |
1 ms |
2396 KB |
Output is correct |
58 |
Correct |
1 ms |
2396 KB |
Output is correct |
59 |
Correct |
1 ms |
2396 KB |
Output is correct |
60 |
Correct |
1 ms |
2512 KB |
Output is correct |
61 |
Correct |
119 ms |
13872 KB |
Output is correct |
62 |
Correct |
120 ms |
14164 KB |
Output is correct |
63 |
Correct |
100 ms |
13868 KB |
Output is correct |
64 |
Correct |
134 ms |
14108 KB |
Output is correct |
65 |
Correct |
124 ms |
13992 KB |
Output is correct |
66 |
Correct |
125 ms |
14160 KB |
Output is correct |
67 |
Correct |
120 ms |
13868 KB |
Output is correct |
68 |
Correct |
89 ms |
13864 KB |
Output is correct |
69 |
Correct |
98 ms |
14028 KB |
Output is correct |
70 |
Correct |
96 ms |
14012 KB |
Output is correct |