#include <iostream>
#include <vector>
#include <set>
#include <map>
#include <algorithm>
#include <cmath>
using namespace std;
typedef vector<int> vi;
typedef pair<int,int> pi;
typedef long long ll;
#define loop(i,a,b) for (int i = a; i <= b; i++)
#define INF ((unsigned) ~0)
#define F first
#define S second
#define PB push_back
#define MP make_pair
const int MXN = 500000;
int N, K;
int a[MXN];
vector<ll> g;
vector<ll> sg;
ll mem[4000][4000];
ll dp(int l, int n){
if(l >= g.size())
return 0;
if(n <= 0)
return 0;
if(mem[l][n] != -1)
return mem[l][n];
ll sol = sg[l + 1] - sg[l];
for(int i = l + 1; i <= g.size(); i++)
for(int j = l; j <= i; j++)
sol = max(sol, dp(i, n - 1) + sg[i] - sg[j]);
mem[l][n] = sol;
return sol;
}
int main(){
cin >> N >> K;
for(int i = 0; i < N; i++){
cin >> a[i];
if(g.size() == 0){
g.push_back(a[i]);
}else if((g[g.size() - 1] >= 0 && a[i] >= 0) || (g[g.size() - 1] < 0 && a[i] < 0)){
g[g.size() - 1] += a[i];
}else{
//cout << g[g.size() - 1] << " ";
g.push_back(a[i]);
}
}
//cout << "\n";
ll sm = 0;
sg.push_back(0);
for(int i = 0; i < g.size(); i++){
sm += g[i];
sg.push_back(sm);
}
if(g.size() == 1){
cout << sm << "\n";
}else if(g.size() == 2){
cout << max(g[0], g[1]) << "\n";
}else if(g.size() == 3 && g[0] > 0){
if(K == 1)
cout << max(max(g[0], g[2]), g[0] + g[1] + g[2]) << "\n";
if(K >= 2)
cout << g[0] + g[2] << "\n";
}else if(K == 1){
ll mx = 0;
ll mxEnd = 0;
for(int i = 0; i < g.size(); i++){
mxEnd = max(mxEnd + g[i], g[i]);
mx = max(mx, mxEnd);
}
cout << mx << "\n";
}else{
for(int i = 0; i < 4000; i++)
for(int j = 0; j < 4000; j++)
mem[i][j] = -1;
cout << dp(0, N) << "\n";
}
}
Compilation message
feast.cpp: In function 'll dp(int, int)':
feast.cpp:30:10: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
if(l >= g.size())
~~^~~~~~~~~~~
feast.cpp:37:26: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i = l + 1; i <= g.size(); i++)
~~^~~~~~~~~~~
feast.cpp: In function 'int main()':
feast.cpp:60:22: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i = 0; i < g.size(); i++){
~~^~~~~~~~~~
feast.cpp:76:26: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i = 0; i < g.size(); i++){
~~^~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
179 ms |
1400 KB |
Output is correct |
2 |
Correct |
179 ms |
1528 KB |
Output is correct |
3 |
Correct |
178 ms |
1528 KB |
Output is correct |
4 |
Correct |
176 ms |
1528 KB |
Output is correct |
5 |
Correct |
175 ms |
1528 KB |
Output is correct |
6 |
Correct |
179 ms |
1532 KB |
Output is correct |
7 |
Correct |
175 ms |
1456 KB |
Output is correct |
8 |
Correct |
177 ms |
1528 KB |
Output is correct |
9 |
Correct |
174 ms |
1624 KB |
Output is correct |
10 |
Correct |
179 ms |
1528 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
93 ms |
1528 KB |
Output is correct |
2 |
Correct |
100 ms |
1528 KB |
Output is correct |
3 |
Correct |
92 ms |
1400 KB |
Output is correct |
4 |
Correct |
96 ms |
1528 KB |
Output is correct |
5 |
Correct |
175 ms |
1528 KB |
Output is correct |
6 |
Correct |
95 ms |
1400 KB |
Output is correct |
7 |
Correct |
98 ms |
1528 KB |
Output is correct |
8 |
Correct |
184 ms |
1528 KB |
Output is correct |
9 |
Correct |
177 ms |
1528 KB |
Output is correct |
10 |
Correct |
97 ms |
1528 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
200 ms |
5716 KB |
Output is correct |
2 |
Correct |
191 ms |
5736 KB |
Output is correct |
3 |
Correct |
195 ms |
5732 KB |
Output is correct |
4 |
Correct |
195 ms |
5732 KB |
Output is correct |
5 |
Correct |
200 ms |
5776 KB |
Output is correct |
6 |
Correct |
200 ms |
5888 KB |
Output is correct |
7 |
Correct |
201 ms |
5860 KB |
Output is correct |
8 |
Correct |
199 ms |
5872 KB |
Output is correct |
9 |
Correct |
199 ms |
5732 KB |
Output is correct |
10 |
Correct |
201 ms |
5732 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
384 KB |
Output is correct |
2 |
Correct |
65 ms |
125560 KB |
Output is correct |
3 |
Incorrect |
78 ms |
125560 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
384 KB |
Output is correct |
2 |
Correct |
65 ms |
125560 KB |
Output is correct |
3 |
Incorrect |
78 ms |
125560 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
384 KB |
Output is correct |
2 |
Correct |
65 ms |
125560 KB |
Output is correct |
3 |
Incorrect |
78 ms |
125560 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
179 ms |
1400 KB |
Output is correct |
2 |
Correct |
179 ms |
1528 KB |
Output is correct |
3 |
Correct |
178 ms |
1528 KB |
Output is correct |
4 |
Correct |
176 ms |
1528 KB |
Output is correct |
5 |
Correct |
175 ms |
1528 KB |
Output is correct |
6 |
Correct |
179 ms |
1532 KB |
Output is correct |
7 |
Correct |
175 ms |
1456 KB |
Output is correct |
8 |
Correct |
177 ms |
1528 KB |
Output is correct |
9 |
Correct |
174 ms |
1624 KB |
Output is correct |
10 |
Correct |
179 ms |
1528 KB |
Output is correct |
11 |
Correct |
93 ms |
1528 KB |
Output is correct |
12 |
Correct |
100 ms |
1528 KB |
Output is correct |
13 |
Correct |
92 ms |
1400 KB |
Output is correct |
14 |
Correct |
96 ms |
1528 KB |
Output is correct |
15 |
Correct |
175 ms |
1528 KB |
Output is correct |
16 |
Correct |
95 ms |
1400 KB |
Output is correct |
17 |
Correct |
98 ms |
1528 KB |
Output is correct |
18 |
Correct |
184 ms |
1528 KB |
Output is correct |
19 |
Correct |
177 ms |
1528 KB |
Output is correct |
20 |
Correct |
97 ms |
1528 KB |
Output is correct |
21 |
Correct |
200 ms |
5716 KB |
Output is correct |
22 |
Correct |
191 ms |
5736 KB |
Output is correct |
23 |
Correct |
195 ms |
5732 KB |
Output is correct |
24 |
Correct |
195 ms |
5732 KB |
Output is correct |
25 |
Correct |
200 ms |
5776 KB |
Output is correct |
26 |
Correct |
200 ms |
5888 KB |
Output is correct |
27 |
Correct |
201 ms |
5860 KB |
Output is correct |
28 |
Correct |
199 ms |
5872 KB |
Output is correct |
29 |
Correct |
199 ms |
5732 KB |
Output is correct |
30 |
Correct |
201 ms |
5732 KB |
Output is correct |
31 |
Correct |
4 ms |
384 KB |
Output is correct |
32 |
Correct |
65 ms |
125560 KB |
Output is correct |
33 |
Incorrect |
78 ms |
125560 KB |
Output isn't correct |
34 |
Halted |
0 ms |
0 KB |
- |