#pragma GCC optimize("Os")
#include <bits/stdc++.h>
using namespace std;
const int MN = 2e6+5;
const int INF = 0x3f3f3f3f;
#define int long long
#define pb push_back
#define p make_pair
#define fi first
#define se second
#define pii pair<int,int>
#define f(i,a,b) for (int i = a ; i < b ; i++)
#define f1(i,a,b) for (int i = a ; i > b ; i--)
#define f2(i,a,b) for (int i = a ; i <= b ; i++)
#define FILE(X) \
freopen(#X ".INP","r",stdin); \
freopen(#X ".OUT","w",stdout);
mt19937 rd(chrono::steady_clock::now().time_since_epoch().count());
int random(int l, int r) { return l + rd() % (r - l + 1); }
int a[MN];
int n,k,pos;
int cnt = 0;
int dp[5003][5003];
signed main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
bool flag = false;
cin >> n >> k;
for (int i = 1 ; i <= n ; i++) {
cin >> a[i];
if (a[i] < 0) flag = true,cnt++, pos = i;
}
if (flag == false) cout << accumulate(a+1,a+n+1,0LL);
else if (cnt == 1) {
if (k == 1) {
int sum = 0, sum2 = 0;
for (int i = 1 ; i < pos ; i++) sum += a[i];
for (int i = pos+1 ; i <= n ; i++) sum2 += a[i];
cout << max(sum,sum2) << "\n";
}
else {
cout << accumulate(a+1,a+n+1,0LL) - a[pos];
}
}
else if (cnt == n) cout << 0;
else if (n <= 5000) {
for (int i = 0 ; i <= n ; i++) dp[i][1] = 0;
for (int i = 1 ; i <= n ; i++) {
for (int j = 0 ; j <= k ; j++) {
dp[i][j+1] = max(dp[i][j+1],dp[i-1][j]);
dp[i][j] = max(dp[i][j],dp[i-1][j] + a[i]);
}
}
int ans = 0;
for (int i = 0 ; i <= k ; i++) ans = max(ans,dp[n][i]);
cout << ans <<"\n";
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
22 ms |
5968 KB |
Output is correct |
2 |
Correct |
26 ms |
7216 KB |
Output is correct |
3 |
Correct |
23 ms |
7252 KB |
Output is correct |
4 |
Correct |
23 ms |
7248 KB |
Output is correct |
5 |
Correct |
23 ms |
7252 KB |
Output is correct |
6 |
Correct |
23 ms |
7248 KB |
Output is correct |
7 |
Correct |
24 ms |
7220 KB |
Output is correct |
8 |
Correct |
23 ms |
7256 KB |
Output is correct |
9 |
Correct |
22 ms |
7248 KB |
Output is correct |
10 |
Correct |
23 ms |
7248 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
4956 KB |
Output is correct |
2 |
Incorrect |
14 ms |
5464 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
25 ms |
6232 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
6488 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
6488 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
6488 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
22 ms |
5968 KB |
Output is correct |
2 |
Correct |
26 ms |
7216 KB |
Output is correct |
3 |
Correct |
23 ms |
7252 KB |
Output is correct |
4 |
Correct |
23 ms |
7248 KB |
Output is correct |
5 |
Correct |
23 ms |
7252 KB |
Output is correct |
6 |
Correct |
23 ms |
7248 KB |
Output is correct |
7 |
Correct |
24 ms |
7220 KB |
Output is correct |
8 |
Correct |
23 ms |
7256 KB |
Output is correct |
9 |
Correct |
22 ms |
7248 KB |
Output is correct |
10 |
Correct |
23 ms |
7248 KB |
Output is correct |
11 |
Correct |
14 ms |
4956 KB |
Output is correct |
12 |
Incorrect |
14 ms |
5464 KB |
Output isn't correct |
13 |
Halted |
0 ms |
0 KB |
- |