#include <bits/stdc++.h>
#define sz(x) (int)x.size()
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define int long long
using namespace std;
using ll = long long;
using ull = unsigned long long;
using ld = long double;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
const int mod = 1e9 + 7;
const int LOG = 20;
const int maxn = 1e5 + 5;
const double eps = 1e-9;
ll dp[500][500];
int n, K;
vector<int> v;
ll f(int pos, int sub) {
if(pos == n + 1) return (sub == K ? 0 : -1e18);
if(dp[pos][sub] != -1) return dp[pos][sub];
ll ans = max(f(pos+1, sub), f(pos+1, sub+1));
ll sum = 0;
for(int i=pos; i<=n; i++) {
sum += v[i];
ans = max(ans, sum + f(i+1, sub+1));
}
return dp[pos][sub] = ans;
}
int32_t main() {
int neg = 0;
cin >> n >> K;
v.resize(n+1);
vector<ll> pref(n+1);
for(int i=1; i<=n; i++) {
cin >> v[i];
neg += (v[i] < 0);
pref[i] = pref[i-1] + v[i];
}
if(neg == 0) {
cout << pref[n] << '\n';
return 0;
}
if(K == 1) {
ll ans = 0, sum = 0, mn = 0;
for(int i=1; i<=n; i++) {
sum += v[i];
ans = max(ans, sum - mn);
mn = min(mn, sum);
}
cout << ans << '\n';
return 0;
}
memset(dp, -1, sizeof(dp));
cout << f(1, 0) << '\n';
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
100 ms |
6520 KB |
Output is correct |
2 |
Correct |
81 ms |
6740 KB |
Output is correct |
3 |
Correct |
82 ms |
6744 KB |
Output is correct |
4 |
Correct |
82 ms |
7132 KB |
Output is correct |
5 |
Correct |
82 ms |
6748 KB |
Output is correct |
6 |
Correct |
80 ms |
6480 KB |
Output is correct |
7 |
Correct |
80 ms |
6484 KB |
Output is correct |
8 |
Correct |
100 ms |
6864 KB |
Output is correct |
9 |
Correct |
80 ms |
6480 KB |
Output is correct |
10 |
Correct |
81 ms |
6736 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
43 ms |
5724 KB |
Output is correct |
2 |
Correct |
44 ms |
5724 KB |
Output is correct |
3 |
Correct |
46 ms |
5780 KB |
Output is correct |
4 |
Runtime error |
48 ms |
14516 KB |
Execution killed with signal 11 |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
88 ms |
6740 KB |
Output is correct |
2 |
Correct |
91 ms |
6996 KB |
Output is correct |
3 |
Correct |
88 ms |
6736 KB |
Output is correct |
4 |
Correct |
88 ms |
6784 KB |
Output is correct |
5 |
Correct |
88 ms |
6736 KB |
Output is correct |
6 |
Correct |
89 ms |
6880 KB |
Output is correct |
7 |
Correct |
89 ms |
6892 KB |
Output is correct |
8 |
Correct |
87 ms |
6852 KB |
Output is correct |
9 |
Correct |
89 ms |
6740 KB |
Output is correct |
10 |
Correct |
89 ms |
6736 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2236 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
2 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
2240 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
2140 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2236 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
2 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
2240 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
2140 KB |
Output is correct |
11 |
Correct |
6 ms |
2396 KB |
Output is correct |
12 |
Correct |
5 ms |
2396 KB |
Output is correct |
13 |
Correct |
5 ms |
2396 KB |
Output is correct |
14 |
Correct |
5 ms |
2236 KB |
Output is correct |
15 |
Correct |
6 ms |
2396 KB |
Output is correct |
16 |
Correct |
4 ms |
2392 KB |
Output is correct |
17 |
Correct |
5 ms |
2416 KB |
Output is correct |
18 |
Correct |
5 ms |
2396 KB |
Output is correct |
19 |
Correct |
5 ms |
2396 KB |
Output is correct |
20 |
Correct |
6 ms |
2396 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2236 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
2 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
2240 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
2140 KB |
Output is correct |
11 |
Correct |
6 ms |
2396 KB |
Output is correct |
12 |
Correct |
5 ms |
2396 KB |
Output is correct |
13 |
Correct |
5 ms |
2396 KB |
Output is correct |
14 |
Correct |
5 ms |
2236 KB |
Output is correct |
15 |
Correct |
6 ms |
2396 KB |
Output is correct |
16 |
Correct |
4 ms |
2392 KB |
Output is correct |
17 |
Correct |
5 ms |
2416 KB |
Output is correct |
18 |
Correct |
5 ms |
2396 KB |
Output is correct |
19 |
Correct |
5 ms |
2396 KB |
Output is correct |
20 |
Correct |
6 ms |
2396 KB |
Output is correct |
21 |
Runtime error |
3 ms |
4440 KB |
Execution killed with signal 11 |
22 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
100 ms |
6520 KB |
Output is correct |
2 |
Correct |
81 ms |
6740 KB |
Output is correct |
3 |
Correct |
82 ms |
6744 KB |
Output is correct |
4 |
Correct |
82 ms |
7132 KB |
Output is correct |
5 |
Correct |
82 ms |
6748 KB |
Output is correct |
6 |
Correct |
80 ms |
6480 KB |
Output is correct |
7 |
Correct |
80 ms |
6484 KB |
Output is correct |
8 |
Correct |
100 ms |
6864 KB |
Output is correct |
9 |
Correct |
80 ms |
6480 KB |
Output is correct |
10 |
Correct |
81 ms |
6736 KB |
Output is correct |
11 |
Correct |
43 ms |
5724 KB |
Output is correct |
12 |
Correct |
44 ms |
5724 KB |
Output is correct |
13 |
Correct |
46 ms |
5780 KB |
Output is correct |
14 |
Runtime error |
48 ms |
14516 KB |
Execution killed with signal 11 |
15 |
Halted |
0 ms |
0 KB |
- |