#include "bits/stdc++.h"
using namespace std;
#define int long long
struct Fish {
int col, row;
int weight;
};
bool operator < (const Fish& a, const Fish& b) {
if (a.col != b.col) return a.col < b.col;
return a.row < b.row;
}
// sub1 - 3 {{{
// fishes are on even columns -> build piers on odd columns
// & catch all fishes
int sub1(const std::vector<Fish>& fishes) {
int res = 0;
for (const auto& fish : fishes) {
res += fish.weight;
}
return res;
}
// fishes are on first 2 columns
int sub2(int n, const std::vector<Fish>& fishes) {
std::vector<int> zeroes(n); // prefix sum of fish weights at column == 0
std::vector<int> ones(n); // prefix sum of fish weights at column == 1
for (const auto& fish : fishes) {
if (fish.col == 0) zeroes[fish.row] += fish.weight;
if (fish.col == 1) ones[fish.row] += fish.weight;
}
std::partial_sum(zeroes.begin(), zeroes.end(), zeroes.begin());
std::partial_sum(ones.begin(), ones.end(), ones.begin());
int res = ones.back(); // init: only catch fishes at column == 1
for (int i = 0; i < n; ++i) {
// build pier until at column 1, row 0-i
if (n == 2) res = std::max(res, zeroes[i]);
else res = std::max(res, zeroes[i] + ones.back() - ones[i]);
}
return res;
}
// all fishes are on row == 0
int sub3(int n, const std::vector<Fish>& fishes) {
std::vector<int> weights(n); // weights[i] = weight of fish at column i
for (const auto& fish : fishes) {
weights[fish.col] += fish.weight;
}
// f[i] = best strategy if we BUILD PIER AT i, only considering col 0..i
// i-4 i-3 i-2 i-1 i
std::vector<int> f(n);
f[0] = 0;
for (int i = 1; i < n; ++i) {
f[i] = std::max(f[i-1], weights[i-1]);
if (i >= 2) {
f[i] = std::max(f[i], f[i-2] + weights[i-1]);
}
if (i >= 3) {
f[i] = std::max(f[i], f[i-3] + weights[i-2] + weights[i-1]);
}
}
int res = 0;
for (int i = 0; i < n; ++i) {
int cur = f[i];
if (i + 1 < n) cur += weights[i+1];
res = std::max(res, cur);
}
return res;
}
// }}}
// N <= 300
int sub5(int n, const std::vector<Fish>& fishes) {
// Init weights[i][j] = sum of fish on column i, from row 0 -> row j
std::vector<std::vector<int>> weights(n, std::vector<int> (n, 0));
for (const auto& fish : fishes) {
weights[fish.col][fish.row] += fish.weight;
}
for (int col = 0; col < n; ++col) {
std::partial_sum(weights[col].begin(), weights[col].end(), weights[col].begin());
}
// f[c][r] = best strategy if we last BUILD PIER AT column c, row r
// only considering fishes <= (c, r)
// g[c][r] = similar to f[c][r] but consider fishes at column c, in row [r, n-1]
std::vector<std::vector<int>> f(n, std::vector<int> (n, 0)),
g(n, std::vector<int> (n, 0));
// f <= g
for (int c = 1; c < n; ++c) {
for (int r = 0; r < n; ++r) {
// this is first pier
f[c][r] = g[c][r] = weights[c-1][r];
// last pier at column i-1
for (int lastRow = 0; lastRow < n; ++lastRow) {
if (lastRow <= r) {
int cur = std::max(
f[c-1][lastRow] + weights[c-1][r] - weights[c-1][lastRow],
g[c-1][lastRow]);
f[c][r] = std::max(f[c][r], cur);
g[c][r] = std::max(g[c][r], cur);
} else {
f[c][r] = std::max(f[c][r], g[c-1][lastRow]);
g[c][r] = std::max(
g[c][r],
g[c-1][lastRow] + weights[c][lastRow] - weights[c][r]);
}
}
// last pier at column i-2
if (c >= 2) {
for (int lastRow = 0; lastRow < n; ++lastRow) {
int cur = g[c-2][lastRow] + weights[c-1][std::max(lastRow, r)];
f[c][r] = std::max(f[c][r], cur);
g[c][r] = std::max(g[c][r], cur);
}
}
// last pier at column i-3
if (c >= 3) {
for (int lastRow = 0; lastRow < n; ++lastRow) {
int cur = g[c-3][lastRow] + weights[c-2][lastRow] + weights[c-1][r];
f[c][r] = std::max(f[c][r], cur);
g[c][r] = std::max(g[c][r], cur);
}
}
}
}
int res = 0;
for (int c = 0; c < n; ++c) {
for (int r = 0; r < n; ++r) {
assert(g[c][r] >= f[c][r]);
int cur = g[c][r];
if (c + 1 < n) {
cur += weights[c+1][r];
}
res = std::max(res, cur);
}
}
return res;
}
// N <= 3000
int sub6(int n, const std::vector<Fish>& fishes) {
// Init weights[i][j] = sum of fish on column i, from row 0 -> row j
std::vector<std::vector<int>> weights(n, std::vector<int> (n, 0));
for (const auto& fish : fishes) {
weights[fish.col][fish.row] += fish.weight;
}
for (int col = 0; col < n; ++col) {
std::partial_sum(weights[col].begin(), weights[col].end(), weights[col].begin());
}
// f[c][r] = best strategy if we last BUILD PIER AT column c, row r
// only considering fishes <= (c, r)
// g[c][r] = similar to f[c][r] but consider fishes at column c, in row [r, n-1]
std::vector<std::vector<int>> f(n, std::vector<int> (n, 0)),
g(n, std::vector<int> (n, 0)),
g_agg(n, std::vector<int> (n, 0)),
g_agg_with_next_col(n, std::vector<int> (n, 0));
// f <= g
for (int c = 0; c < n; ++c) {
// compute {{{
if (c > 0) {
for (int r = 0; r < n; ++r) {
// this is first pier
f[c][r] = g[c][r] = weights[c-1][r];
// last pier at column i-1
for (int lastRow = 0; lastRow < n; ++lastRow) {
if (lastRow <= r) {
int cur = std::max(
f[c-1][lastRow] + weights[c-1][r] - weights[c-1][lastRow],
g[c-1][lastRow]);
f[c][r] = std::max(f[c][r], cur);
g[c][r] = std::max(g[c][r], cur);
} else {
f[c][r] = std::max(f[c][r], g[c-1][lastRow]);
g[c][r] = std::max(
g[c][r],
g[c-1][lastRow] + weights[c][lastRow] - weights[c][r]);
}
}
// last pier at column i-2
if (c >= 2) {
int cur = std::max(
g_agg[c-2].back() + weights[c-1][r],
g_agg_with_next_col[c-2].back());
f[c][r] = std::max(f[c][r], cur);
g[c][r] = std::max(g[c][r], cur);
}
// last pier at column i-3
if (c >= 3) {
int cur = g_agg_with_next_col[c-3].back() + weights[c-1][r];
f[c][r] = std::max(f[c][r], cur);
g[c][r] = std::max(g[c][r], cur);
}
}
}
// }}}
// aggregate {{{
// g_agg[c][r] = max(g[c][0], .., g[c][r])
std::partial_sum(g[c].begin(), g[c].end(), g_agg[c].begin(),
[] (auto a, auto b) { return std::max(a, b); });
if (c + 1 < n) {
// g_agg_with_next_col[c][r] = max(
// g[c][0] + weights[c+1][0],
// ...
// g[c][r] + weights[c+1][r])
g_agg_with_next_col[c][0] = g[c][0] + weights[c+1][0];
for (int r = 1; r < n; ++r) {
g_agg_with_next_col[c][r] = std::max(
g_agg_with_next_col[c][r-1],
g[c][r] + weights[c+1][r]);
}
}
// }}}
}
int res = 0;
for (int c = 0; c < n; ++c) {
for (int r = 0; r < n; ++r) {
assert(g[c][r] >= f[c][r]);
int cur = g[c][r];
if (c + 1 < n) {
cur += weights[c+1][r];
}
res = std::max(res, cur);
}
}
return res;
}
#undef int
long long max_weights(
int n, int nFish,
std::vector<int> xs,
std::vector<int> ys,
std::vector<int> ws) {
std::vector<Fish> fishes;
for (int i = 0; i < nFish; ++i) {
fishes.push_back({xs[i], ys[i], ws[i]});
}
if (std::all_of(xs.begin(), xs.end(), [] (int x) { return x % 2 == 0; })) {
return sub1(fishes);
}
if (*std::max_element(xs.begin(), xs.end()) <= 1) {
return sub2(n, fishes);
}
if (*std::max_element(ys.begin(), ys.end()) == 0) {
return sub3(n, fishes);
}
if (n <= 3000) {
return sub6(n, fishes);
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
35 ms |
5328 KB |
Output is correct |
2 |
Correct |
48 ms |
5816 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
256 KB |
Output is correct |
5 |
Correct |
123 ms |
19768 KB |
Output is correct |
6 |
Correct |
135 ms |
19792 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
62 ms |
10392 KB |
Output is correct |
3 |
Correct |
92 ms |
11220 KB |
Output is correct |
4 |
Correct |
34 ms |
5320 KB |
Output is correct |
5 |
Correct |
35 ms |
5816 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
268 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
37 ms |
5456 KB |
Output is correct |
13 |
Correct |
43 ms |
6496 KB |
Output is correct |
14 |
Correct |
31 ms |
5448 KB |
Output is correct |
15 |
Correct |
38 ms |
6084 KB |
Output is correct |
16 |
Correct |
31 ms |
5468 KB |
Output is correct |
17 |
Correct |
34 ms |
6080 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
3 ms |
1876 KB |
Output is correct |
3 |
Correct |
24 ms |
4156 KB |
Output is correct |
4 |
Correct |
19 ms |
3656 KB |
Output is correct |
5 |
Correct |
72 ms |
6444 KB |
Output is correct |
6 |
Correct |
28 ms |
6508 KB |
Output is correct |
7 |
Correct |
42 ms |
6460 KB |
Output is correct |
8 |
Correct |
33 ms |
6448 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
256 KB |
Output is correct |
5 |
Correct |
1 ms |
256 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
14 ms |
1236 KB |
Output is correct |
10 |
Correct |
104 ms |
3980 KB |
Output is correct |
11 |
Correct |
14 ms |
1236 KB |
Output is correct |
12 |
Correct |
110 ms |
3924 KB |
Output is correct |
13 |
Correct |
3 ms |
468 KB |
Output is correct |
14 |
Correct |
104 ms |
3912 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
256 KB |
Output is correct |
5 |
Correct |
1 ms |
256 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
14 ms |
1236 KB |
Output is correct |
10 |
Correct |
104 ms |
3980 KB |
Output is correct |
11 |
Correct |
14 ms |
1236 KB |
Output is correct |
12 |
Correct |
110 ms |
3924 KB |
Output is correct |
13 |
Correct |
3 ms |
468 KB |
Output is correct |
14 |
Correct |
104 ms |
3912 KB |
Output is correct |
15 |
Correct |
105 ms |
3796 KB |
Output is correct |
16 |
Correct |
3 ms |
596 KB |
Output is correct |
17 |
Correct |
126 ms |
5952 KB |
Output is correct |
18 |
Correct |
118 ms |
5996 KB |
Output is correct |
19 |
Correct |
128 ms |
5996 KB |
Output is correct |
20 |
Correct |
113 ms |
5996 KB |
Output is correct |
21 |
Correct |
116 ms |
5944 KB |
Output is correct |
22 |
Correct |
136 ms |
8120 KB |
Output is correct |
23 |
Correct |
114 ms |
4304 KB |
Output is correct |
24 |
Correct |
116 ms |
5276 KB |
Output is correct |
25 |
Correct |
97 ms |
3924 KB |
Output is correct |
26 |
Correct |
102 ms |
4180 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
256 KB |
Output is correct |
5 |
Correct |
1 ms |
256 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
14 ms |
1236 KB |
Output is correct |
10 |
Correct |
104 ms |
3980 KB |
Output is correct |
11 |
Correct |
14 ms |
1236 KB |
Output is correct |
12 |
Correct |
110 ms |
3924 KB |
Output is correct |
13 |
Correct |
3 ms |
468 KB |
Output is correct |
14 |
Correct |
104 ms |
3912 KB |
Output is correct |
15 |
Correct |
105 ms |
3796 KB |
Output is correct |
16 |
Correct |
3 ms |
596 KB |
Output is correct |
17 |
Correct |
126 ms |
5952 KB |
Output is correct |
18 |
Correct |
118 ms |
5996 KB |
Output is correct |
19 |
Correct |
128 ms |
5996 KB |
Output is correct |
20 |
Correct |
113 ms |
5996 KB |
Output is correct |
21 |
Correct |
116 ms |
5944 KB |
Output is correct |
22 |
Correct |
136 ms |
8120 KB |
Output is correct |
23 |
Correct |
114 ms |
4304 KB |
Output is correct |
24 |
Correct |
116 ms |
5276 KB |
Output is correct |
25 |
Correct |
97 ms |
3924 KB |
Output is correct |
26 |
Correct |
102 ms |
4180 KB |
Output is correct |
27 |
Execution timed out |
1105 ms |
353228 KB |
Time limit exceeded |
28 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
3 ms |
1876 KB |
Output is correct |
3 |
Correct |
24 ms |
4156 KB |
Output is correct |
4 |
Correct |
19 ms |
3656 KB |
Output is correct |
5 |
Correct |
72 ms |
6444 KB |
Output is correct |
6 |
Correct |
28 ms |
6508 KB |
Output is correct |
7 |
Correct |
42 ms |
6460 KB |
Output is correct |
8 |
Correct |
33 ms |
6448 KB |
Output is correct |
9 |
Incorrect |
29 ms |
5832 KB |
1st lines differ - on the 1st token, expected: '99999', found: '0' |
10 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
35 ms |
5328 KB |
Output is correct |
2 |
Correct |
48 ms |
5816 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
256 KB |
Output is correct |
5 |
Correct |
123 ms |
19768 KB |
Output is correct |
6 |
Correct |
135 ms |
19792 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
62 ms |
10392 KB |
Output is correct |
9 |
Correct |
92 ms |
11220 KB |
Output is correct |
10 |
Correct |
34 ms |
5320 KB |
Output is correct |
11 |
Correct |
35 ms |
5816 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
268 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
1 ms |
212 KB |
Output is correct |
18 |
Correct |
37 ms |
5456 KB |
Output is correct |
19 |
Correct |
43 ms |
6496 KB |
Output is correct |
20 |
Correct |
31 ms |
5448 KB |
Output is correct |
21 |
Correct |
38 ms |
6084 KB |
Output is correct |
22 |
Correct |
31 ms |
5468 KB |
Output is correct |
23 |
Correct |
34 ms |
6080 KB |
Output is correct |
24 |
Correct |
1 ms |
212 KB |
Output is correct |
25 |
Correct |
3 ms |
1876 KB |
Output is correct |
26 |
Correct |
24 ms |
4156 KB |
Output is correct |
27 |
Correct |
19 ms |
3656 KB |
Output is correct |
28 |
Correct |
72 ms |
6444 KB |
Output is correct |
29 |
Correct |
28 ms |
6508 KB |
Output is correct |
30 |
Correct |
42 ms |
6460 KB |
Output is correct |
31 |
Correct |
33 ms |
6448 KB |
Output is correct |
32 |
Correct |
1 ms |
212 KB |
Output is correct |
33 |
Correct |
1 ms |
212 KB |
Output is correct |
34 |
Correct |
1 ms |
212 KB |
Output is correct |
35 |
Correct |
1 ms |
256 KB |
Output is correct |
36 |
Correct |
1 ms |
256 KB |
Output is correct |
37 |
Correct |
0 ms |
212 KB |
Output is correct |
38 |
Correct |
0 ms |
212 KB |
Output is correct |
39 |
Correct |
0 ms |
212 KB |
Output is correct |
40 |
Correct |
14 ms |
1236 KB |
Output is correct |
41 |
Correct |
104 ms |
3980 KB |
Output is correct |
42 |
Correct |
14 ms |
1236 KB |
Output is correct |
43 |
Correct |
110 ms |
3924 KB |
Output is correct |
44 |
Correct |
3 ms |
468 KB |
Output is correct |
45 |
Correct |
104 ms |
3912 KB |
Output is correct |
46 |
Correct |
105 ms |
3796 KB |
Output is correct |
47 |
Correct |
3 ms |
596 KB |
Output is correct |
48 |
Correct |
126 ms |
5952 KB |
Output is correct |
49 |
Correct |
118 ms |
5996 KB |
Output is correct |
50 |
Correct |
128 ms |
5996 KB |
Output is correct |
51 |
Correct |
113 ms |
5996 KB |
Output is correct |
52 |
Correct |
116 ms |
5944 KB |
Output is correct |
53 |
Correct |
136 ms |
8120 KB |
Output is correct |
54 |
Correct |
114 ms |
4304 KB |
Output is correct |
55 |
Correct |
116 ms |
5276 KB |
Output is correct |
56 |
Correct |
97 ms |
3924 KB |
Output is correct |
57 |
Correct |
102 ms |
4180 KB |
Output is correct |
58 |
Execution timed out |
1105 ms |
353228 KB |
Time limit exceeded |
59 |
Halted |
0 ms |
0 KB |
- |