#include<bits/stdc++.h>
#define int long long
#define pb push_back
#define fast ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
#define MOD 1000000007
#define INF 1e18
#define fi first
#define se second
#define endl "\n"
#define FOR(i,a,b) for(int i=a;i<=b;i++)
#define FORD(i,a,b) for(int i=a;i>=b;i--)
#define sz(a) ((int)(a).size())
#define pi 3.14159265359
#define TASKNAME "party_new"
template<typename T> bool maximize(T &res, const T &val) { if (res < val){ res = val; return true; }; return false; }
template<typename T> bool minimize(T &res, const T &val) { if (res > val){ res = val; return true; }; return false; }
using namespace std;
typedef pair<int,int> ii;
typedef pair<int,ii> iii;
typedef vector<int> vi;
const int MAXN = 2e3 + 9;
int pre[300005],a[300005];
int n,k, suma = 0;
bool sub1 = true;
main()
{
fast;
if (fopen(TASKNAME".inp","r")){
freopen(TASKNAME".inp","r",stdin);
freopen(TASKNAME".out","w",stdout);
}
cin>>n>>k;
for(int i=1;i<=n;i++){
cin>>a[i];
suma += a[i];
pre[i] = pre[i-1] + a[i];
if (a[i] < 0) sub1 = false;
}
if (sub1){
cout<<suma<<endl;
return 0;
}
if (n * k <= 1e7){
vector<vector<int>> dp(k+1,vector<int>(n+1,-INF));
vector<vector<int>> opt(k+1,vector<int>(n+1,-INF));
int ans = 0;
for(int i=0;i<=n;i++){
dp[0][i] = 0;
opt[0][i] = max( ((i>0) ? opt[0][i-1] : -1000000000000000000), dp[0][i] - pre[i]);
}
for(int i=1;i<=k;i++){
for(int j=1;j<=n;j++){
maximize(dp[i][j],dp[i][j-1]);
maximize(dp[i][j], opt[i-1][j-1] + pre[j]);
maximize(ans, dp[i][j]);
// cout<<dp[i][j]<<' ';
}
// cout<<endl;
for(int j=0;j<=n;j++){
opt[i][j] = max( ((j>0) ? opt[i][j-1] : -1000000000000000000), dp[i][j] - pre[j]);
}
}
// cout<<endl;
cout<<ans<<endl;
}
}
Compilation message
feast.cpp:25:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
25 | main()
| ^~~~
feast.cpp: In function 'int main()':
feast.cpp:29:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
29 | freopen(TASKNAME".inp","r",stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
feast.cpp:30:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
30 | freopen(TASKNAME".out","w",stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
24 ms |
4948 KB |
Output is correct |
2 |
Correct |
24 ms |
6708 KB |
Output is correct |
3 |
Correct |
24 ms |
6848 KB |
Output is correct |
4 |
Correct |
23 ms |
6752 KB |
Output is correct |
5 |
Correct |
24 ms |
6740 KB |
Output is correct |
6 |
Correct |
23 ms |
6480 KB |
Output is correct |
7 |
Correct |
29 ms |
6288 KB |
Output is correct |
8 |
Correct |
28 ms |
6736 KB |
Output is correct |
9 |
Correct |
27 ms |
6480 KB |
Output is correct |
10 |
Correct |
28 ms |
7148 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
22 ms |
16480 KB |
Output is correct |
2 |
Correct |
22 ms |
17968 KB |
Output is correct |
3 |
Correct |
28 ms |
17604 KB |
Output is correct |
4 |
Incorrect |
22 ms |
6036 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
32 ms |
16704 KB |
Output is correct |
2 |
Correct |
36 ms |
18652 KB |
Output is correct |
3 |
Correct |
38 ms |
18424 KB |
Output is correct |
4 |
Correct |
33 ms |
18276 KB |
Output is correct |
5 |
Correct |
32 ms |
18432 KB |
Output is correct |
6 |
Correct |
33 ms |
18492 KB |
Output is correct |
7 |
Correct |
33 ms |
18392 KB |
Output is correct |
8 |
Correct |
38 ms |
18296 KB |
Output is correct |
9 |
Correct |
33 ms |
18588 KB |
Output is correct |
10 |
Correct |
34 ms |
18484 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
344 KB |
Output is correct |
6 |
Correct |
0 ms |
344 KB |
Output is correct |
7 |
Correct |
1 ms |
344 KB |
Output is correct |
8 |
Correct |
0 ms |
344 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
344 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
344 KB |
Output is correct |
6 |
Correct |
0 ms |
344 KB |
Output is correct |
7 |
Correct |
1 ms |
344 KB |
Output is correct |
8 |
Correct |
0 ms |
344 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
344 KB |
Output is correct |
11 |
Correct |
0 ms |
600 KB |
Output is correct |
12 |
Correct |
1 ms |
600 KB |
Output is correct |
13 |
Correct |
0 ms |
600 KB |
Output is correct |
14 |
Correct |
0 ms |
604 KB |
Output is correct |
15 |
Correct |
0 ms |
600 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
600 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
344 KB |
Output is correct |
6 |
Correct |
0 ms |
344 KB |
Output is correct |
7 |
Correct |
1 ms |
344 KB |
Output is correct |
8 |
Correct |
0 ms |
344 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
344 KB |
Output is correct |
11 |
Correct |
0 ms |
600 KB |
Output is correct |
12 |
Correct |
1 ms |
600 KB |
Output is correct |
13 |
Correct |
0 ms |
600 KB |
Output is correct |
14 |
Correct |
0 ms |
604 KB |
Output is correct |
15 |
Correct |
0 ms |
600 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
600 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
5 ms |
6488 KB |
Output is correct |
22 |
Correct |
36 ms |
51548 KB |
Output is correct |
23 |
Correct |
10 ms |
12124 KB |
Output is correct |
24 |
Correct |
5 ms |
7000 KB |
Output is correct |
25 |
Correct |
7 ms |
9564 KB |
Output is correct |
26 |
Correct |
4 ms |
5980 KB |
Output is correct |
27 |
Correct |
6 ms |
9184 KB |
Output is correct |
28 |
Correct |
1 ms |
1368 KB |
Output is correct |
29 |
Correct |
1 ms |
1372 KB |
Output is correct |
30 |
Correct |
1 ms |
604 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
24 ms |
4948 KB |
Output is correct |
2 |
Correct |
24 ms |
6708 KB |
Output is correct |
3 |
Correct |
24 ms |
6848 KB |
Output is correct |
4 |
Correct |
23 ms |
6752 KB |
Output is correct |
5 |
Correct |
24 ms |
6740 KB |
Output is correct |
6 |
Correct |
23 ms |
6480 KB |
Output is correct |
7 |
Correct |
29 ms |
6288 KB |
Output is correct |
8 |
Correct |
28 ms |
6736 KB |
Output is correct |
9 |
Correct |
27 ms |
6480 KB |
Output is correct |
10 |
Correct |
28 ms |
7148 KB |
Output is correct |
11 |
Correct |
22 ms |
16480 KB |
Output is correct |
12 |
Correct |
22 ms |
17968 KB |
Output is correct |
13 |
Correct |
28 ms |
17604 KB |
Output is correct |
14 |
Incorrect |
22 ms |
6036 KB |
Output isn't correct |
15 |
Halted |
0 ms |
0 KB |
- |