#include <bits/stdc++.h>
#pragma GCC optimize("O3")
#pragma GCC target("avx2")
#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;
}
swap(each[i], nw);
}
vector<ar<ll, 2>> dp;
auto query = [&](int pos, int x) {
return (*prev(upper_bound(all(each[pos]), ar<ll, 2>{x, LLONG_MAX})))[1];
};
vector<vector<ll>> l(n), r(n);
for (int i = 0; i < n; i++) {
l[i].resize(sz(each[i]));
r[i].resize(sz(each[i]));
for (int k = 0; k < sz(each[i]); k++) {
if (i) l[i][k] = query(i - 1, each[i][k][0]);
if (i+1 < n) r[i][k] = query(i + 1, each[i][k][0]);
}
}
for (int i = 0; i < n; i++) {
vector<ar<ll, 2>> dp2;
const int N = sz(each[i]);
dp2.resize(N+1, {-INF, -INF});
if (!i) {
dp2[0][0] = 0, dp2[N][1] = 0;
swap(dp, dp2);
}
else {
vector<ll> dp3(sz(dp)), dp4(sz(dp));
for (int j = 0; j < sz(dp); j++) {
dp3[j] = max(dp[j][0] - (j ? each[i-1][j-1][1] : 0), j ? dp3[j-1] : -INF);
}
for (int j = sz(dp)-1; j >= 0; j--) {
dp4[j] = max(j+1 < sz(dp) ? dp4[j+1] : -INF, dp[j][1] + (j ? r[i-1][j-1] : 0));
}
for (int j = 1; j <= N; j++) {
int ans = 0;
{
int l = 0, r = sz(dp)-1;
while (l <= r) {
int mid = (l+r)/2;
if (each[i][j-1][0] >= (mid ? each[i-1][mid-1][0] : -1)) ans = mid, l = mid+1;
else r = mid-1;
}
}
ckmax(dp2[j][j==N], max(dp3[ans] + l[i][j-1], dp[1][1]));
}
ckmax(dp2[0][0], dp[1][1]);
for (int j = 1; j <= N; j++) {
if (each[i][j-1][0] <= each[i-1][sz(dp)-2][0]) {
int ans = sz(dp)-1;
{
int l = 0, r = sz(dp)-1;
while (l <= r) {
int mid = (l+r)/2;
if (each[i][j-1][0] <= each[i-1][mid-1][0]) ans = mid, r = mid-1;
else l = mid+1;
}
}
ckmax(dp2[j][1], dp4[ans] - each[i][j-1][1]);
}
}
swap(dp, dp2);
}
}
ll ans = -INF;
for (auto [x, y] : dp) ckmax(ans, x), ckmax(ans, y);
return ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
60 ms |
24028 KB |
Output is correct |
2 |
Correct |
113 ms |
26964 KB |
Output is correct |
3 |
Correct |
43 ms |
18444 KB |
Output is correct |
4 |
Correct |
32 ms |
18260 KB |
Output is correct |
5 |
Correct |
156 ms |
39312 KB |
Output is correct |
6 |
Correct |
159 ms |
47952 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
129 ms |
30024 KB |
Output is correct |
3 |
Correct |
139 ms |
37400 KB |
Output is correct |
4 |
Correct |
67 ms |
25520 KB |
Output is correct |
5 |
Correct |
80 ms |
28792 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
436 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
35 ms |
18408 KB |
Output is correct |
11 |
Correct |
33 ms |
18212 KB |
Output is correct |
12 |
Correct |
86 ms |
25428 KB |
Output is correct |
13 |
Correct |
76 ms |
28548 KB |
Output is correct |
14 |
Correct |
69 ms |
24716 KB |
Output is correct |
15 |
Correct |
105 ms |
27472 KB |
Output is correct |
16 |
Correct |
75 ms |
24840 KB |
Output is correct |
17 |
Correct |
70 ms |
27316 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
54 ms |
18428 KB |
Output is correct |
2 |
Correct |
38 ms |
18280 KB |
Output is correct |
3 |
Correct |
51 ms |
19576 KB |
Output is correct |
4 |
Correct |
49 ms |
20380 KB |
Output is correct |
5 |
Correct |
90 ms |
24032 KB |
Output is correct |
6 |
Correct |
69 ms |
23892 KB |
Output is correct |
7 |
Correct |
78 ms |
24140 KB |
Output is correct |
8 |
Correct |
98 ms |
23892 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 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 |
1 ms |
344 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
344 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
604 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
344 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 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 |
1 ms |
344 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
344 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
604 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
344 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 |
344 KB |
Output is correct |
16 |
Correct |
2 ms |
604 KB |
Output is correct |
17 |
Correct |
19 ms |
3816 KB |
Output is correct |
18 |
Correct |
18 ms |
4184 KB |
Output is correct |
19 |
Correct |
16 ms |
4188 KB |
Output is correct |
20 |
Correct |
18 ms |
4188 KB |
Output is correct |
21 |
Correct |
24 ms |
4188 KB |
Output is correct |
22 |
Correct |
35 ms |
7736 KB |
Output is correct |
23 |
Correct |
7 ms |
1372 KB |
Output is correct |
24 |
Correct |
15 ms |
3084 KB |
Output is correct |
25 |
Correct |
1 ms |
348 KB |
Output is correct |
26 |
Correct |
5 ms |
1116 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 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 |
1 ms |
344 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
344 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
604 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
344 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 |
344 KB |
Output is correct |
16 |
Correct |
2 ms |
604 KB |
Output is correct |
17 |
Correct |
19 ms |
3816 KB |
Output is correct |
18 |
Correct |
18 ms |
4184 KB |
Output is correct |
19 |
Correct |
16 ms |
4188 KB |
Output is correct |
20 |
Correct |
18 ms |
4188 KB |
Output is correct |
21 |
Correct |
24 ms |
4188 KB |
Output is correct |
22 |
Correct |
35 ms |
7736 KB |
Output is correct |
23 |
Correct |
7 ms |
1372 KB |
Output is correct |
24 |
Correct |
15 ms |
3084 KB |
Output is correct |
25 |
Correct |
1 ms |
348 KB |
Output is correct |
26 |
Correct |
5 ms |
1116 KB |
Output is correct |
27 |
Correct |
2 ms |
1112 KB |
Output is correct |
28 |
Correct |
108 ms |
17736 KB |
Output is correct |
29 |
Correct |
170 ms |
21936 KB |
Output is correct |
30 |
Correct |
199 ms |
34724 KB |
Output is correct |
31 |
Correct |
160 ms |
34644 KB |
Output is correct |
32 |
Correct |
123 ms |
30800 KB |
Output is correct |
33 |
Correct |
159 ms |
34796 KB |
Output is correct |
34 |
Correct |
166 ms |
34900 KB |
Output is correct |
35 |
Correct |
56 ms |
13392 KB |
Output is correct |
36 |
Correct |
179 ms |
31824 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
54 ms |
18428 KB |
Output is correct |
2 |
Correct |
38 ms |
18280 KB |
Output is correct |
3 |
Correct |
51 ms |
19576 KB |
Output is correct |
4 |
Correct |
49 ms |
20380 KB |
Output is correct |
5 |
Correct |
90 ms |
24032 KB |
Output is correct |
6 |
Correct |
69 ms |
23892 KB |
Output is correct |
7 |
Correct |
78 ms |
24140 KB |
Output is correct |
8 |
Correct |
98 ms |
23892 KB |
Output is correct |
9 |
Correct |
77 ms |
27216 KB |
Output is correct |
10 |
Correct |
58 ms |
14852 KB |
Output is correct |
11 |
Correct |
129 ms |
29520 KB |
Output is correct |
12 |
Correct |
1 ms |
344 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
31 ms |
18268 KB |
Output is correct |
19 |
Correct |
34 ms |
18268 KB |
Output is correct |
20 |
Correct |
33 ms |
18264 KB |
Output is correct |
21 |
Correct |
32 ms |
18360 KB |
Output is correct |
22 |
Correct |
83 ms |
27984 KB |
Output is correct |
23 |
Correct |
135 ms |
35412 KB |
Output is correct |
24 |
Correct |
129 ms |
35792 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
60 ms |
24028 KB |
Output is correct |
2 |
Correct |
113 ms |
26964 KB |
Output is correct |
3 |
Correct |
43 ms |
18444 KB |
Output is correct |
4 |
Correct |
32 ms |
18260 KB |
Output is correct |
5 |
Correct |
156 ms |
39312 KB |
Output is correct |
6 |
Correct |
159 ms |
47952 KB |
Output is correct |
7 |
Correct |
0 ms |
344 KB |
Output is correct |
8 |
Correct |
129 ms |
30024 KB |
Output is correct |
9 |
Correct |
139 ms |
37400 KB |
Output is correct |
10 |
Correct |
67 ms |
25520 KB |
Output is correct |
11 |
Correct |
80 ms |
28792 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
436 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
35 ms |
18408 KB |
Output is correct |
17 |
Correct |
33 ms |
18212 KB |
Output is correct |
18 |
Correct |
86 ms |
25428 KB |
Output is correct |
19 |
Correct |
76 ms |
28548 KB |
Output is correct |
20 |
Correct |
69 ms |
24716 KB |
Output is correct |
21 |
Correct |
105 ms |
27472 KB |
Output is correct |
22 |
Correct |
75 ms |
24840 KB |
Output is correct |
23 |
Correct |
70 ms |
27316 KB |
Output is correct |
24 |
Correct |
54 ms |
18428 KB |
Output is correct |
25 |
Correct |
38 ms |
18280 KB |
Output is correct |
26 |
Correct |
51 ms |
19576 KB |
Output is correct |
27 |
Correct |
49 ms |
20380 KB |
Output is correct |
28 |
Correct |
90 ms |
24032 KB |
Output is correct |
29 |
Correct |
69 ms |
23892 KB |
Output is correct |
30 |
Correct |
78 ms |
24140 KB |
Output is correct |
31 |
Correct |
98 ms |
23892 KB |
Output is correct |
32 |
Correct |
0 ms |
344 KB |
Output is correct |
33 |
Correct |
0 ms |
348 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 |
1 ms |
344 KB |
Output is correct |
38 |
Correct |
0 ms |
348 KB |
Output is correct |
39 |
Correct |
0 ms |
344 KB |
Output is correct |
40 |
Correct |
1 ms |
348 KB |
Output is correct |
41 |
Correct |
1 ms |
604 KB |
Output is correct |
42 |
Correct |
1 ms |
348 KB |
Output is correct |
43 |
Correct |
1 ms |
344 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 |
344 KB |
Output is correct |
47 |
Correct |
2 ms |
604 KB |
Output is correct |
48 |
Correct |
19 ms |
3816 KB |
Output is correct |
49 |
Correct |
18 ms |
4184 KB |
Output is correct |
50 |
Correct |
16 ms |
4188 KB |
Output is correct |
51 |
Correct |
18 ms |
4188 KB |
Output is correct |
52 |
Correct |
24 ms |
4188 KB |
Output is correct |
53 |
Correct |
35 ms |
7736 KB |
Output is correct |
54 |
Correct |
7 ms |
1372 KB |
Output is correct |
55 |
Correct |
15 ms |
3084 KB |
Output is correct |
56 |
Correct |
1 ms |
348 KB |
Output is correct |
57 |
Correct |
5 ms |
1116 KB |
Output is correct |
58 |
Correct |
2 ms |
1112 KB |
Output is correct |
59 |
Correct |
108 ms |
17736 KB |
Output is correct |
60 |
Correct |
170 ms |
21936 KB |
Output is correct |
61 |
Correct |
199 ms |
34724 KB |
Output is correct |
62 |
Correct |
160 ms |
34644 KB |
Output is correct |
63 |
Correct |
123 ms |
30800 KB |
Output is correct |
64 |
Correct |
159 ms |
34796 KB |
Output is correct |
65 |
Correct |
166 ms |
34900 KB |
Output is correct |
66 |
Correct |
56 ms |
13392 KB |
Output is correct |
67 |
Correct |
179 ms |
31824 KB |
Output is correct |
68 |
Correct |
77 ms |
27216 KB |
Output is correct |
69 |
Correct |
58 ms |
14852 KB |
Output is correct |
70 |
Correct |
129 ms |
29520 KB |
Output is correct |
71 |
Correct |
1 ms |
344 KB |
Output is correct |
72 |
Correct |
1 ms |
348 KB |
Output is correct |
73 |
Correct |
1 ms |
348 KB |
Output is correct |
74 |
Correct |
0 ms |
348 KB |
Output is correct |
75 |
Correct |
0 ms |
348 KB |
Output is correct |
76 |
Correct |
0 ms |
348 KB |
Output is correct |
77 |
Correct |
31 ms |
18268 KB |
Output is correct |
78 |
Correct |
34 ms |
18268 KB |
Output is correct |
79 |
Correct |
33 ms |
18264 KB |
Output is correct |
80 |
Correct |
32 ms |
18360 KB |
Output is correct |
81 |
Correct |
83 ms |
27984 KB |
Output is correct |
82 |
Correct |
135 ms |
35412 KB |
Output is correct |
83 |
Correct |
129 ms |
35792 KB |
Output is correct |
84 |
Correct |
220 ms |
48232 KB |
Output is correct |
85 |
Correct |
242 ms |
49780 KB |
Output is correct |
86 |
Correct |
188 ms |
51832 KB |
Output is correct |
87 |
Correct |
194 ms |
53976 KB |
Output is correct |
88 |
Correct |
1 ms |
348 KB |
Output is correct |
89 |
Correct |
194 ms |
53860 KB |
Output is correct |
90 |
Correct |
177 ms |
49748 KB |
Output is correct |
91 |
Correct |
164 ms |
44368 KB |
Output is correct |