#include "fish.h"
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
#define rep2(i, a, b) for (int i = (int)(a); i < (int)(b); ++i)
#define rrep(i, n) for (int i = (int)(n) - 1; i >= 0; --i)
#define rrep2(i, a, b) for (int i = (int)(b) - 1; i >= (int)(a); --i)
#define all(v) begin(v), end(v)
#define rall(v) rbegin(v), rend(v)
using namespace std;
using ll = long long;
template<class T, class U> bool chmin(T& a, const U& b) { return a > b ? a = b, true : false; }
template<class T, class U> bool chmax(T& a, const U& b) { return a < b ? a = b, true : false; }
constexpr ll inf = 1e18;
template<class M>
class SegmentTree {
using T = typename M::T;
int n;
vector<T> dat;
public:
SegmentTree(int n_, T x) {
n = 1;
while (n < n_) n *= 2;
dat.assign(2 * n, x);
}
void set(int k, T x) {
k += n;
dat[k] = x;
while (k > 1) {
k /= 2;
dat[k] = M::op(dat[2 * k], dat[2 * k + 1]);
}
}
void apply(int k, T x) { set(k, M::op(dat[k + n], x)); }
T prod(int l, int r) const {
l += n; r += n;
T lsm = M::id(), rsm = M::id();
while (l < r) {
if (l & 1) lsm = M::op(lsm, dat[l++]);
if (r & 1) rsm = M::op(dat[--r], rsm);
l >>= 1; r >>= 1;
}
return M::op(lsm, rsm);
}
T all_prod() const { return dat[1]; }
};
struct Max {
using T = ll;
static T op(T a, T b) { return max(a, b); }
static T id() { return -inf; }
};
long long max_weights(int N, int M, std::vector<int> X, std::vector<int> Y, std::vector<int> W) {
vector<vector<int>> P(N), Q(N);
rep (i, M) P[X[i]].push_back(i);
rep (i, N) sort(all(P[i]), [&](int i, int j) { return Y[i] < Y[j]; });
rep (i, N) {
Q[i] = P[i];
reverse(all(Q[i]));
}
vector<ll> Hsm(N);
rep (i, M) Hsm[X[i]] += W[i];
vector<ll> dp0(N + 1, -inf), dp1(N + 1, -inf);
dp1[0] = 0;
dp0[0] = 0;
SegmentTree<Max> seg1(N + 1, -inf), seg2(N + 1, -inf);
rep (i, N) {
// i 行目から LIS を始める
if (i != N - 1) {
seg1.apply(0, dp1[i]);
for (auto k : P[i]) {
ll t = seg1.prod(0, Y[k] + 1) + W[k];
seg1.apply(Y[k] + 1, t);
}
chmax(dp0[i + 1], seg1.all_prod());
}
// i 行目が max なので、 i+1 行目から LDS をする
if (i != N - 1) {
seg2.set(N, dp0[i]);
for (auto k : Q[i + 1]) {
ll t = seg2.prod(Y[k] + 1, N + 1) + W[k];
seg2.apply(Y[k], t);
}
chmax(dp1[i + 2], seg2.all_prod());
}
if (i + 1 < N) chmax(dp0[i + 2], dp0[i] + Hsm[i + 1]);
}
return max(*max_element(all(dp0)), *max_element(all(dp1)));
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
33 ms |
13516 KB |
Output is correct |
2 |
Correct |
46 ms |
15968 KB |
Output is correct |
3 |
Correct |
11 ms |
11352 KB |
Output is correct |
4 |
Correct |
12 ms |
11608 KB |
Output is correct |
5 |
Correct |
132 ms |
24620 KB |
Output is correct |
6 |
Correct |
135 ms |
26452 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
64 ms |
16004 KB |
Output is correct |
3 |
Correct |
78 ms |
21444 KB |
Output is correct |
4 |
Correct |
38 ms |
14804 KB |
Output is correct |
5 |
Correct |
40 ms |
16584 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
344 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
13 ms |
11608 KB |
Output is correct |
11 |
Correct |
17 ms |
11616 KB |
Output is correct |
12 |
Correct |
47 ms |
14792 KB |
Output is correct |
13 |
Correct |
49 ms |
16592 KB |
Output is correct |
14 |
Correct |
37 ms |
14736 KB |
Output is correct |
15 |
Correct |
48 ms |
15820 KB |
Output is correct |
16 |
Correct |
37 ms |
14796 KB |
Output is correct |
17 |
Correct |
40 ms |
15832 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
14 ms |
11352 KB |
Output is correct |
2 |
Correct |
13 ms |
11616 KB |
Output is correct |
3 |
Correct |
34 ms |
16220 KB |
Output is correct |
4 |
Correct |
27 ms |
15192 KB |
Output is correct |
5 |
Correct |
55 ms |
21220 KB |
Output is correct |
6 |
Correct |
60 ms |
20816 KB |
Output is correct |
7 |
Correct |
53 ms |
21072 KB |
Output is correct |
8 |
Correct |
53 ms |
21072 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
344 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 |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 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 |
344 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
344 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 |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 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 |
344 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
11 ms |
1884 KB |
Output is correct |
18 |
Correct |
10 ms |
2000 KB |
Output is correct |
19 |
Correct |
12 ms |
1880 KB |
Output is correct |
20 |
Correct |
10 ms |
1884 KB |
Output is correct |
21 |
Correct |
10 ms |
1996 KB |
Output is correct |
22 |
Correct |
22 ms |
3552 KB |
Output is correct |
23 |
Correct |
3 ms |
604 KB |
Output is correct |
24 |
Correct |
8 ms |
1372 KB |
Output is correct |
25 |
Correct |
1 ms |
348 KB |
Output is correct |
26 |
Correct |
2 ms |
604 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
344 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 |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 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 |
344 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
11 ms |
1884 KB |
Output is correct |
18 |
Correct |
10 ms |
2000 KB |
Output is correct |
19 |
Correct |
12 ms |
1880 KB |
Output is correct |
20 |
Correct |
10 ms |
1884 KB |
Output is correct |
21 |
Correct |
10 ms |
1996 KB |
Output is correct |
22 |
Correct |
22 ms |
3552 KB |
Output is correct |
23 |
Correct |
3 ms |
604 KB |
Output is correct |
24 |
Correct |
8 ms |
1372 KB |
Output is correct |
25 |
Correct |
1 ms |
348 KB |
Output is correct |
26 |
Correct |
2 ms |
604 KB |
Output is correct |
27 |
Correct |
1 ms |
860 KB |
Output is correct |
28 |
Correct |
56 ms |
7836 KB |
Output is correct |
29 |
Correct |
130 ms |
16468 KB |
Output is correct |
30 |
Correct |
94 ms |
16084 KB |
Output is correct |
31 |
Correct |
94 ms |
16208 KB |
Output is correct |
32 |
Correct |
109 ms |
16208 KB |
Output is correct |
33 |
Correct |
111 ms |
16208 KB |
Output is correct |
34 |
Correct |
94 ms |
16228 KB |
Output is correct |
35 |
Correct |
35 ms |
6484 KB |
Output is correct |
36 |
Correct |
95 ms |
16360 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
14 ms |
11352 KB |
Output is correct |
2 |
Correct |
13 ms |
11616 KB |
Output is correct |
3 |
Correct |
34 ms |
16220 KB |
Output is correct |
4 |
Correct |
27 ms |
15192 KB |
Output is correct |
5 |
Correct |
55 ms |
21220 KB |
Output is correct |
6 |
Correct |
60 ms |
20816 KB |
Output is correct |
7 |
Correct |
53 ms |
21072 KB |
Output is correct |
8 |
Correct |
53 ms |
21072 KB |
Output is correct |
9 |
Correct |
54 ms |
21172 KB |
Output is correct |
10 |
Correct |
40 ms |
13240 KB |
Output is correct |
11 |
Correct |
94 ms |
25940 KB |
Output is correct |
12 |
Correct |
0 ms |
344 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 |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
12 ms |
11356 KB |
Output is correct |
19 |
Correct |
12 ms |
11612 KB |
Output is correct |
20 |
Correct |
13 ms |
11868 KB |
Output is correct |
21 |
Correct |
12 ms |
11356 KB |
Output is correct |
22 |
Correct |
55 ms |
20228 KB |
Output is correct |
23 |
Correct |
88 ms |
25816 KB |
Output is correct |
24 |
Correct |
84 ms |
26444 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
33 ms |
13516 KB |
Output is correct |
2 |
Correct |
46 ms |
15968 KB |
Output is correct |
3 |
Correct |
11 ms |
11352 KB |
Output is correct |
4 |
Correct |
12 ms |
11608 KB |
Output is correct |
5 |
Correct |
132 ms |
24620 KB |
Output is correct |
6 |
Correct |
135 ms |
26452 KB |
Output is correct |
7 |
Correct |
0 ms |
344 KB |
Output is correct |
8 |
Correct |
64 ms |
16004 KB |
Output is correct |
9 |
Correct |
78 ms |
21444 KB |
Output is correct |
10 |
Correct |
38 ms |
14804 KB |
Output is correct |
11 |
Correct |
40 ms |
16584 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
344 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
13 ms |
11608 KB |
Output is correct |
17 |
Correct |
17 ms |
11616 KB |
Output is correct |
18 |
Correct |
47 ms |
14792 KB |
Output is correct |
19 |
Correct |
49 ms |
16592 KB |
Output is correct |
20 |
Correct |
37 ms |
14736 KB |
Output is correct |
21 |
Correct |
48 ms |
15820 KB |
Output is correct |
22 |
Correct |
37 ms |
14796 KB |
Output is correct |
23 |
Correct |
40 ms |
15832 KB |
Output is correct |
24 |
Correct |
14 ms |
11352 KB |
Output is correct |
25 |
Correct |
13 ms |
11616 KB |
Output is correct |
26 |
Correct |
34 ms |
16220 KB |
Output is correct |
27 |
Correct |
27 ms |
15192 KB |
Output is correct |
28 |
Correct |
55 ms |
21220 KB |
Output is correct |
29 |
Correct |
60 ms |
20816 KB |
Output is correct |
30 |
Correct |
53 ms |
21072 KB |
Output is correct |
31 |
Correct |
53 ms |
21072 KB |
Output is correct |
32 |
Correct |
0 ms |
344 KB |
Output is correct |
33 |
Correct |
0 ms |
344 KB |
Output is correct |
34 |
Correct |
0 ms |
344 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 |
0 ms |
348 KB |
Output is correct |
41 |
Correct |
1 ms |
348 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 |
344 KB |
Output is correct |
45 |
Correct |
0 ms |
348 KB |
Output is correct |
46 |
Correct |
0 ms |
348 KB |
Output is correct |
47 |
Correct |
1 ms |
348 KB |
Output is correct |
48 |
Correct |
11 ms |
1884 KB |
Output is correct |
49 |
Correct |
10 ms |
2000 KB |
Output is correct |
50 |
Correct |
12 ms |
1880 KB |
Output is correct |
51 |
Correct |
10 ms |
1884 KB |
Output is correct |
52 |
Correct |
10 ms |
1996 KB |
Output is correct |
53 |
Correct |
22 ms |
3552 KB |
Output is correct |
54 |
Correct |
3 ms |
604 KB |
Output is correct |
55 |
Correct |
8 ms |
1372 KB |
Output is correct |
56 |
Correct |
1 ms |
348 KB |
Output is correct |
57 |
Correct |
2 ms |
604 KB |
Output is correct |
58 |
Correct |
1 ms |
860 KB |
Output is correct |
59 |
Correct |
56 ms |
7836 KB |
Output is correct |
60 |
Correct |
130 ms |
16468 KB |
Output is correct |
61 |
Correct |
94 ms |
16084 KB |
Output is correct |
62 |
Correct |
94 ms |
16208 KB |
Output is correct |
63 |
Correct |
109 ms |
16208 KB |
Output is correct |
64 |
Correct |
111 ms |
16208 KB |
Output is correct |
65 |
Correct |
94 ms |
16228 KB |
Output is correct |
66 |
Correct |
35 ms |
6484 KB |
Output is correct |
67 |
Correct |
95 ms |
16360 KB |
Output is correct |
68 |
Correct |
54 ms |
21172 KB |
Output is correct |
69 |
Correct |
40 ms |
13240 KB |
Output is correct |
70 |
Correct |
94 ms |
25940 KB |
Output is correct |
71 |
Correct |
0 ms |
344 KB |
Output is correct |
72 |
Correct |
0 ms |
348 KB |
Output is correct |
73 |
Correct |
0 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 |
12 ms |
11356 KB |
Output is correct |
78 |
Correct |
12 ms |
11612 KB |
Output is correct |
79 |
Correct |
13 ms |
11868 KB |
Output is correct |
80 |
Correct |
12 ms |
11356 KB |
Output is correct |
81 |
Correct |
55 ms |
20228 KB |
Output is correct |
82 |
Correct |
88 ms |
25816 KB |
Output is correct |
83 |
Correct |
84 ms |
26444 KB |
Output is correct |
84 |
Correct |
128 ms |
26584 KB |
Output is correct |
85 |
Correct |
128 ms |
26708 KB |
Output is correct |
86 |
Correct |
137 ms |
30036 KB |
Output is correct |
87 |
Correct |
136 ms |
31188 KB |
Output is correct |
88 |
Correct |
1 ms |
348 KB |
Output is correct |
89 |
Correct |
150 ms |
31320 KB |
Output is correct |
90 |
Correct |
117 ms |
28248 KB |
Output is correct |
91 |
Correct |
108 ms |
27216 KB |
Output is correct |