#include <bits/stdc++.h>
using namespace std;
#define int long long
const int N = 3e5 + 5;
const int inf = 0x3f3f3f3f3f3f3f3f;
int a[N],pre[N],n,k;
namespace sub1 {
bool check_condition() {
for (int i = 1; i <= n; i++)
if (a[i] < 0) return false;
return true;
}
void solve() {
int ans = 0;
for (int i = 1; i <= n; i++) ans += a[i];
cout << ans << "\n";
}
}
namespace sub2 {
bool check_condition() {
int cnt = 0;
for (int i = 1; i <= n; i++)
if (a[i] < 0) cnt++;
return (cnt == 1);
}
void solve() {
pre[0] = 0;
int pos = 0;
for (int i = 1; i <= n; i++) {
pre[i] = pre[i - 1] + a[i];
if (a[i] < 0) pos = i;
}
if (k == 1) cout << max(max(pre[pos - 1],pre[n] - pre[pos]),pre[n]) << "\n";
else cout << pre[n] - a[pos] << "\n";
}
}
namespace sub3 {
void solve() {
int ans = -inf,sum = 0;
for (int i = 1; i <= n; i++) {
sum = max(sum + a[i],a[i]);
ans = max(ans,sum);
}
cout << ans << "\n";
}
}
namespace sub5 {
int dp[305][305];
void solve() {
pre[0] = 0;
for (int i = 1; i <= n; i++) pre[i] = pre[i - 1] + a[i];
dp[0][0] = 0;
for (int i = 1; i <= n; i++)
for (int j = 1; j <= k; j++) {
dp[i][j] = dp[i - 1][j];
for (int z = 1; z <= i; z++)
dp[i][j] = max(dp[i][j],dp[z - 1][j - 1] + pre[i] - pre[z - 1]);
}
cout << dp[n][k] << "\n";
}
}
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
cin >> n >> k;
for (int i = 1; i <= n; i++) cin >> a[i];
if (sub1::check_condition()) sub1::solve();
else if (sub2::check_condition()) sub2::solve();
else if (k == 1) sub3::solve();
else if (n <= 300) sub5::solve();
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
22 ms |
5468 KB |
Output is correct |
2 |
Correct |
29 ms |
5468 KB |
Output is correct |
3 |
Correct |
23 ms |
5464 KB |
Output is correct |
4 |
Correct |
22 ms |
5464 KB |
Output is correct |
5 |
Correct |
23 ms |
5468 KB |
Output is correct |
6 |
Correct |
28 ms |
5452 KB |
Output is correct |
7 |
Correct |
22 ms |
5464 KB |
Output is correct |
8 |
Correct |
26 ms |
5724 KB |
Output is correct |
9 |
Correct |
22 ms |
5468 KB |
Output is correct |
10 |
Correct |
22 ms |
5596 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
15 ms |
5468 KB |
Output is correct |
2 |
Correct |
15 ms |
5676 KB |
Output is correct |
3 |
Correct |
15 ms |
5608 KB |
Output is correct |
4 |
Correct |
15 ms |
5596 KB |
Output is correct |
5 |
Correct |
22 ms |
5464 KB |
Output is correct |
6 |
Correct |
15 ms |
5468 KB |
Output is correct |
7 |
Correct |
15 ms |
5468 KB |
Output is correct |
8 |
Correct |
23 ms |
5692 KB |
Output is correct |
9 |
Correct |
22 ms |
5468 KB |
Output is correct |
10 |
Correct |
15 ms |
5468 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
25 ms |
5572 KB |
Output is correct |
2 |
Correct |
25 ms |
5468 KB |
Output is correct |
3 |
Correct |
27 ms |
5596 KB |
Output is correct |
4 |
Correct |
28 ms |
5584 KB |
Output is correct |
5 |
Correct |
25 ms |
5468 KB |
Output is correct |
6 |
Correct |
26 ms |
5456 KB |
Output is correct |
7 |
Correct |
25 ms |
5596 KB |
Output is correct |
8 |
Correct |
26 ms |
5608 KB |
Output is correct |
9 |
Correct |
34 ms |
5468 KB |
Output is correct |
10 |
Correct |
26 ms |
5468 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4444 KB |
Output is correct |
2 |
Correct |
1 ms |
4700 KB |
Output is correct |
3 |
Correct |
1 ms |
4952 KB |
Output is correct |
4 |
Correct |
1 ms |
4700 KB |
Output is correct |
5 |
Correct |
1 ms |
4700 KB |
Output is correct |
6 |
Correct |
1 ms |
4700 KB |
Output is correct |
7 |
Correct |
1 ms |
4700 KB |
Output is correct |
8 |
Correct |
1 ms |
4700 KB |
Output is correct |
9 |
Correct |
1 ms |
4444 KB |
Output is correct |
10 |
Correct |
1 ms |
4700 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4444 KB |
Output is correct |
2 |
Correct |
1 ms |
4700 KB |
Output is correct |
3 |
Correct |
1 ms |
4952 KB |
Output is correct |
4 |
Correct |
1 ms |
4700 KB |
Output is correct |
5 |
Correct |
1 ms |
4700 KB |
Output is correct |
6 |
Correct |
1 ms |
4700 KB |
Output is correct |
7 |
Correct |
1 ms |
4700 KB |
Output is correct |
8 |
Correct |
1 ms |
4700 KB |
Output is correct |
9 |
Correct |
1 ms |
4444 KB |
Output is correct |
10 |
Correct |
1 ms |
4700 KB |
Output is correct |
11 |
Correct |
3 ms |
4700 KB |
Output is correct |
12 |
Correct |
4 ms |
4696 KB |
Output is correct |
13 |
Correct |
2 ms |
4696 KB |
Output is correct |
14 |
Correct |
3 ms |
4700 KB |
Output is correct |
15 |
Correct |
2 ms |
4700 KB |
Output is correct |
16 |
Correct |
2 ms |
4700 KB |
Output is correct |
17 |
Correct |
3 ms |
4700 KB |
Output is correct |
18 |
Correct |
1 ms |
4700 KB |
Output is correct |
19 |
Correct |
2 ms |
4952 KB |
Output is correct |
20 |
Correct |
1 ms |
4700 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4444 KB |
Output is correct |
2 |
Correct |
1 ms |
4700 KB |
Output is correct |
3 |
Correct |
1 ms |
4952 KB |
Output is correct |
4 |
Correct |
1 ms |
4700 KB |
Output is correct |
5 |
Correct |
1 ms |
4700 KB |
Output is correct |
6 |
Correct |
1 ms |
4700 KB |
Output is correct |
7 |
Correct |
1 ms |
4700 KB |
Output is correct |
8 |
Correct |
1 ms |
4700 KB |
Output is correct |
9 |
Correct |
1 ms |
4444 KB |
Output is correct |
10 |
Correct |
1 ms |
4700 KB |
Output is correct |
11 |
Correct |
3 ms |
4700 KB |
Output is correct |
12 |
Correct |
4 ms |
4696 KB |
Output is correct |
13 |
Correct |
2 ms |
4696 KB |
Output is correct |
14 |
Correct |
3 ms |
4700 KB |
Output is correct |
15 |
Correct |
2 ms |
4700 KB |
Output is correct |
16 |
Correct |
2 ms |
4700 KB |
Output is correct |
17 |
Correct |
3 ms |
4700 KB |
Output is correct |
18 |
Correct |
1 ms |
4700 KB |
Output is correct |
19 |
Correct |
2 ms |
4952 KB |
Output is correct |
20 |
Correct |
1 ms |
4700 KB |
Output is correct |
21 |
Incorrect |
1 ms |
4444 KB |
Output isn't correct |
22 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
22 ms |
5468 KB |
Output is correct |
2 |
Correct |
29 ms |
5468 KB |
Output is correct |
3 |
Correct |
23 ms |
5464 KB |
Output is correct |
4 |
Correct |
22 ms |
5464 KB |
Output is correct |
5 |
Correct |
23 ms |
5468 KB |
Output is correct |
6 |
Correct |
28 ms |
5452 KB |
Output is correct |
7 |
Correct |
22 ms |
5464 KB |
Output is correct |
8 |
Correct |
26 ms |
5724 KB |
Output is correct |
9 |
Correct |
22 ms |
5468 KB |
Output is correct |
10 |
Correct |
22 ms |
5596 KB |
Output is correct |
11 |
Correct |
15 ms |
5468 KB |
Output is correct |
12 |
Correct |
15 ms |
5676 KB |
Output is correct |
13 |
Correct |
15 ms |
5608 KB |
Output is correct |
14 |
Correct |
15 ms |
5596 KB |
Output is correct |
15 |
Correct |
22 ms |
5464 KB |
Output is correct |
16 |
Correct |
15 ms |
5468 KB |
Output is correct |
17 |
Correct |
15 ms |
5468 KB |
Output is correct |
18 |
Correct |
23 ms |
5692 KB |
Output is correct |
19 |
Correct |
22 ms |
5468 KB |
Output is correct |
20 |
Correct |
15 ms |
5468 KB |
Output is correct |
21 |
Correct |
25 ms |
5572 KB |
Output is correct |
22 |
Correct |
25 ms |
5468 KB |
Output is correct |
23 |
Correct |
27 ms |
5596 KB |
Output is correct |
24 |
Correct |
28 ms |
5584 KB |
Output is correct |
25 |
Correct |
25 ms |
5468 KB |
Output is correct |
26 |
Correct |
26 ms |
5456 KB |
Output is correct |
27 |
Correct |
25 ms |
5596 KB |
Output is correct |
28 |
Correct |
26 ms |
5608 KB |
Output is correct |
29 |
Correct |
34 ms |
5468 KB |
Output is correct |
30 |
Correct |
26 ms |
5468 KB |
Output is correct |
31 |
Correct |
1 ms |
4444 KB |
Output is correct |
32 |
Correct |
1 ms |
4700 KB |
Output is correct |
33 |
Correct |
1 ms |
4952 KB |
Output is correct |
34 |
Correct |
1 ms |
4700 KB |
Output is correct |
35 |
Correct |
1 ms |
4700 KB |
Output is correct |
36 |
Correct |
1 ms |
4700 KB |
Output is correct |
37 |
Correct |
1 ms |
4700 KB |
Output is correct |
38 |
Correct |
1 ms |
4700 KB |
Output is correct |
39 |
Correct |
1 ms |
4444 KB |
Output is correct |
40 |
Correct |
1 ms |
4700 KB |
Output is correct |
41 |
Correct |
3 ms |
4700 KB |
Output is correct |
42 |
Correct |
4 ms |
4696 KB |
Output is correct |
43 |
Correct |
2 ms |
4696 KB |
Output is correct |
44 |
Correct |
3 ms |
4700 KB |
Output is correct |
45 |
Correct |
2 ms |
4700 KB |
Output is correct |
46 |
Correct |
2 ms |
4700 KB |
Output is correct |
47 |
Correct |
3 ms |
4700 KB |
Output is correct |
48 |
Correct |
1 ms |
4700 KB |
Output is correct |
49 |
Correct |
2 ms |
4952 KB |
Output is correct |
50 |
Correct |
1 ms |
4700 KB |
Output is correct |
51 |
Incorrect |
1 ms |
4444 KB |
Output isn't correct |
52 |
Halted |
0 ms |
0 KB |
- |