#include <bits/stdc++.h>
#include "fish.h"
#define ll long long
#define ar array
#define sz(x) (int)x.size()
#define all(x) x.begin(), x.end()
using namespace std;
const ll INF = 1e18;
template<typename T> bool ckmin(T &a, const T &b ) { return a > b ? a = b, 1 : 0; }
template<typename T> bool ckmax(T &a, const T &b ) { return a < b ? a = b, 1 : 0; }
long long max_weights(int n, int m, std::vector<int> x, std::vector<int> y, std::vector<int> w) {
vector<vector<ar<ll, 2>>> each(n);
for (int i = 0; i < m; i++) {
each[x[i]].push_back({y[i], w[i]});
each[x[i]].push_back({y[i]-1, 0});
}
for (int i = 0; i < n; i++) each[i].push_back({n-1, 0}), each[i].push_back({-1, 0});
for (int i = 0; i < n; i++) {
sort(all(each[i]));
vector<ar<ll, 2>> nw;
ll s = 0;
for (int j = 0; j < sz(each[i]); j++) {
int r = j;
while (r+1 < sz(each[i]) && each[i][r+1][0] == each[i][r][0]) r++;
for (int k = j; k <= r; k++) s += each[i][k][1];
nw.push_back({each[i][j][0], s});
j = r;
}
assert(is_sorted(all(nw)));
swap(each[i], nw);
}
vector<vector<ll>> dp;
auto query = [&](int pos, int x) {
return prev(upper_bound(all(each[pos]), ar<ll, 2>{x, LLONG_MAX})) - each[pos].begin();
};
if (*max_element(all(x)) <= 1) {
ar<ll, 2> ans{};
for (int i = 0; i < m; i++) ans[x[i]] += w[i];
ll ans2 = max(ans[0], ans[1]);
if (n > 2) {
for (auto [x, y] : each[1]) {
ckmax(ans2, each[1].back()[1] - y + each[0][query(0, x)][1]);
}
}
return ans2;
}
vector<vector<ar<int, 3>>> val(n);
for (int i = 0; i < n; i++) {
val[i].resize(sz(each[i]));
for (int k = 0; k < sz(each[i]); k++) {
for (int j = -1; j <= 1; j++) {
if (i + j >= 0 && i + j < n) {
val[i][k][j+1] = query(i + j, each[i][k][0]);
}
}
}
}
vector<vector<ll>> dp3, dp4, dp5;
for (int i = 0; i < n; i++) {
vector<vector<ll>> dp2;
const int N = sz(each[i]);
dp2.resize(i ? sz(each[i-1]) : 1, vector<ll>(N, -INF));
if (!i) {
for (int j = 0; j < N; j++) {
dp2[0][j] = 0;
}
swap(dp, dp2);
}
else {
dp3 = dp, dp4 = dp;
for (int i = 1; i < sz(dp3); i++) for (int j = 0; j < sz(dp[i]); j++) ckmax(dp3[i][j], dp3[i-1][j]);
if (i-2 >= 0) {
for (int j = 0; j < sz(dp4); j++) for (int k = 0; k < sz(dp4[j]); k++) {
int R = val[i-2][j][2];
dp4[j][k] = dp[j][k] + (k <= R ? each[i-1][R][1] - each[i-1][k][1] : 0);
}
for (int j = sz(dp4) - 2; j >= 0; j--) for (int k = 0; k < sz(dp4[j]); k++) {
ckmax(dp4[j][k], dp4[j+1][k]);
}
}
for (int k = 0; k < sz(dp[0]); k++) {
for (int l = 0; l < N; l++) {
int ans = 0;
{
int l2 = 0, r = i-2 >= 0 ? sz(each[i-2]) - 1 : -1;
while (l2 <= r) {
int mid = (l2+r)/2;
if (val[i-2][mid][2] <= val[i][l][0]) ans = mid, l2 = mid+1;
else r = mid-1;
}
}
{
int L = k, R = val[i][l][0];
ll cost = L <= R ? each[i-1][R][1] - each[i-1][L][1] : 0;
ckmax(dp2[k][l], dp3[ans][k] + cost);
}
if (ans + 1 < sz(dp)) ckmax(dp2[k][l], dp4[ans+1][k]);
}
}
swap(dp, dp2);
}
}
ll ans = -INF;
for (int i = 0; i < sz(dp); i++) {
for (int j = 0; j < sz(dp[i]); j++) {
int L = j, R = val[n-2][i][2];
ckmax(ans, dp[i][j] + (L <= R ? each[n-1][R][1] - each[n-1][L][1] : 0));
}
}
return ans;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
46 ms |
15804 KB |
Output is correct |
2 |
Correct |
52 ms |
18096 KB |
Output is correct |
3 |
Correct |
14 ms |
7256 KB |
Output is correct |
4 |
Correct |
13 ms |
7260 KB |
Output is correct |
5 |
Correct |
236 ms |
56452 KB |
Output is correct |
6 |
Correct |
216 ms |
45904 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
87 ms |
21544 KB |
Output is correct |
3 |
Correct |
103 ms |
25036 KB |
Output is correct |
4 |
Correct |
46 ms |
15808 KB |
Output is correct |
5 |
Correct |
69 ms |
18104 KB |
Output is correct |
6 |
Correct |
0 ms |
344 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
344 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
14 ms |
7260 KB |
Output is correct |
11 |
Correct |
15 ms |
7260 KB |
Output is correct |
12 |
Correct |
45 ms |
15952 KB |
Output is correct |
13 |
Correct |
56 ms |
18096 KB |
Output is correct |
14 |
Correct |
46 ms |
14196 KB |
Output is correct |
15 |
Correct |
59 ms |
15708 KB |
Output is correct |
16 |
Correct |
47 ms |
14264 KB |
Output is correct |
17 |
Correct |
53 ms |
15544 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
13 ms |
7256 KB |
Output is correct |
2 |
Correct |
34 ms |
12968 KB |
Output is correct |
3 |
Correct |
57 ms |
16272 KB |
Output is correct |
4 |
Correct |
51 ms |
15952 KB |
Output is correct |
5 |
Correct |
83 ms |
21688 KB |
Output is correct |
6 |
Correct |
75 ms |
21080 KB |
Output is correct |
7 |
Correct |
77 ms |
21756 KB |
Output is correct |
8 |
Correct |
80 ms |
21584 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
344 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 |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
2 ms |
604 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
2 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
344 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 |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
2 ms |
604 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
2 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
4 ms |
724 KB |
Output is correct |
17 |
Correct |
233 ms |
7972 KB |
Output is correct |
18 |
Correct |
222 ms |
7640 KB |
Output is correct |
19 |
Correct |
102 ms |
4956 KB |
Output is correct |
20 |
Correct |
104 ms |
4952 KB |
Output is correct |
21 |
Correct |
105 ms |
4696 KB |
Output is correct |
22 |
Correct |
394 ms |
10432 KB |
Output is correct |
23 |
Correct |
18 ms |
1624 KB |
Output is correct |
24 |
Correct |
135 ms |
4736 KB |
Output is correct |
25 |
Correct |
2 ms |
600 KB |
Output is correct |
26 |
Correct |
15 ms |
1516 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
344 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 |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
2 ms |
604 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
2 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
4 ms |
724 KB |
Output is correct |
17 |
Correct |
233 ms |
7972 KB |
Output is correct |
18 |
Correct |
222 ms |
7640 KB |
Output is correct |
19 |
Correct |
102 ms |
4956 KB |
Output is correct |
20 |
Correct |
104 ms |
4952 KB |
Output is correct |
21 |
Correct |
105 ms |
4696 KB |
Output is correct |
22 |
Correct |
394 ms |
10432 KB |
Output is correct |
23 |
Correct |
18 ms |
1624 KB |
Output is correct |
24 |
Correct |
135 ms |
4736 KB |
Output is correct |
25 |
Correct |
2 ms |
600 KB |
Output is correct |
26 |
Correct |
15 ms |
1516 KB |
Output is correct |
27 |
Correct |
3 ms |
1112 KB |
Output is correct |
28 |
Execution timed out |
1062 ms |
39356 KB |
Time limit exceeded |
29 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
13 ms |
7256 KB |
Output is correct |
2 |
Correct |
34 ms |
12968 KB |
Output is correct |
3 |
Correct |
57 ms |
16272 KB |
Output is correct |
4 |
Correct |
51 ms |
15952 KB |
Output is correct |
5 |
Correct |
83 ms |
21688 KB |
Output is correct |
6 |
Correct |
75 ms |
21080 KB |
Output is correct |
7 |
Correct |
77 ms |
21756 KB |
Output is correct |
8 |
Correct |
80 ms |
21584 KB |
Output is correct |
9 |
Correct |
92 ms |
23124 KB |
Output is correct |
10 |
Correct |
63 ms |
13840 KB |
Output is correct |
11 |
Correct |
140 ms |
27328 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
604 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
13 ms |
7260 KB |
Output is correct |
19 |
Correct |
13 ms |
7260 KB |
Output is correct |
20 |
Correct |
48 ms |
12884 KB |
Output is correct |
21 |
Correct |
33 ms |
12892 KB |
Output is correct |
22 |
Correct |
103 ms |
24148 KB |
Output is correct |
23 |
Correct |
139 ms |
35144 KB |
Output is correct |
24 |
Correct |
148 ms |
36176 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
46 ms |
15804 KB |
Output is correct |
2 |
Correct |
52 ms |
18096 KB |
Output is correct |
3 |
Correct |
14 ms |
7256 KB |
Output is correct |
4 |
Correct |
13 ms |
7260 KB |
Output is correct |
5 |
Correct |
236 ms |
56452 KB |
Output is correct |
6 |
Correct |
216 ms |
45904 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
87 ms |
21544 KB |
Output is correct |
9 |
Correct |
103 ms |
25036 KB |
Output is correct |
10 |
Correct |
46 ms |
15808 KB |
Output is correct |
11 |
Correct |
69 ms |
18104 KB |
Output is correct |
12 |
Correct |
0 ms |
344 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
344 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
14 ms |
7260 KB |
Output is correct |
17 |
Correct |
15 ms |
7260 KB |
Output is correct |
18 |
Correct |
45 ms |
15952 KB |
Output is correct |
19 |
Correct |
56 ms |
18096 KB |
Output is correct |
20 |
Correct |
46 ms |
14196 KB |
Output is correct |
21 |
Correct |
59 ms |
15708 KB |
Output is correct |
22 |
Correct |
47 ms |
14264 KB |
Output is correct |
23 |
Correct |
53 ms |
15544 KB |
Output is correct |
24 |
Correct |
13 ms |
7256 KB |
Output is correct |
25 |
Correct |
34 ms |
12968 KB |
Output is correct |
26 |
Correct |
57 ms |
16272 KB |
Output is correct |
27 |
Correct |
51 ms |
15952 KB |
Output is correct |
28 |
Correct |
83 ms |
21688 KB |
Output is correct |
29 |
Correct |
75 ms |
21080 KB |
Output is correct |
30 |
Correct |
77 ms |
21756 KB |
Output is correct |
31 |
Correct |
80 ms |
21584 KB |
Output is correct |
32 |
Correct |
0 ms |
348 KB |
Output is correct |
33 |
Correct |
0 ms |
344 KB |
Output is correct |
34 |
Correct |
0 ms |
348 KB |
Output is correct |
35 |
Correct |
0 ms |
348 KB |
Output is correct |
36 |
Correct |
0 ms |
348 KB |
Output is correct |
37 |
Correct |
0 ms |
348 KB |
Output is correct |
38 |
Correct |
0 ms |
348 KB |
Output is correct |
39 |
Correct |
0 ms |
348 KB |
Output is correct |
40 |
Correct |
1 ms |
348 KB |
Output is correct |
41 |
Correct |
2 ms |
604 KB |
Output is correct |
42 |
Correct |
1 ms |
348 KB |
Output is correct |
43 |
Correct |
2 ms |
348 KB |
Output is correct |
44 |
Correct |
0 ms |
348 KB |
Output is correct |
45 |
Correct |
1 ms |
348 KB |
Output is correct |
46 |
Correct |
1 ms |
348 KB |
Output is correct |
47 |
Correct |
4 ms |
724 KB |
Output is correct |
48 |
Correct |
233 ms |
7972 KB |
Output is correct |
49 |
Correct |
222 ms |
7640 KB |
Output is correct |
50 |
Correct |
102 ms |
4956 KB |
Output is correct |
51 |
Correct |
104 ms |
4952 KB |
Output is correct |
52 |
Correct |
105 ms |
4696 KB |
Output is correct |
53 |
Correct |
394 ms |
10432 KB |
Output is correct |
54 |
Correct |
18 ms |
1624 KB |
Output is correct |
55 |
Correct |
135 ms |
4736 KB |
Output is correct |
56 |
Correct |
2 ms |
600 KB |
Output is correct |
57 |
Correct |
15 ms |
1516 KB |
Output is correct |
58 |
Correct |
3 ms |
1112 KB |
Output is correct |
59 |
Execution timed out |
1062 ms |
39356 KB |
Time limit exceeded |
60 |
Halted |
0 ms |
0 KB |
- |