#include "fish.h"
#include <bits/stdc++.h>
using namespace std;
int n;
vector<vector<long long>> b;
vector<vector<long long>> nxt;
vector<vector<long long>> pre;
vector<vector<vector<long long>>> dp;
vector<vector<pair<int, int>>> in_col;
long long bt(int col, int prev, int cur) {
if (col == n) {
return 0;
}
long long& ret = dp[col][prev][cur];
if (ret != -1) {
return ret;
}
ret = 0;
if (col == 0) {
for (int i = 0; i < (int) in_col[col + 1].size(); ++i) {
long long w = max(0LL, pre[col + 1][i] - b[col][cur]);
ret = max(ret, bt(col + 1, cur, i) + w);
}
} else if (col == n - 1) {
ret = max(0LL, nxt[col - 1][prev] - b[col][cur]);
} else {
for (int i = 0; i < (int) in_col[col + 1].size(); ++i) {
long long w = max(0LL, max(pre[col + 1][i], nxt[col - 1][prev]) - b[col][cur]);
ret = max(ret, bt(col + 1, cur, i) + w);
}
}
return ret;
}
long long max_weights(int N, int M, vector<int> X, vector<int> Y, vector<int> W) {
n = N;
in_col.resize(N);
for (int i = 0; i < M; ++i) {
in_col[X[i]].push_back({Y[i], W[i]});
}
for (int i = 0; i < N; ++i) {
sort(in_col[i].begin(), in_col[i].end());
in_col[i].push_back({N, 0});
}
dp.resize(N);
dp[0].resize(1);
dp[0][0].assign(in_col[0].size(), -1);
for (int i = 1; i < N; ++i) {
dp[i].resize(in_col[i - 1].size());
for (int j = 0; j < (int) in_col[i - 1].size(); ++j) {
dp[i][j].assign(in_col[i].size(), -1);
}
}
pre.resize(N);
for (int col = 1; col < N; ++col) {
pre[col].resize(in_col[col].size());
for (int i = 0; i < (int) in_col[col].size(); ++i) {
int ptr = 0;
long long sum = 0;
while (ptr < (int) in_col[col - 1].size() && in_col[col - 1][ptr].first < in_col[col][i].first) {
sum += in_col[col - 1][ptr].second;
ptr++;
}
pre[col][i] = sum;
}
}
nxt.resize(N);
for (int col = 0; col < N - 1; ++col) {
nxt[col].resize(in_col[col].size());
for (int i = 0; i < (int) in_col[col].size(); ++i) {
int ptr = 0;
long long sum = 0;
while (ptr < (int) in_col[col + 1].size() && in_col[col + 1][ptr].first < in_col[col][i].first) {
sum += in_col[col + 1][ptr].second;
ptr++;
}
nxt[col][i] = sum;
}
}
b.resize(N);
for (int col = 0; col < N; ++col) {
b[col].resize((int) in_col[col].size());
for (int i = 1; i < (int) in_col[col].size(); ++i) {
b[col][i] = b[col][i - 1] + in_col[col][i - 1].second;
}
}
long long ans = 0;
//ans = bt(0, 0, 0);
for (int i = 0; i < (int) in_col[0].size(); ++i) {
ans = max(ans, bt(0, 0, i));
}
return ans;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
62 ms |
49348 KB |
Output is correct |
2 |
Correct |
72 ms |
56192 KB |
Output is correct |
3 |
Correct |
43 ms |
43432 KB |
Output is correct |
4 |
Correct |
41 ms |
43472 KB |
Output is correct |
5 |
Correct |
196 ms |
85052 KB |
Output is correct |
6 |
Correct |
166 ms |
83800 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Runtime error |
973 ms |
2097152 KB |
Execution killed with signal 9 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
56 ms |
43344 KB |
Output is correct |
2 |
Correct |
54 ms |
43372 KB |
Output is correct |
3 |
Correct |
96 ms |
44572 KB |
Output is correct |
4 |
Correct |
74 ms |
47188 KB |
Output is correct |
5 |
Correct |
71 ms |
53720 KB |
Output is correct |
6 |
Correct |
72 ms |
52816 KB |
Output is correct |
7 |
Correct |
82 ms |
53428 KB |
Output is correct |
8 |
Correct |
77 ms |
53596 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
352 KB |
Output is correct |
8 |
Correct |
0 ms |
352 KB |
Output is correct |
9 |
Correct |
1 ms |
444 KB |
Output is correct |
10 |
Correct |
3 ms |
868 KB |
Output is correct |
11 |
Correct |
2 ms |
612 KB |
Output is correct |
12 |
Correct |
1 ms |
732 KB |
Output is correct |
13 |
Correct |
0 ms |
352 KB |
Output is correct |
14 |
Correct |
1 ms |
448 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
352 KB |
Output is correct |
8 |
Correct |
0 ms |
352 KB |
Output is correct |
9 |
Correct |
1 ms |
444 KB |
Output is correct |
10 |
Correct |
3 ms |
868 KB |
Output is correct |
11 |
Correct |
2 ms |
612 KB |
Output is correct |
12 |
Correct |
1 ms |
732 KB |
Output is correct |
13 |
Correct |
0 ms |
352 KB |
Output is correct |
14 |
Correct |
1 ms |
448 KB |
Output is correct |
15 |
Correct |
1 ms |
352 KB |
Output is correct |
16 |
Correct |
41 ms |
1632 KB |
Output is correct |
17 |
Execution timed out |
1069 ms |
87628 KB |
Time limit exceeded |
18 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
352 KB |
Output is correct |
8 |
Correct |
0 ms |
352 KB |
Output is correct |
9 |
Correct |
1 ms |
444 KB |
Output is correct |
10 |
Correct |
3 ms |
868 KB |
Output is correct |
11 |
Correct |
2 ms |
612 KB |
Output is correct |
12 |
Correct |
1 ms |
732 KB |
Output is correct |
13 |
Correct |
0 ms |
352 KB |
Output is correct |
14 |
Correct |
1 ms |
448 KB |
Output is correct |
15 |
Correct |
1 ms |
352 KB |
Output is correct |
16 |
Correct |
41 ms |
1632 KB |
Output is correct |
17 |
Execution timed out |
1069 ms |
87628 KB |
Time limit exceeded |
18 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
56 ms |
43344 KB |
Output is correct |
2 |
Correct |
54 ms |
43372 KB |
Output is correct |
3 |
Correct |
96 ms |
44572 KB |
Output is correct |
4 |
Correct |
74 ms |
47188 KB |
Output is correct |
5 |
Correct |
71 ms |
53720 KB |
Output is correct |
6 |
Correct |
72 ms |
52816 KB |
Output is correct |
7 |
Correct |
82 ms |
53428 KB |
Output is correct |
8 |
Correct |
77 ms |
53596 KB |
Output is correct |
9 |
Correct |
82 ms |
53380 KB |
Output is correct |
10 |
Correct |
59 ms |
32340 KB |
Output is correct |
11 |
Correct |
152 ms |
64472 KB |
Output is correct |
12 |
Correct |
0 ms |
352 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
436 KB |
Output is correct |
16 |
Correct |
0 ms |
352 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
44 ms |
43348 KB |
Output is correct |
19 |
Correct |
39 ms |
43440 KB |
Output is correct |
20 |
Correct |
37 ms |
43356 KB |
Output is correct |
21 |
Correct |
56 ms |
43348 KB |
Output is correct |
22 |
Correct |
96 ms |
54100 KB |
Output is correct |
23 |
Correct |
115 ms |
64464 KB |
Output is correct |
24 |
Correct |
129 ms |
65108 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
62 ms |
49348 KB |
Output is correct |
2 |
Correct |
72 ms |
56192 KB |
Output is correct |
3 |
Correct |
43 ms |
43432 KB |
Output is correct |
4 |
Correct |
41 ms |
43472 KB |
Output is correct |
5 |
Correct |
196 ms |
85052 KB |
Output is correct |
6 |
Correct |
166 ms |
83800 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Runtime error |
973 ms |
2097152 KB |
Execution killed with signal 9 |
9 |
Halted |
0 ms |
0 KB |
- |