#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pii pair<int, int>
#define pll pair<ll, ll>
#define ff first
#define ss second
#define pb push_back
#define SZ(x) ((int)(x).size())
#define all(x) x.begin(), x.end()
#define debug(x) cout << #x << ": " << x << " "
#define nl cout << "\n"
#define rep(i, a, b) for(int i = (a); i < (b); i++)
#define per(i, a, b) for(int i = (a); i >= (b); i--)
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const ll INF = 1e16;
const int MAXN = 3e5+100;
int N, K, a[MAXN];
ll dp[MAXN];
pll get(ll penalty){
vector<ll> dp(N + 1, 0), cnt(N + 1, 0);
ll s = 0, curcnt = 0, mx = 0, mxcnt = 0;
rep(i, 1, N + 1){
dp[i] = dp[i - 1], cnt[i] = cnt[i - 1];
s += a[i];
if(s < 0){
s = 0, curcnt = 0;
}
if(mx < s){
mx = s, mxcnt = curcnt;
}
if(mx - penalty > dp[i]){
dp[i] = mx - penalty;
cnt[i] = mxcnt + 1;
}
if(dp[i] > s){
s = dp[i], curcnt = cnt[i];
}
if(s > mx){
mx = s, mxcnt = cnt[i];
}
}
return {dp[N], cnt[N]};
}
void solve(){
cin >> N >> K;
int cnt = 0;
rep(i, 1, N + 1){
cin >> a[i];
}
ll l = -1, r = INF;
while(r - l > 1){
ll m = (l + r) / 2;
pll ans = get(m);
if(ans.ss <= K){
r = m;
} else{
l = m;
}
}
pll ans = get(r);
ll solution = ans.ff + ans.ss * r;
cout << solution;
}
int main(){
ios_base::sync_with_stdio(false), cin.tie(nullptr);
solve();
return 0;
}
Compilation message
feast.cpp: In function 'void solve()':
feast.cpp:50:9: warning: unused variable 'cnt' [-Wunused-variable]
50 | int cnt = 0;
| ^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
178 ms |
6364 KB |
Output is correct |
2 |
Correct |
198 ms |
6344 KB |
Output is correct |
3 |
Correct |
178 ms |
6376 KB |
Output is correct |
4 |
Correct |
182 ms |
6352 KB |
Output is correct |
5 |
Correct |
184 ms |
6308 KB |
Output is correct |
6 |
Correct |
175 ms |
6228 KB |
Output is correct |
7 |
Correct |
185 ms |
6204 KB |
Output is correct |
8 |
Correct |
200 ms |
6456 KB |
Output is correct |
9 |
Correct |
175 ms |
6360 KB |
Output is correct |
10 |
Correct |
177 ms |
6320 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
171 ms |
6412 KB |
Output is correct |
2 |
Correct |
182 ms |
6588 KB |
Output is correct |
3 |
Correct |
184 ms |
6364 KB |
Output is correct |
4 |
Correct |
197 ms |
6460 KB |
Output is correct |
5 |
Correct |
193 ms |
6228 KB |
Output is correct |
6 |
Correct |
166 ms |
6252 KB |
Output is correct |
7 |
Correct |
170 ms |
6396 KB |
Output is correct |
8 |
Correct |
186 ms |
6360 KB |
Output is correct |
9 |
Correct |
185 ms |
6240 KB |
Output is correct |
10 |
Correct |
171 ms |
6416 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
218 ms |
6536 KB |
Output is correct |
2 |
Correct |
212 ms |
6584 KB |
Output is correct |
3 |
Correct |
223 ms |
6504 KB |
Output is correct |
4 |
Correct |
202 ms |
6452 KB |
Output is correct |
5 |
Correct |
243 ms |
6536 KB |
Output is correct |
6 |
Correct |
227 ms |
6552 KB |
Output is correct |
7 |
Correct |
207 ms |
6512 KB |
Output is correct |
8 |
Correct |
218 ms |
6560 KB |
Output is correct |
9 |
Correct |
224 ms |
6560 KB |
Output is correct |
10 |
Correct |
211 ms |
6468 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
332 KB |
Output is correct |
12 |
Correct |
1 ms |
332 KB |
Output is correct |
13 |
Correct |
1 ms |
332 KB |
Output is correct |
14 |
Correct |
1 ms |
332 KB |
Output is correct |
15 |
Correct |
1 ms |
332 KB |
Output is correct |
16 |
Correct |
1 ms |
332 KB |
Output is correct |
17 |
Correct |
1 ms |
332 KB |
Output is correct |
18 |
Correct |
1 ms |
332 KB |
Output is correct |
19 |
Correct |
1 ms |
332 KB |
Output is correct |
20 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
332 KB |
Output is correct |
12 |
Correct |
1 ms |
332 KB |
Output is correct |
13 |
Correct |
1 ms |
332 KB |
Output is correct |
14 |
Correct |
1 ms |
332 KB |
Output is correct |
15 |
Correct |
1 ms |
332 KB |
Output is correct |
16 |
Correct |
1 ms |
332 KB |
Output is correct |
17 |
Correct |
1 ms |
332 KB |
Output is correct |
18 |
Correct |
1 ms |
332 KB |
Output is correct |
19 |
Correct |
1 ms |
332 KB |
Output is correct |
20 |
Correct |
1 ms |
332 KB |
Output is correct |
21 |
Correct |
2 ms |
332 KB |
Output is correct |
22 |
Correct |
2 ms |
332 KB |
Output is correct |
23 |
Correct |
2 ms |
332 KB |
Output is correct |
24 |
Correct |
2 ms |
332 KB |
Output is correct |
25 |
Correct |
2 ms |
368 KB |
Output is correct |
26 |
Correct |
2 ms |
332 KB |
Output is correct |
27 |
Correct |
2 ms |
332 KB |
Output is correct |
28 |
Correct |
2 ms |
332 KB |
Output is correct |
29 |
Correct |
2 ms |
332 KB |
Output is correct |
30 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
178 ms |
6364 KB |
Output is correct |
2 |
Correct |
198 ms |
6344 KB |
Output is correct |
3 |
Correct |
178 ms |
6376 KB |
Output is correct |
4 |
Correct |
182 ms |
6352 KB |
Output is correct |
5 |
Correct |
184 ms |
6308 KB |
Output is correct |
6 |
Correct |
175 ms |
6228 KB |
Output is correct |
7 |
Correct |
185 ms |
6204 KB |
Output is correct |
8 |
Correct |
200 ms |
6456 KB |
Output is correct |
9 |
Correct |
175 ms |
6360 KB |
Output is correct |
10 |
Correct |
177 ms |
6320 KB |
Output is correct |
11 |
Correct |
171 ms |
6412 KB |
Output is correct |
12 |
Correct |
182 ms |
6588 KB |
Output is correct |
13 |
Correct |
184 ms |
6364 KB |
Output is correct |
14 |
Correct |
197 ms |
6460 KB |
Output is correct |
15 |
Correct |
193 ms |
6228 KB |
Output is correct |
16 |
Correct |
166 ms |
6252 KB |
Output is correct |
17 |
Correct |
170 ms |
6396 KB |
Output is correct |
18 |
Correct |
186 ms |
6360 KB |
Output is correct |
19 |
Correct |
185 ms |
6240 KB |
Output is correct |
20 |
Correct |
171 ms |
6416 KB |
Output is correct |
21 |
Correct |
218 ms |
6536 KB |
Output is correct |
22 |
Correct |
212 ms |
6584 KB |
Output is correct |
23 |
Correct |
223 ms |
6504 KB |
Output is correct |
24 |
Correct |
202 ms |
6452 KB |
Output is correct |
25 |
Correct |
243 ms |
6536 KB |
Output is correct |
26 |
Correct |
227 ms |
6552 KB |
Output is correct |
27 |
Correct |
207 ms |
6512 KB |
Output is correct |
28 |
Correct |
218 ms |
6560 KB |
Output is correct |
29 |
Correct |
224 ms |
6560 KB |
Output is correct |
30 |
Correct |
211 ms |
6468 KB |
Output is correct |
31 |
Correct |
1 ms |
332 KB |
Output is correct |
32 |
Correct |
1 ms |
332 KB |
Output is correct |
33 |
Correct |
1 ms |
332 KB |
Output is correct |
34 |
Correct |
1 ms |
204 KB |
Output is correct |
35 |
Correct |
1 ms |
332 KB |
Output is correct |
36 |
Correct |
1 ms |
204 KB |
Output is correct |
37 |
Correct |
1 ms |
204 KB |
Output is correct |
38 |
Correct |
1 ms |
332 KB |
Output is correct |
39 |
Correct |
1 ms |
332 KB |
Output is correct |
40 |
Correct |
1 ms |
332 KB |
Output is correct |
41 |
Correct |
1 ms |
332 KB |
Output is correct |
42 |
Correct |
1 ms |
332 KB |
Output is correct |
43 |
Correct |
1 ms |
332 KB |
Output is correct |
44 |
Correct |
1 ms |
332 KB |
Output is correct |
45 |
Correct |
1 ms |
332 KB |
Output is correct |
46 |
Correct |
1 ms |
332 KB |
Output is correct |
47 |
Correct |
1 ms |
332 KB |
Output is correct |
48 |
Correct |
1 ms |
332 KB |
Output is correct |
49 |
Correct |
1 ms |
332 KB |
Output is correct |
50 |
Correct |
1 ms |
332 KB |
Output is correct |
51 |
Correct |
2 ms |
332 KB |
Output is correct |
52 |
Correct |
2 ms |
332 KB |
Output is correct |
53 |
Correct |
2 ms |
332 KB |
Output is correct |
54 |
Correct |
2 ms |
332 KB |
Output is correct |
55 |
Correct |
2 ms |
368 KB |
Output is correct |
56 |
Correct |
2 ms |
332 KB |
Output is correct |
57 |
Correct |
2 ms |
332 KB |
Output is correct |
58 |
Correct |
2 ms |
332 KB |
Output is correct |
59 |
Correct |
2 ms |
332 KB |
Output is correct |
60 |
Correct |
1 ms |
332 KB |
Output is correct |
61 |
Correct |
252 ms |
6260 KB |
Output is correct |
62 |
Correct |
259 ms |
9432 KB |
Output is correct |
63 |
Correct |
226 ms |
9064 KB |
Output is correct |
64 |
Correct |
258 ms |
9304 KB |
Output is correct |
65 |
Correct |
262 ms |
9272 KB |
Output is correct |
66 |
Correct |
267 ms |
9216 KB |
Output is correct |
67 |
Correct |
253 ms |
9200 KB |
Output is correct |
68 |
Correct |
217 ms |
9308 KB |
Output is correct |
69 |
Correct |
212 ms |
9208 KB |
Output is correct |
70 |
Correct |
209 ms |
9036 KB |
Output is correct |