//#include "fish.h"
#include <iostream>
#include <cmath>
#include <vector>
#include <set>
#include <map>
#include <algorithm>
using namespace std;
typedef long long ll;
const int MAX_N = 3e2 + 10;
const int M = 4e5 + 10;
ll col_sm[MAX_N][MAX_N];
ll dp[MAX_N][MAX_N][MAX_N];
ll dp_pref[MAX_N][MAX_N][MAX_N];
ll dp_suff[MAX_N][MAX_N][MAX_N];
ll pref_mx[MAX_N][MAX_N][MAX_N];
ll dpp[MAX_N][MAX_N][MAX_N];
ll esim[M][2][2];
ll f(int r, int c) {
if (r == -1) return 0;
return col_sm[r][c];
}
ll sm(int c, int r1, int r2) {
return f(c, r2) - f(c, r1 - 1);
}
ll max_weights(int N, int M, vector<int> X, vector<int> Y, vector<int> W) {
if (N <= 300) {
for (int i = 0; i < M; i++) {
col_sm[X[i]][Y[i]] = W[i];
}
for (int i = 0; i <= N; i++) {
for (int j = 0; j <= N; j++) {
col_sm[i][j] += col_sm[i][j - 1];
}
}
for (int h1 = 0; h1 <= N; h1++) {
for (int h2 = 0; h2 <= N; h2++) {
if (h1 >= h2) dp[1][h1][h2] = sm(1, h2, h1 - 1);
else dp[1][h1][h2] = sm(0, h1, h2 - 1);
}
}
for (int h1 = 0; h1 <= N; h1++) {
for (int h2 = 0; h2 <= N; h2++) {
pref_mx[1][h1][h2] = max(dp[1][h1][h2], pref_mx[1][h1 - 1][h2]);
if (h1 >= h2) {
dp_pref[1][h1][h2] = max(dp_pref[1][h1 - 1][h2], dp[1][h1][h2] - sm(1, h2, h1 - 1));
}
}
}
for (int h1 = N; h1 >= 0; h1--) {
for (int h2 = 0; h2 <= h1; h2++) {
dp_suff[1][h1][h2] = max(dp[1][h1][h2], dp_suff[1][h1 + 1][h2]);
}
}
for (int i = 2; i < N; i++) {
for (int h1 = 0; h1 <= N; h1++) {
for (int h2 = 0; h2 <= N; h2++) {
if (h1 >= h2) {
dp[i][h1][h2] = pref_mx[i - 1][N][h1] + sm(i, h2, h1 - 1);
}
else {
dp[i][h1][h2] = pref_mx[i - 1][h1][h1] + sm(i - 1, h1, h2 - 1);
dp[i][h1][h2] = max(dp[i][h1][h2], dp_suff[i - 1][h2][h1]);
dp[i][h1][h2] = max(dp[i][h1][h2], dp_pref[i - 1][h2][h1] + sm(i - 1, h1, h2 - 1));
}
}
}
for (int h1 = 0; h1 <= N; h1++) {
for (int h2 = 0; h2 <= N; h2++) {
pref_mx[i][h1][h2] = max(dp[i][h1][h2], pref_mx[i][h1 - 1][h2]);
if (h1 >= h2) {
dp_pref[i][h1][h2] = max(dp_pref[i][h1 - 1][h2], dp[i][h1][h2] - sm(i, h2, h1 - 1));
}
}
}
for (int h1 = N; h1 >= 0; h1--) {
for (int h2 = 0; h2 <= h1; h2++) {
dp_suff[i][h1][h2] = max(dp[i][h1][h2], dp_suff[i][h1 + 1][h2]);
}
}
}
ll mx = 0;
for (int h1 = 0; h1 <= N; h1++) {
for (int h2 = 0; h2 <= N; h2++) {
mx = max(mx, dp[N - 1][h1][h2]);
}
}
return mx;
}
bool ch = true;
ll sm = 0;
for (int i = 0; i < M; i++) {
if (X[i] % 2 == 1) {
ch = false;
}
sm += W[i];
}
if (ch) return sm;
bool ch2 = true;
for (int i = 0; i < M; i++) {
if (X[i] > 1) ch2 = false;
}
if (ch2) {
bool ch0 = false, ch1 = false;
for (int i = 0; i < M; i++) {
if (X[i] == 0) ch0 = true;
else ch1 = true;
}
if (!ch1 || !ch0) return sm;
sm = 0;
map <pair<int, int>, int> mp;
for (int i = 0; i < M; i++) {
mp[{ X[i], Y[i] }] = W[i];
if (X[i] == 1) sm += W[i];
}
ll ans = sm;
for (int i = 0; i < N; i++) {
sm -= mp[{ 1, i }];
sm += mp[{ 0, i }];
ans = max(ans, sm);
}
return ans;
}
map <pair<int, int>, int> mp;
for (int i = 0; i < M; i++) {
mp[{ X[i], Y[i] }] = W[i];
}
esim[1][0][0] = 0;
esim[1][0][1] = mp[{ 0, 0 }];
esim[1][1][0] = mp[{ 1, 0 }];
esim[1][1][1] = 0;
for (int i = 2; i < N; i++) {
esim[i][0][0] = max(esim[i - 1][1][0], esim[i - 1][0][0]);
esim[i][1][1] = max(esim[i - 1][1][1], esim[i - 1][0][1]);
esim[i][0][1] = max(esim[i - 1][1][0], esim[i - 1][0][0] + mp[{ i - 1, 0 }]);
esim[i][1][0] = max(esim[i - 1][0][1], esim[i - 1][1][1]) + mp[{ i, 0 }];
}
return max({ esim[N - 1][0][0], esim[N - 1][0][1],
esim[N - 1][1][0], esim[N - 1][1][1] });
}
/*
int main() {
//3 2
//0 0 1
//1 1 1
//cout << max_weights(3, 2, { 0, 1 }, { 0, 1 }, { 1, 1 });
}
*/
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
13 ms |
3412 KB |
Output is correct |
2 |
Correct |
16 ms |
3676 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
53 ms |
10616 KB |
Output is correct |
6 |
Correct |
55 ms |
10832 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4444 KB |
Output is correct |
2 |
Correct |
99 ms |
16812 KB |
Output is correct |
3 |
Correct |
115 ms |
19684 KB |
Output is correct |
4 |
Correct |
14 ms |
3152 KB |
Output is correct |
5 |
Correct |
16 ms |
3676 KB |
Output is correct |
6 |
Correct |
1 ms |
2392 KB |
Output is correct |
7 |
Correct |
0 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
0 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
13 ms |
3160 KB |
Output is correct |
13 |
Correct |
17 ms |
3676 KB |
Output is correct |
14 |
Correct |
63 ms |
14528 KB |
Output is correct |
15 |
Correct |
71 ms |
15912 KB |
Output is correct |
16 |
Correct |
59 ms |
14404 KB |
Output is correct |
17 |
Correct |
65 ms |
16076 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
23 ms |
10832 KB |
Output is correct |
3 |
Correct |
36 ms |
12372 KB |
Output is correct |
4 |
Correct |
33 ms |
12432 KB |
Output is correct |
5 |
Correct |
59 ms |
14932 KB |
Output is correct |
6 |
Correct |
58 ms |
14120 KB |
Output is correct |
7 |
Correct |
57 ms |
14676 KB |
Output is correct |
8 |
Correct |
65 ms |
14788 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4696 KB |
Output is correct |
2 |
Correct |
1 ms |
9052 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2392 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2500 KB |
Output is correct |
7 |
Correct |
1 ms |
4444 KB |
Output is correct |
8 |
Correct |
1 ms |
4444 KB |
Output is correct |
9 |
Correct |
78 ms |
339128 KB |
Output is correct |
10 |
Correct |
399 ms |
888660 KB |
Output is correct |
11 |
Correct |
81 ms |
339192 KB |
Output is correct |
12 |
Correct |
298 ms |
890496 KB |
Output is correct |
13 |
Correct |
24 ms |
121684 KB |
Output is correct |
14 |
Correct |
283 ms |
891420 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4696 KB |
Output is correct |
2 |
Correct |
1 ms |
9052 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2392 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2500 KB |
Output is correct |
7 |
Correct |
1 ms |
4444 KB |
Output is correct |
8 |
Correct |
1 ms |
4444 KB |
Output is correct |
9 |
Correct |
78 ms |
339128 KB |
Output is correct |
10 |
Correct |
399 ms |
888660 KB |
Output is correct |
11 |
Correct |
81 ms |
339192 KB |
Output is correct |
12 |
Correct |
298 ms |
890496 KB |
Output is correct |
13 |
Correct |
24 ms |
121684 KB |
Output is correct |
14 |
Correct |
283 ms |
891420 KB |
Output is correct |
15 |
Correct |
301 ms |
891476 KB |
Output is correct |
16 |
Correct |
37 ms |
121680 KB |
Output is correct |
17 |
Correct |
289 ms |
892244 KB |
Output is correct |
18 |
Correct |
277 ms |
892240 KB |
Output is correct |
19 |
Correct |
313 ms |
892240 KB |
Output is correct |
20 |
Correct |
324 ms |
892568 KB |
Output is correct |
21 |
Correct |
331 ms |
892860 KB |
Output is correct |
22 |
Correct |
309 ms |
894032 KB |
Output is correct |
23 |
Correct |
301 ms |
891900 KB |
Output is correct |
24 |
Correct |
302 ms |
892296 KB |
Output is correct |
25 |
Correct |
295 ms |
891760 KB |
Output is correct |
26 |
Correct |
299 ms |
891860 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4696 KB |
Output is correct |
2 |
Correct |
1 ms |
9052 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2392 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2500 KB |
Output is correct |
7 |
Correct |
1 ms |
4444 KB |
Output is correct |
8 |
Correct |
1 ms |
4444 KB |
Output is correct |
9 |
Correct |
78 ms |
339128 KB |
Output is correct |
10 |
Correct |
399 ms |
888660 KB |
Output is correct |
11 |
Correct |
81 ms |
339192 KB |
Output is correct |
12 |
Correct |
298 ms |
890496 KB |
Output is correct |
13 |
Correct |
24 ms |
121684 KB |
Output is correct |
14 |
Correct |
283 ms |
891420 KB |
Output is correct |
15 |
Correct |
301 ms |
891476 KB |
Output is correct |
16 |
Correct |
37 ms |
121680 KB |
Output is correct |
17 |
Correct |
289 ms |
892244 KB |
Output is correct |
18 |
Correct |
277 ms |
892240 KB |
Output is correct |
19 |
Correct |
313 ms |
892240 KB |
Output is correct |
20 |
Correct |
324 ms |
892568 KB |
Output is correct |
21 |
Correct |
331 ms |
892860 KB |
Output is correct |
22 |
Correct |
309 ms |
894032 KB |
Output is correct |
23 |
Correct |
301 ms |
891900 KB |
Output is correct |
24 |
Correct |
302 ms |
892296 KB |
Output is correct |
25 |
Correct |
295 ms |
891760 KB |
Output is correct |
26 |
Correct |
299 ms |
891860 KB |
Output is correct |
27 |
Incorrect |
2 ms |
860 KB |
1st lines differ - on the 1st token, expected: '2999', found: '1' |
28 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
23 ms |
10832 KB |
Output is correct |
3 |
Correct |
36 ms |
12372 KB |
Output is correct |
4 |
Correct |
33 ms |
12432 KB |
Output is correct |
5 |
Correct |
59 ms |
14932 KB |
Output is correct |
6 |
Correct |
58 ms |
14120 KB |
Output is correct |
7 |
Correct |
57 ms |
14676 KB |
Output is correct |
8 |
Correct |
65 ms |
14788 KB |
Output is correct |
9 |
Incorrect |
71 ms |
20820 KB |
1st lines differ - on the 1st token, expected: '99999', found: '1' |
10 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
13 ms |
3412 KB |
Output is correct |
2 |
Correct |
16 ms |
3676 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
53 ms |
10616 KB |
Output is correct |
6 |
Correct |
55 ms |
10832 KB |
Output is correct |
7 |
Correct |
1 ms |
4444 KB |
Output is correct |
8 |
Correct |
99 ms |
16812 KB |
Output is correct |
9 |
Correct |
115 ms |
19684 KB |
Output is correct |
10 |
Correct |
14 ms |
3152 KB |
Output is correct |
11 |
Correct |
16 ms |
3676 KB |
Output is correct |
12 |
Correct |
1 ms |
2392 KB |
Output is correct |
13 |
Correct |
0 ms |
2396 KB |
Output is correct |
14 |
Correct |
1 ms |
2396 KB |
Output is correct |
15 |
Correct |
0 ms |
2396 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
13 ms |
3160 KB |
Output is correct |
19 |
Correct |
17 ms |
3676 KB |
Output is correct |
20 |
Correct |
63 ms |
14528 KB |
Output is correct |
21 |
Correct |
71 ms |
15912 KB |
Output is correct |
22 |
Correct |
59 ms |
14404 KB |
Output is correct |
23 |
Correct |
65 ms |
16076 KB |
Output is correct |
24 |
Correct |
1 ms |
344 KB |
Output is correct |
25 |
Correct |
23 ms |
10832 KB |
Output is correct |
26 |
Correct |
36 ms |
12372 KB |
Output is correct |
27 |
Correct |
33 ms |
12432 KB |
Output is correct |
28 |
Correct |
59 ms |
14932 KB |
Output is correct |
29 |
Correct |
58 ms |
14120 KB |
Output is correct |
30 |
Correct |
57 ms |
14676 KB |
Output is correct |
31 |
Correct |
65 ms |
14788 KB |
Output is correct |
32 |
Correct |
1 ms |
4696 KB |
Output is correct |
33 |
Correct |
1 ms |
9052 KB |
Output is correct |
34 |
Correct |
1 ms |
2396 KB |
Output is correct |
35 |
Correct |
1 ms |
2392 KB |
Output is correct |
36 |
Correct |
1 ms |
2396 KB |
Output is correct |
37 |
Correct |
1 ms |
2500 KB |
Output is correct |
38 |
Correct |
1 ms |
4444 KB |
Output is correct |
39 |
Correct |
1 ms |
4444 KB |
Output is correct |
40 |
Correct |
78 ms |
339128 KB |
Output is correct |
41 |
Correct |
399 ms |
888660 KB |
Output is correct |
42 |
Correct |
81 ms |
339192 KB |
Output is correct |
43 |
Correct |
298 ms |
890496 KB |
Output is correct |
44 |
Correct |
24 ms |
121684 KB |
Output is correct |
45 |
Correct |
283 ms |
891420 KB |
Output is correct |
46 |
Correct |
301 ms |
891476 KB |
Output is correct |
47 |
Correct |
37 ms |
121680 KB |
Output is correct |
48 |
Correct |
289 ms |
892244 KB |
Output is correct |
49 |
Correct |
277 ms |
892240 KB |
Output is correct |
50 |
Correct |
313 ms |
892240 KB |
Output is correct |
51 |
Correct |
324 ms |
892568 KB |
Output is correct |
52 |
Correct |
331 ms |
892860 KB |
Output is correct |
53 |
Correct |
309 ms |
894032 KB |
Output is correct |
54 |
Correct |
301 ms |
891900 KB |
Output is correct |
55 |
Correct |
302 ms |
892296 KB |
Output is correct |
56 |
Correct |
295 ms |
891760 KB |
Output is correct |
57 |
Correct |
299 ms |
891860 KB |
Output is correct |
58 |
Incorrect |
2 ms |
860 KB |
1st lines differ - on the 1st token, expected: '2999', found: '1' |
59 |
Halted |
0 ms |
0 KB |
- |