#include<bits/stdc++.h>
using namespace std;
#define int long long
const int inf = 1e18;
vector<vector<int>> table;
int query(int l, int r){
if (l > r) return inf;
int len = r-l+1;
int k = 63- __builtin_clzll(len);
return min(table[l][k], table[r-(1<<k)+1][k]);
}
signed main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
int n,k;
cin>>n>>k;
if(k == 1){
int bst = 0;
int cur = 0;
for(int i = 0; i<n; i++){
int a; cin>>a;
cur = max(0ll, cur+a);
bst = max(bst,cur);
}
cout<<bst<<endl;
return 0;
}
if(n > 3000){
int s = 0;
for(int i = 0; i<n; i++){
int a; cin>>a;
s+= max(0ll,a);
}
cout<<s<<endl;
return 0;
}
vector<int> v(n); for(int& i : v) cin>>i;
vector<int> pre(n+1); for(int i =0 ; i<n; i++) pre[i+1] = pre[i] + v[i];
table.resize(n+1, vector<int>(25,inf));
for(int i = 0; i<=n; i++) table[i][0] = pre[i];
for(int k = 1; k<25; k++){
for(int i = 0; i<n; i++){
int covers = i + (1<<k) -1;
if(covers >= n) break;
int nxt = i + (1<<(k-1));
table[i][k] = min(table[i][k-1], table[nxt][k-1]);
}
}
vector<vector<int>> dp(n+1, vector<int>(k+1,0)); // using i with i first, and k customers
for(int i = 0; i<=n; i++) dp[i][0] = 0;
for(int j = 1; j<=k; j++){
for(int i = 1; i<=n; i++){
int idx = i-1;
for(int z = 0; z<i; z++){
dp[i][j] = max(dp[i][j], dp[z][j] + pre[i] - pre[z]);
dp[i][j] = max(dp[i][j], dp[z][j-1] + pre[i] - query(z, i-1));
}
}
}
int ans = 0;
for(int i = 0; i<=n; i++) ans = max(ans, dp[i][k]);
cout<<ans<<endl;
}
Compilation message
feast.cpp: In function 'int main()':
feast.cpp:57:8: warning: unused variable 'idx' [-Wunused-variable]
57 | int idx = i-1;
| ^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
21 ms |
344 KB |
Output is correct |
2 |
Correct |
21 ms |
348 KB |
Output is correct |
3 |
Correct |
22 ms |
348 KB |
Output is correct |
4 |
Correct |
22 ms |
460 KB |
Output is correct |
5 |
Correct |
21 ms |
348 KB |
Output is correct |
6 |
Correct |
21 ms |
348 KB |
Output is correct |
7 |
Correct |
21 ms |
348 KB |
Output is correct |
8 |
Correct |
22 ms |
348 KB |
Output is correct |
9 |
Correct |
21 ms |
348 KB |
Output is correct |
10 |
Correct |
22 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
344 KB |
Output is correct |
2 |
Correct |
16 ms |
348 KB |
Output is correct |
3 |
Correct |
21 ms |
348 KB |
Output is correct |
4 |
Correct |
15 ms |
348 KB |
Output is correct |
5 |
Correct |
23 ms |
348 KB |
Output is correct |
6 |
Correct |
14 ms |
348 KB |
Output is correct |
7 |
Correct |
14 ms |
348 KB |
Output is correct |
8 |
Correct |
22 ms |
348 KB |
Output is correct |
9 |
Correct |
26 ms |
348 KB |
Output is correct |
10 |
Correct |
21 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
24 ms |
348 KB |
Output is correct |
2 |
Correct |
36 ms |
348 KB |
Output is correct |
3 |
Correct |
24 ms |
452 KB |
Output is correct |
4 |
Correct |
23 ms |
348 KB |
Output is correct |
5 |
Correct |
24 ms |
348 KB |
Output is correct |
6 |
Correct |
31 ms |
348 KB |
Output is correct |
7 |
Correct |
31 ms |
348 KB |
Output is correct |
8 |
Correct |
25 ms |
348 KB |
Output is correct |
9 |
Correct |
24 ms |
348 KB |
Output is correct |
10 |
Correct |
38 ms |
2128 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
344 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
344 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
9 ms |
600 KB |
Output is correct |
12 |
Correct |
10 ms |
664 KB |
Output is correct |
13 |
Correct |
4 ms |
344 KB |
Output is correct |
14 |
Correct |
8 ms |
604 KB |
Output is correct |
15 |
Correct |
6 ms |
604 KB |
Output is correct |
16 |
Correct |
4 ms |
348 KB |
Output is correct |
17 |
Correct |
11 ms |
656 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
3 ms |
344 KB |
Output is correct |
20 |
Correct |
2 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
344 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
9 ms |
600 KB |
Output is correct |
12 |
Correct |
10 ms |
664 KB |
Output is correct |
13 |
Correct |
4 ms |
344 KB |
Output is correct |
14 |
Correct |
8 ms |
604 KB |
Output is correct |
15 |
Correct |
6 ms |
604 KB |
Output is correct |
16 |
Correct |
4 ms |
348 KB |
Output is correct |
17 |
Correct |
11 ms |
656 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
3 ms |
344 KB |
Output is correct |
20 |
Correct |
2 ms |
348 KB |
Output is correct |
21 |
Execution timed out |
1044 ms |
3932 KB |
Time limit exceeded |
22 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
21 ms |
344 KB |
Output is correct |
2 |
Correct |
21 ms |
348 KB |
Output is correct |
3 |
Correct |
22 ms |
348 KB |
Output is correct |
4 |
Correct |
22 ms |
460 KB |
Output is correct |
5 |
Correct |
21 ms |
348 KB |
Output is correct |
6 |
Correct |
21 ms |
348 KB |
Output is correct |
7 |
Correct |
21 ms |
348 KB |
Output is correct |
8 |
Correct |
22 ms |
348 KB |
Output is correct |
9 |
Correct |
21 ms |
348 KB |
Output is correct |
10 |
Correct |
22 ms |
348 KB |
Output is correct |
11 |
Correct |
14 ms |
344 KB |
Output is correct |
12 |
Correct |
16 ms |
348 KB |
Output is correct |
13 |
Correct |
21 ms |
348 KB |
Output is correct |
14 |
Correct |
15 ms |
348 KB |
Output is correct |
15 |
Correct |
23 ms |
348 KB |
Output is correct |
16 |
Correct |
14 ms |
348 KB |
Output is correct |
17 |
Correct |
14 ms |
348 KB |
Output is correct |
18 |
Correct |
22 ms |
348 KB |
Output is correct |
19 |
Correct |
26 ms |
348 KB |
Output is correct |
20 |
Correct |
21 ms |
348 KB |
Output is correct |
21 |
Correct |
24 ms |
348 KB |
Output is correct |
22 |
Correct |
36 ms |
348 KB |
Output is correct |
23 |
Correct |
24 ms |
452 KB |
Output is correct |
24 |
Correct |
23 ms |
348 KB |
Output is correct |
25 |
Correct |
24 ms |
348 KB |
Output is correct |
26 |
Correct |
31 ms |
348 KB |
Output is correct |
27 |
Correct |
31 ms |
348 KB |
Output is correct |
28 |
Correct |
25 ms |
348 KB |
Output is correct |
29 |
Correct |
24 ms |
348 KB |
Output is correct |
30 |
Correct |
38 ms |
2128 KB |
Output is correct |
31 |
Correct |
0 ms |
348 KB |
Output is correct |
32 |
Correct |
1 ms |
348 KB |
Output is correct |
33 |
Correct |
1 ms |
348 KB |
Output is correct |
34 |
Correct |
1 ms |
348 KB |
Output is correct |
35 |
Correct |
1 ms |
344 KB |
Output is correct |
36 |
Correct |
1 ms |
348 KB |
Output is correct |
37 |
Correct |
1 ms |
348 KB |
Output is correct |
38 |
Correct |
1 ms |
348 KB |
Output is correct |
39 |
Correct |
0 ms |
348 KB |
Output is correct |
40 |
Correct |
1 ms |
348 KB |
Output is correct |
41 |
Correct |
9 ms |
600 KB |
Output is correct |
42 |
Correct |
10 ms |
664 KB |
Output is correct |
43 |
Correct |
4 ms |
344 KB |
Output is correct |
44 |
Correct |
8 ms |
604 KB |
Output is correct |
45 |
Correct |
6 ms |
604 KB |
Output is correct |
46 |
Correct |
4 ms |
348 KB |
Output is correct |
47 |
Correct |
11 ms |
656 KB |
Output is correct |
48 |
Correct |
1 ms |
348 KB |
Output is correct |
49 |
Correct |
3 ms |
344 KB |
Output is correct |
50 |
Correct |
2 ms |
348 KB |
Output is correct |
51 |
Execution timed out |
1044 ms |
3932 KB |
Time limit exceeded |
52 |
Halted |
0 ms |
0 KB |
- |