#include <iostream>
#include <string>
#include <vector>
#include <queue>
#include <stack>
#include <array>
#include <algorithm>
#include <numeric>
#include <map>
#include <unordered_map>
#include <set>
#include <cstring>
#include <cmath>
#include <iomanip>
#include <cassert>
#include <random>
#include <cstdlib>
#define debug(x) std::cout << #x << " " << (x) << '\n';
#define pb push_back
#define mp std::make_pair
#define remax(a, b) a = std::max((a), (b));
#define remin(a, b) a = std::min((a), (b));
#define maxWeights max_weights
const int32_t MAX_N = 1e5;
struct Catfish {
int32_t row;
int32_t id;
mutable int64_t prefSum;
Catfish(int32_t _row, int32_t _id, int32_t w): row(_row), id(_id), prefSum((int64_t) w) {}
bool operator< (const Catfish &other) const {
if(row != other.row) {
return row <= other.row;
}
else {
return id <= other.id;
}
}
};
struct DpState {
int32_t row;
mutable int64_t incrVal, decrVal;
DpState(int32_t _row): row(_row), incrVal(0), decrVal(0) {}
bool operator< (const DpState &other) const {
return row <= other.row;
}
};
int64_t maxWeights(int32_t n, int32_t m, std::vector<int32_t> x, std::vector<int32_t> y,
std::vector<int32_t> w) {
std::vector<std::vector<Catfish>> catfishByColumn(n);
for(int32_t i = 0; i < m; i++) {
catfishByColumn[x[i]].push_back(Catfish(y[i], i, w[i]));
}
for(int32_t i = 0; i < n; i++) {
std::sort(catfishByColumn[i].begin(), catfishByColumn[i].end());
int64_t total = 0;
for(auto &x : catfishByColumn[i]) {
total += (int64_t) x.prefSum;
x.prefSum = total;
}
}
std::vector<std::vector<DpState>> dp(n + 1);
for(int32_t i = 0; i <= n; i++) {
if(i > 0 && i != n) {
for(auto &x : catfishByColumn[i - 1]) {
dp[i].push_back(DpState(x.row + 1));
}
}
if(i < n - 1) {
for(auto &x : catfishByColumn[i + 1]) {
dp[i].push_back(DpState(x.row + 1));
}
}
dp[i].push_back(DpState(0));
std::sort(dp[i].begin(), dp[i].end());
}
auto getWPrefSum = [&](int32_t column, int32_t row) -> int64_t {
if(column >= n) {
return 0;
}
auto p = std::lower_bound(catfishByColumn[column].begin(), catfishByColumn[column].end(), Catfish(row, -1, 0));
if(p == catfishByColumn[column].begin()) {
return 0;
}
p--;
return p->prefSum;
};
for(int32_t i = 1; i <= n; i++) {
{ // compute incrVal
auto prevIt = dp[i - 1].begin();
int64_t pref = 0;
for(auto it = dp[i].begin(); it != dp[i].end(); it++) {
while(prevIt != dp[i - 1].end() && prevIt->row <= it->row) {
remax(pref, prevIt->incrVal - getWPrefSum(i - 1, prevIt->row));
prevIt++;
}
it->incrVal = std::max(pref + getWPrefSum(i - 1, it->row), dp[i - 1].begin()->decrVal);
}
}
{ // compute decrVal
auto prevIt = dp[i - 1].rbegin();
int64_t suff = 0;
for(auto it = dp[i].rbegin(); it != dp[i].rend(); it++) {
while(prevIt != dp[i - 1].rend() && prevIt->row >= it->row) {
remax(suff, std::max(prevIt->incrVal, prevIt->decrVal) + getWPrefSum(i, prevIt->row));
prevIt++;
}
it->decrVal = suff - getWPrefSum(i, it->row);
}
}
}
return std::max(dp.back().begin()->incrVal, dp.back().begin()->decrVal);
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
57 ms |
12356 KB |
Output is correct |
2 |
Correct |
67 ms |
14316 KB |
Output is correct |
3 |
Correct |
13 ms |
8132 KB |
Output is correct |
4 |
Correct |
12 ms |
8104 KB |
Output is correct |
5 |
Correct |
249 ms |
36236 KB |
Output is correct |
6 |
Correct |
182 ms |
42316 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
128 ms |
19388 KB |
Output is correct |
3 |
Correct |
185 ms |
22956 KB |
Output is correct |
4 |
Correct |
55 ms |
12364 KB |
Output is correct |
5 |
Correct |
70 ms |
14376 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 |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
14 ms |
8068 KB |
Output is correct |
11 |
Correct |
11 ms |
8020 KB |
Output is correct |
12 |
Correct |
71 ms |
14304 KB |
Output is correct |
13 |
Correct |
86 ms |
16688 KB |
Output is correct |
14 |
Correct |
62 ms |
13408 KB |
Output is correct |
15 |
Correct |
78 ms |
14852 KB |
Output is correct |
16 |
Correct |
64 ms |
13352 KB |
Output is correct |
17 |
Correct |
77 ms |
14764 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
8020 KB |
Output is correct |
2 |
Correct |
11 ms |
8020 KB |
Output is correct |
3 |
Correct |
43 ms |
14400 KB |
Output is correct |
4 |
Correct |
32 ms |
12744 KB |
Output is correct |
5 |
Correct |
72 ms |
21436 KB |
Output is correct |
6 |
Correct |
63 ms |
21316 KB |
Output is correct |
7 |
Correct |
83 ms |
21320 KB |
Output is correct |
8 |
Correct |
77 ms |
21344 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 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 |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
596 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
2 ms |
468 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 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 |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
596 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
2 ms |
468 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
2 ms |
468 KB |
Output is correct |
17 |
Correct |
35 ms |
4560 KB |
Output is correct |
18 |
Correct |
40 ms |
5716 KB |
Output is correct |
19 |
Correct |
29 ms |
6100 KB |
Output is correct |
20 |
Correct |
28 ms |
5972 KB |
Output is correct |
21 |
Correct |
28 ms |
6076 KB |
Output is correct |
22 |
Correct |
67 ms |
10528 KB |
Output is correct |
23 |
Correct |
6 ms |
1364 KB |
Output is correct |
24 |
Correct |
21 ms |
3412 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
7 ms |
1236 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 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 |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
596 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
2 ms |
468 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
2 ms |
468 KB |
Output is correct |
17 |
Correct |
35 ms |
4560 KB |
Output is correct |
18 |
Correct |
40 ms |
5716 KB |
Output is correct |
19 |
Correct |
29 ms |
6100 KB |
Output is correct |
20 |
Correct |
28 ms |
5972 KB |
Output is correct |
21 |
Correct |
28 ms |
6076 KB |
Output is correct |
22 |
Correct |
67 ms |
10528 KB |
Output is correct |
23 |
Correct |
6 ms |
1364 KB |
Output is correct |
24 |
Correct |
21 ms |
3412 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
7 ms |
1236 KB |
Output is correct |
27 |
Correct |
3 ms |
852 KB |
Output is correct |
28 |
Correct |
172 ms |
22680 KB |
Output is correct |
29 |
Correct |
239 ms |
30436 KB |
Output is correct |
30 |
Correct |
209 ms |
32236 KB |
Output is correct |
31 |
Correct |
202 ms |
32128 KB |
Output is correct |
32 |
Correct |
229 ms |
32372 KB |
Output is correct |
33 |
Correct |
200 ms |
32248 KB |
Output is correct |
34 |
Correct |
199 ms |
32204 KB |
Output is correct |
35 |
Correct |
74 ms |
14596 KB |
Output is correct |
36 |
Correct |
222 ms |
33248 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
11 ms |
8020 KB |
Output is correct |
2 |
Correct |
11 ms |
8020 KB |
Output is correct |
3 |
Correct |
43 ms |
14400 KB |
Output is correct |
4 |
Correct |
32 ms |
12744 KB |
Output is correct |
5 |
Correct |
72 ms |
21436 KB |
Output is correct |
6 |
Correct |
63 ms |
21316 KB |
Output is correct |
7 |
Correct |
83 ms |
21320 KB |
Output is correct |
8 |
Correct |
77 ms |
21344 KB |
Output is correct |
9 |
Correct |
72 ms |
21432 KB |
Output is correct |
10 |
Correct |
62 ms |
17728 KB |
Output is correct |
11 |
Correct |
135 ms |
35284 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
0 ms |
212 KB |
Output is correct |
18 |
Correct |
12 ms |
8020 KB |
Output is correct |
19 |
Correct |
12 ms |
8084 KB |
Output is correct |
20 |
Correct |
11 ms |
8020 KB |
Output is correct |
21 |
Correct |
11 ms |
8020 KB |
Output is correct |
22 |
Correct |
84 ms |
20044 KB |
Output is correct |
23 |
Correct |
150 ms |
35196 KB |
Output is correct |
24 |
Correct |
137 ms |
35272 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
57 ms |
12356 KB |
Output is correct |
2 |
Correct |
67 ms |
14316 KB |
Output is correct |
3 |
Correct |
13 ms |
8132 KB |
Output is correct |
4 |
Correct |
12 ms |
8104 KB |
Output is correct |
5 |
Correct |
249 ms |
36236 KB |
Output is correct |
6 |
Correct |
182 ms |
42316 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
128 ms |
19388 KB |
Output is correct |
9 |
Correct |
185 ms |
22956 KB |
Output is correct |
10 |
Correct |
55 ms |
12364 KB |
Output is correct |
11 |
Correct |
70 ms |
14376 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
14 ms |
8068 KB |
Output is correct |
17 |
Correct |
11 ms |
8020 KB |
Output is correct |
18 |
Correct |
71 ms |
14304 KB |
Output is correct |
19 |
Correct |
86 ms |
16688 KB |
Output is correct |
20 |
Correct |
62 ms |
13408 KB |
Output is correct |
21 |
Correct |
78 ms |
14852 KB |
Output is correct |
22 |
Correct |
64 ms |
13352 KB |
Output is correct |
23 |
Correct |
77 ms |
14764 KB |
Output is correct |
24 |
Correct |
11 ms |
8020 KB |
Output is correct |
25 |
Correct |
11 ms |
8020 KB |
Output is correct |
26 |
Correct |
43 ms |
14400 KB |
Output is correct |
27 |
Correct |
32 ms |
12744 KB |
Output is correct |
28 |
Correct |
72 ms |
21436 KB |
Output is correct |
29 |
Correct |
63 ms |
21316 KB |
Output is correct |
30 |
Correct |
83 ms |
21320 KB |
Output is correct |
31 |
Correct |
77 ms |
21344 KB |
Output is correct |
32 |
Correct |
0 ms |
212 KB |
Output is correct |
33 |
Correct |
0 ms |
212 KB |
Output is correct |
34 |
Correct |
0 ms |
212 KB |
Output is correct |
35 |
Correct |
0 ms |
212 KB |
Output is correct |
36 |
Correct |
0 ms |
212 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 |
1 ms |
340 KB |
Output is correct |
41 |
Correct |
1 ms |
596 KB |
Output is correct |
42 |
Correct |
1 ms |
340 KB |
Output is correct |
43 |
Correct |
2 ms |
468 KB |
Output is correct |
44 |
Correct |
1 ms |
212 KB |
Output is correct |
45 |
Correct |
1 ms |
340 KB |
Output is correct |
46 |
Correct |
1 ms |
340 KB |
Output is correct |
47 |
Correct |
2 ms |
468 KB |
Output is correct |
48 |
Correct |
35 ms |
4560 KB |
Output is correct |
49 |
Correct |
40 ms |
5716 KB |
Output is correct |
50 |
Correct |
29 ms |
6100 KB |
Output is correct |
51 |
Correct |
28 ms |
5972 KB |
Output is correct |
52 |
Correct |
28 ms |
6076 KB |
Output is correct |
53 |
Correct |
67 ms |
10528 KB |
Output is correct |
54 |
Correct |
6 ms |
1364 KB |
Output is correct |
55 |
Correct |
21 ms |
3412 KB |
Output is correct |
56 |
Correct |
1 ms |
340 KB |
Output is correct |
57 |
Correct |
7 ms |
1236 KB |
Output is correct |
58 |
Correct |
3 ms |
852 KB |
Output is correct |
59 |
Correct |
172 ms |
22680 KB |
Output is correct |
60 |
Correct |
239 ms |
30436 KB |
Output is correct |
61 |
Correct |
209 ms |
32236 KB |
Output is correct |
62 |
Correct |
202 ms |
32128 KB |
Output is correct |
63 |
Correct |
229 ms |
32372 KB |
Output is correct |
64 |
Correct |
200 ms |
32248 KB |
Output is correct |
65 |
Correct |
199 ms |
32204 KB |
Output is correct |
66 |
Correct |
74 ms |
14596 KB |
Output is correct |
67 |
Correct |
222 ms |
33248 KB |
Output is correct |
68 |
Correct |
72 ms |
21432 KB |
Output is correct |
69 |
Correct |
62 ms |
17728 KB |
Output is correct |
70 |
Correct |
135 ms |
35284 KB |
Output is correct |
71 |
Correct |
0 ms |
212 KB |
Output is correct |
72 |
Correct |
1 ms |
212 KB |
Output is correct |
73 |
Correct |
1 ms |
212 KB |
Output is correct |
74 |
Correct |
1 ms |
212 KB |
Output is correct |
75 |
Correct |
1 ms |
212 KB |
Output is correct |
76 |
Correct |
0 ms |
212 KB |
Output is correct |
77 |
Correct |
12 ms |
8020 KB |
Output is correct |
78 |
Correct |
12 ms |
8084 KB |
Output is correct |
79 |
Correct |
11 ms |
8020 KB |
Output is correct |
80 |
Correct |
11 ms |
8020 KB |
Output is correct |
81 |
Correct |
84 ms |
20044 KB |
Output is correct |
82 |
Correct |
150 ms |
35196 KB |
Output is correct |
83 |
Correct |
137 ms |
35272 KB |
Output is correct |
84 |
Correct |
290 ms |
34884 KB |
Output is correct |
85 |
Correct |
269 ms |
35096 KB |
Output is correct |
86 |
Correct |
214 ms |
42384 KB |
Output is correct |
87 |
Correct |
217 ms |
43364 KB |
Output is correct |
88 |
Correct |
0 ms |
212 KB |
Output is correct |
89 |
Correct |
216 ms |
43412 KB |
Output is correct |
90 |
Correct |
210 ms |
42596 KB |
Output is correct |
91 |
Correct |
182 ms |
42384 KB |
Output is correct |