# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
1101092 |
2024-10-15T13:22:18 Z |
trie |
Feast (NOI19_feast) |
C++17 |
|
114 ms |
104188 KB |
#include<bits/stdc++.h>
#define mp make_pair
#define fi first
#define se second
#define ii pair<int, int>
#define iii pair<ii, int>
#define ll long long
#define pb push_back
#define pli pair<ll, int>
#define all(v) (v).begin(), (v).end()
#define uniquev(v) sort(all(v)), (v).resize(unique(all(v)) - (v).begin())
#define cntbit(mask) __builtin_popcount(mask)
#define getbit(x, i) ((x >> i) & 1)
#define MASK(i) (1 << i)
#define pli pair<ll, int>
using namespace std;
template<class T1, class T2>
bool maximize(T1 &a, T2 b){if(a < b){a = b; return 1;} return 0;}
template<class T1, class T2>
bool minimize(T1 &a, T2 b){if(a > b){a = b; return 1;} return 0;}
const int N = 2005;
const int V = 3e5 + 5;
const int MOD = 1e9 + 7;
int n, k;
ll pr[V], dp[N][N], mx[N][N], a[V];
int negative, idx;
void solve() {
cin >> n >> k;
for(int i = 1 ; i <= n ; i++) {
cin >> a[i];
pr[i] = pr[i - 1] + a[i];
if (a[i] < 0) {
negative++;
idx = i;
}
}
if (!negative) return cout << pr[n], void();
if (k == 1) { // kadane algorithm.
ll sum = 0, mxsum = 0;
for(int i = 1 ; i <= n ; i++) {
sum += a[i];
maximize(mxsum, sum);
if (sum < 0) sum = 0;
}
return cout << mxsum, void();
}
if (negative == 1) return cout << pr[n] - a[idx], void();
for(int i = 1 ; i <= n ; i++) mx[i][0] = max(mx[i - 1][0], - pr[i]);
for(int i = 1 ; i <= n ; i++)
for(int j = 1 ; j <= n ; j++) {
dp[i][j] = dp[i - 1][j];
maximize(dp[i][j], mx[i][j - 1] + pr[i]);
mx[i][j] = max(mx[i - 1][j], dp[i][j] - pr[i]);
}
cout << dp[n][k];
}
int main(void) {
//freopen("task.inp", "r", stdin);
//freopen("task.out", "w", stdout);
ios_base::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
int t = 1; while(t--) solve();
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
20 ms |
6992 KB |
Output is correct |
2 |
Correct |
21 ms |
6992 KB |
Output is correct |
3 |
Correct |
21 ms |
7080 KB |
Output is correct |
4 |
Correct |
21 ms |
6992 KB |
Output is correct |
5 |
Correct |
20 ms |
7036 KB |
Output is correct |
6 |
Correct |
20 ms |
6992 KB |
Output is correct |
7 |
Correct |
20 ms |
6992 KB |
Output is correct |
8 |
Correct |
21 ms |
6988 KB |
Output is correct |
9 |
Correct |
20 ms |
6896 KB |
Output is correct |
10 |
Correct |
20 ms |
6992 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
14 ms |
6968 KB |
Output is correct |
2 |
Correct |
14 ms |
6992 KB |
Output is correct |
3 |
Correct |
14 ms |
6992 KB |
Output is correct |
4 |
Correct |
15 ms |
6992 KB |
Output is correct |
5 |
Correct |
22 ms |
9784 KB |
Output is correct |
6 |
Correct |
14 ms |
8016 KB |
Output is correct |
7 |
Correct |
14 ms |
8016 KB |
Output is correct |
8 |
Correct |
22 ms |
9820 KB |
Output is correct |
9 |
Correct |
22 ms |
9728 KB |
Output is correct |
10 |
Correct |
14 ms |
8016 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
23 ms |
6992 KB |
Output is correct |
2 |
Correct |
23 ms |
6960 KB |
Output is correct |
3 |
Correct |
23 ms |
6992 KB |
Output is correct |
4 |
Correct |
23 ms |
6992 KB |
Output is correct |
5 |
Correct |
23 ms |
6992 KB |
Output is correct |
6 |
Correct |
23 ms |
6984 KB |
Output is correct |
7 |
Correct |
23 ms |
6984 KB |
Output is correct |
8 |
Correct |
23 ms |
6996 KB |
Output is correct |
9 |
Correct |
33 ms |
6984 KB |
Output is correct |
10 |
Correct |
24 ms |
6964 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
1104 KB |
Output is correct |
3 |
Correct |
1 ms |
1104 KB |
Output is correct |
4 |
Correct |
1 ms |
1104 KB |
Output is correct |
5 |
Correct |
2 ms |
1104 KB |
Output is correct |
6 |
Correct |
1 ms |
1104 KB |
Output is correct |
7 |
Correct |
1 ms |
1104 KB |
Output is correct |
8 |
Correct |
1 ms |
1292 KB |
Output is correct |
9 |
Correct |
1 ms |
336 KB |
Output is correct |
10 |
Correct |
1 ms |
1104 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
1104 KB |
Output is correct |
3 |
Correct |
1 ms |
1104 KB |
Output is correct |
4 |
Correct |
1 ms |
1104 KB |
Output is correct |
5 |
Correct |
2 ms |
1104 KB |
Output is correct |
6 |
Correct |
1 ms |
1104 KB |
Output is correct |
7 |
Correct |
1 ms |
1104 KB |
Output is correct |
8 |
Correct |
1 ms |
1292 KB |
Output is correct |
9 |
Correct |
1 ms |
336 KB |
Output is correct |
10 |
Correct |
1 ms |
1104 KB |
Output is correct |
11 |
Correct |
3 ms |
9808 KB |
Output is correct |
12 |
Correct |
2 ms |
9808 KB |
Output is correct |
13 |
Correct |
2 ms |
9976 KB |
Output is correct |
14 |
Correct |
2 ms |
9808 KB |
Output is correct |
15 |
Correct |
2 ms |
9808 KB |
Output is correct |
16 |
Correct |
3 ms |
9808 KB |
Output is correct |
17 |
Correct |
2 ms |
9808 KB |
Output is correct |
18 |
Correct |
2 ms |
9808 KB |
Output is correct |
19 |
Correct |
2 ms |
9808 KB |
Output is correct |
20 |
Correct |
2 ms |
9808 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
1104 KB |
Output is correct |
3 |
Correct |
1 ms |
1104 KB |
Output is correct |
4 |
Correct |
1 ms |
1104 KB |
Output is correct |
5 |
Correct |
2 ms |
1104 KB |
Output is correct |
6 |
Correct |
1 ms |
1104 KB |
Output is correct |
7 |
Correct |
1 ms |
1104 KB |
Output is correct |
8 |
Correct |
1 ms |
1292 KB |
Output is correct |
9 |
Correct |
1 ms |
336 KB |
Output is correct |
10 |
Correct |
1 ms |
1104 KB |
Output is correct |
11 |
Correct |
3 ms |
9808 KB |
Output is correct |
12 |
Correct |
2 ms |
9808 KB |
Output is correct |
13 |
Correct |
2 ms |
9976 KB |
Output is correct |
14 |
Correct |
2 ms |
9808 KB |
Output is correct |
15 |
Correct |
2 ms |
9808 KB |
Output is correct |
16 |
Correct |
3 ms |
9808 KB |
Output is correct |
17 |
Correct |
2 ms |
9808 KB |
Output is correct |
18 |
Correct |
2 ms |
9808 KB |
Output is correct |
19 |
Correct |
2 ms |
9808 KB |
Output is correct |
20 |
Correct |
2 ms |
9808 KB |
Output is correct |
21 |
Correct |
22 ms |
61464 KB |
Output is correct |
22 |
Correct |
24 ms |
61436 KB |
Output is correct |
23 |
Correct |
21 ms |
61564 KB |
Output is correct |
24 |
Correct |
23 ms |
61520 KB |
Output is correct |
25 |
Correct |
23 ms |
63316 KB |
Output is correct |
26 |
Correct |
21 ms |
63056 KB |
Output is correct |
27 |
Correct |
22 ms |
63048 KB |
Output is correct |
28 |
Correct |
25 ms |
63056 KB |
Output is correct |
29 |
Correct |
22 ms |
62800 KB |
Output is correct |
30 |
Correct |
18 ms |
61520 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
20 ms |
6992 KB |
Output is correct |
2 |
Correct |
21 ms |
6992 KB |
Output is correct |
3 |
Correct |
21 ms |
7080 KB |
Output is correct |
4 |
Correct |
21 ms |
6992 KB |
Output is correct |
5 |
Correct |
20 ms |
7036 KB |
Output is correct |
6 |
Correct |
20 ms |
6992 KB |
Output is correct |
7 |
Correct |
20 ms |
6992 KB |
Output is correct |
8 |
Correct |
21 ms |
6988 KB |
Output is correct |
9 |
Correct |
20 ms |
6896 KB |
Output is correct |
10 |
Correct |
20 ms |
6992 KB |
Output is correct |
11 |
Correct |
14 ms |
6968 KB |
Output is correct |
12 |
Correct |
14 ms |
6992 KB |
Output is correct |
13 |
Correct |
14 ms |
6992 KB |
Output is correct |
14 |
Correct |
15 ms |
6992 KB |
Output is correct |
15 |
Correct |
22 ms |
9784 KB |
Output is correct |
16 |
Correct |
14 ms |
8016 KB |
Output is correct |
17 |
Correct |
14 ms |
8016 KB |
Output is correct |
18 |
Correct |
22 ms |
9820 KB |
Output is correct |
19 |
Correct |
22 ms |
9728 KB |
Output is correct |
20 |
Correct |
14 ms |
8016 KB |
Output is correct |
21 |
Correct |
23 ms |
6992 KB |
Output is correct |
22 |
Correct |
23 ms |
6960 KB |
Output is correct |
23 |
Correct |
23 ms |
6992 KB |
Output is correct |
24 |
Correct |
23 ms |
6992 KB |
Output is correct |
25 |
Correct |
23 ms |
6992 KB |
Output is correct |
26 |
Correct |
23 ms |
6984 KB |
Output is correct |
27 |
Correct |
23 ms |
6984 KB |
Output is correct |
28 |
Correct |
23 ms |
6996 KB |
Output is correct |
29 |
Correct |
33 ms |
6984 KB |
Output is correct |
30 |
Correct |
24 ms |
6964 KB |
Output is correct |
31 |
Correct |
1 ms |
336 KB |
Output is correct |
32 |
Correct |
1 ms |
1104 KB |
Output is correct |
33 |
Correct |
1 ms |
1104 KB |
Output is correct |
34 |
Correct |
1 ms |
1104 KB |
Output is correct |
35 |
Correct |
2 ms |
1104 KB |
Output is correct |
36 |
Correct |
1 ms |
1104 KB |
Output is correct |
37 |
Correct |
1 ms |
1104 KB |
Output is correct |
38 |
Correct |
1 ms |
1292 KB |
Output is correct |
39 |
Correct |
1 ms |
336 KB |
Output is correct |
40 |
Correct |
1 ms |
1104 KB |
Output is correct |
41 |
Correct |
3 ms |
9808 KB |
Output is correct |
42 |
Correct |
2 ms |
9808 KB |
Output is correct |
43 |
Correct |
2 ms |
9976 KB |
Output is correct |
44 |
Correct |
2 ms |
9808 KB |
Output is correct |
45 |
Correct |
2 ms |
9808 KB |
Output is correct |
46 |
Correct |
3 ms |
9808 KB |
Output is correct |
47 |
Correct |
2 ms |
9808 KB |
Output is correct |
48 |
Correct |
2 ms |
9808 KB |
Output is correct |
49 |
Correct |
2 ms |
9808 KB |
Output is correct |
50 |
Correct |
2 ms |
9808 KB |
Output is correct |
51 |
Correct |
22 ms |
61464 KB |
Output is correct |
52 |
Correct |
24 ms |
61436 KB |
Output is correct |
53 |
Correct |
21 ms |
61564 KB |
Output is correct |
54 |
Correct |
23 ms |
61520 KB |
Output is correct |
55 |
Correct |
23 ms |
63316 KB |
Output is correct |
56 |
Correct |
21 ms |
63056 KB |
Output is correct |
57 |
Correct |
22 ms |
63048 KB |
Output is correct |
58 |
Correct |
25 ms |
63056 KB |
Output is correct |
59 |
Correct |
22 ms |
62800 KB |
Output is correct |
60 |
Correct |
18 ms |
61520 KB |
Output is correct |
61 |
Runtime error |
114 ms |
104188 KB |
Execution killed with signal 11 |
62 |
Halted |
0 ms |
0 KB |
- |