#include "fish.h"
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int MAXN = 1e5;
const ll INF = 1e18;
int N, M;
vector<pii> V[MAXN+10];
vector<ll> dp1[MAXN+10], dp2[MAXN+10];
struct BIT
{
ll tree[MAXN+10];
void init() { for(int i=1; i<=N; i++) tree[i]=-INF; }
void update(int i, ll k) { for(; i<=N; i+=(i&-i)) tree[i]=max(tree[i], k); }
ll query(int i) { ll ret=-INF; for(; i>0; i-=(i&-i)) ret=max(ret, tree[i]); return ret; }
}bit1, bit2;
ll max_weights(int _N, int _M, vector<int> _X, vector<int> _Y, vector<int> _W)
{
N=_N; M=_M;
for(int i=0; i<M; i++)
{
int x=_X[i]+1, y=_Y[i]+1, w=_W[i];
V[x].push_back({y, w});
}
for(int i=0; i<=N+1; i++)
{
sort(V[i].begin(), V[i].end());
if(V[i].empty() || V[i][0].first!=1) V[i].push_back({1, 0});
if(V[i].back().first!=N) V[i].push_back({N, 0});
sort(V[i].begin(), V[i].end());
dp1[i]=vector<ll>(V[i].size(), -INF);
dp2[i]=vector<ll>(V[i].size(), -INF);
}
bit1.init(); bit2.init();
dp2[0][0]=0; dp1[0][dp1[0].size()-1]=0;
ll p=-INF, q=-INF, ans=0;
for(int i=0; i<=N+1; i++)
{
int t=V[i].size();
if(i-1>=0) p=max(p, dp2[i-1][0]);
if(i-2>=0) q=max(q, dp1[i-2].back());
dp1[i][0]=max(dp1[i][0], p+V[i][0].second);
if(i-2>=0) dp1[i][0]=max(dp1[i][0], dp1[i-2].back()+V[i][0].second);
dp2[i][t-1]=max(dp2[i][t-1], q+V[i][t-1].second);
for(int j=0; j<t; j++)
{
auto [y, w] = V[i][j];
dp1[i][j]=max(dp1[i][j], bit1.query(y-1)+w);
bit1.update(y, dp1[i][j]);
}
for(int j=t-1; j>=0; j--)
{
auto [y, w] = V[i][j];
dp2[i][j]=max(dp2[i][j], bit2.query(N-y)+w);
bit2.update(N-y+1, dp2[i][j]);
}
ans=max(ans, dp2[i][0]);
// for(auto it : dp1[i]) printf("%lld ", it); printf("\n");
// for(auto it : dp2[i]) printf("%lld ", it); printf("\n");
// printf("\n");
}
return ans;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
55 ms |
20928 KB |
Output is correct |
2 |
Correct |
62 ms |
22888 KB |
Output is correct |
3 |
Correct |
28 ms |
18260 KB |
Output is correct |
4 |
Correct |
28 ms |
18260 KB |
Output is correct |
5 |
Correct |
150 ms |
32412 KB |
Output is correct |
6 |
Correct |
152 ms |
33224 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
7252 KB |
Output is correct |
2 |
Correct |
83 ms |
24764 KB |
Output is correct |
3 |
Correct |
121 ms |
27736 KB |
Output is correct |
4 |
Correct |
53 ms |
21012 KB |
Output is correct |
5 |
Correct |
62 ms |
22988 KB |
Output is correct |
6 |
Correct |
3 ms |
7252 KB |
Output is correct |
7 |
Correct |
3 ms |
7356 KB |
Output is correct |
8 |
Correct |
3 ms |
7356 KB |
Output is correct |
9 |
Correct |
3 ms |
7252 KB |
Output is correct |
10 |
Correct |
27 ms |
18264 KB |
Output is correct |
11 |
Correct |
28 ms |
18244 KB |
Output is correct |
12 |
Correct |
55 ms |
22376 KB |
Output is correct |
13 |
Correct |
64 ms |
24760 KB |
Output is correct |
14 |
Correct |
55 ms |
22396 KB |
Output is correct |
15 |
Correct |
66 ms |
24148 KB |
Output is correct |
16 |
Correct |
54 ms |
22464 KB |
Output is correct |
17 |
Correct |
61 ms |
24112 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
27 ms |
18308 KB |
Output is correct |
2 |
Correct |
34 ms |
18404 KB |
Output is correct |
3 |
Correct |
38 ms |
18532 KB |
Output is correct |
4 |
Correct |
37 ms |
19056 KB |
Output is correct |
5 |
Correct |
55 ms |
20612 KB |
Output is correct |
6 |
Correct |
51 ms |
20536 KB |
Output is correct |
7 |
Correct |
69 ms |
20632 KB |
Output is correct |
8 |
Correct |
52 ms |
20564 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
7252 KB |
Output is correct |
2 |
Correct |
3 ms |
7256 KB |
Output is correct |
3 |
Correct |
3 ms |
7252 KB |
Output is correct |
4 |
Correct |
4 ms |
7380 KB |
Output is correct |
5 |
Correct |
3 ms |
7252 KB |
Output is correct |
6 |
Correct |
3 ms |
7356 KB |
Output is correct |
7 |
Correct |
3 ms |
7252 KB |
Output is correct |
8 |
Correct |
3 ms |
7252 KB |
Output is correct |
9 |
Correct |
4 ms |
7380 KB |
Output is correct |
10 |
Correct |
4 ms |
7508 KB |
Output is correct |
11 |
Correct |
4 ms |
7380 KB |
Output is correct |
12 |
Correct |
4 ms |
7380 KB |
Output is correct |
13 |
Correct |
3 ms |
7356 KB |
Output is correct |
14 |
Correct |
4 ms |
7380 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
7252 KB |
Output is correct |
2 |
Correct |
3 ms |
7256 KB |
Output is correct |
3 |
Correct |
3 ms |
7252 KB |
Output is correct |
4 |
Correct |
4 ms |
7380 KB |
Output is correct |
5 |
Correct |
3 ms |
7252 KB |
Output is correct |
6 |
Correct |
3 ms |
7356 KB |
Output is correct |
7 |
Correct |
3 ms |
7252 KB |
Output is correct |
8 |
Correct |
3 ms |
7252 KB |
Output is correct |
9 |
Correct |
4 ms |
7380 KB |
Output is correct |
10 |
Correct |
4 ms |
7508 KB |
Output is correct |
11 |
Correct |
4 ms |
7380 KB |
Output is correct |
12 |
Correct |
4 ms |
7380 KB |
Output is correct |
13 |
Correct |
3 ms |
7356 KB |
Output is correct |
14 |
Correct |
4 ms |
7380 KB |
Output is correct |
15 |
Correct |
3 ms |
7380 KB |
Output is correct |
16 |
Correct |
4 ms |
7408 KB |
Output is correct |
17 |
Correct |
17 ms |
10324 KB |
Output is correct |
18 |
Correct |
17 ms |
10580 KB |
Output is correct |
19 |
Correct |
17 ms |
10568 KB |
Output is correct |
20 |
Correct |
18 ms |
10564 KB |
Output is correct |
21 |
Correct |
17 ms |
10452 KB |
Output is correct |
22 |
Correct |
35 ms |
13632 KB |
Output is correct |
23 |
Correct |
6 ms |
7892 KB |
Output is correct |
24 |
Correct |
13 ms |
9300 KB |
Output is correct |
25 |
Correct |
5 ms |
7368 KB |
Output is correct |
26 |
Correct |
6 ms |
7852 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
7252 KB |
Output is correct |
2 |
Correct |
3 ms |
7256 KB |
Output is correct |
3 |
Correct |
3 ms |
7252 KB |
Output is correct |
4 |
Correct |
4 ms |
7380 KB |
Output is correct |
5 |
Correct |
3 ms |
7252 KB |
Output is correct |
6 |
Correct |
3 ms |
7356 KB |
Output is correct |
7 |
Correct |
3 ms |
7252 KB |
Output is correct |
8 |
Correct |
3 ms |
7252 KB |
Output is correct |
9 |
Correct |
4 ms |
7380 KB |
Output is correct |
10 |
Correct |
4 ms |
7508 KB |
Output is correct |
11 |
Correct |
4 ms |
7380 KB |
Output is correct |
12 |
Correct |
4 ms |
7380 KB |
Output is correct |
13 |
Correct |
3 ms |
7356 KB |
Output is correct |
14 |
Correct |
4 ms |
7380 KB |
Output is correct |
15 |
Correct |
3 ms |
7380 KB |
Output is correct |
16 |
Correct |
4 ms |
7408 KB |
Output is correct |
17 |
Correct |
17 ms |
10324 KB |
Output is correct |
18 |
Correct |
17 ms |
10580 KB |
Output is correct |
19 |
Correct |
17 ms |
10568 KB |
Output is correct |
20 |
Correct |
18 ms |
10564 KB |
Output is correct |
21 |
Correct |
17 ms |
10452 KB |
Output is correct |
22 |
Correct |
35 ms |
13632 KB |
Output is correct |
23 |
Correct |
6 ms |
7892 KB |
Output is correct |
24 |
Correct |
13 ms |
9300 KB |
Output is correct |
25 |
Correct |
5 ms |
7368 KB |
Output is correct |
26 |
Correct |
6 ms |
7852 KB |
Output is correct |
27 |
Correct |
5 ms |
7764 KB |
Output is correct |
28 |
Correct |
71 ms |
22680 KB |
Output is correct |
29 |
Correct |
115 ms |
28476 KB |
Output is correct |
30 |
Correct |
108 ms |
28344 KB |
Output is correct |
31 |
Correct |
101 ms |
28108 KB |
Output is correct |
32 |
Correct |
95 ms |
28904 KB |
Output is correct |
33 |
Correct |
102 ms |
28128 KB |
Output is correct |
34 |
Correct |
102 ms |
28108 KB |
Output is correct |
35 |
Correct |
41 ms |
15564 KB |
Output is correct |
36 |
Correct |
102 ms |
28684 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
27 ms |
18308 KB |
Output is correct |
2 |
Correct |
34 ms |
18404 KB |
Output is correct |
3 |
Correct |
38 ms |
18532 KB |
Output is correct |
4 |
Correct |
37 ms |
19056 KB |
Output is correct |
5 |
Correct |
55 ms |
20612 KB |
Output is correct |
6 |
Correct |
51 ms |
20536 KB |
Output is correct |
7 |
Correct |
69 ms |
20632 KB |
Output is correct |
8 |
Correct |
52 ms |
20564 KB |
Output is correct |
9 |
Correct |
57 ms |
22104 KB |
Output is correct |
10 |
Correct |
45 ms |
17612 KB |
Output is correct |
11 |
Correct |
85 ms |
28004 KB |
Output is correct |
12 |
Correct |
3 ms |
7352 KB |
Output is correct |
13 |
Correct |
3 ms |
7252 KB |
Output is correct |
14 |
Correct |
3 ms |
7252 KB |
Output is correct |
15 |
Correct |
3 ms |
7252 KB |
Output is correct |
16 |
Correct |
3 ms |
7252 KB |
Output is correct |
17 |
Correct |
3 ms |
7252 KB |
Output is correct |
18 |
Correct |
27 ms |
18220 KB |
Output is correct |
19 |
Correct |
27 ms |
18260 KB |
Output is correct |
20 |
Correct |
27 ms |
18292 KB |
Output is correct |
21 |
Correct |
27 ms |
18260 KB |
Output is correct |
22 |
Correct |
66 ms |
24768 KB |
Output is correct |
23 |
Correct |
95 ms |
31700 KB |
Output is correct |
24 |
Correct |
100 ms |
32596 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
55 ms |
20928 KB |
Output is correct |
2 |
Correct |
62 ms |
22888 KB |
Output is correct |
3 |
Correct |
28 ms |
18260 KB |
Output is correct |
4 |
Correct |
28 ms |
18260 KB |
Output is correct |
5 |
Correct |
150 ms |
32412 KB |
Output is correct |
6 |
Correct |
152 ms |
33224 KB |
Output is correct |
7 |
Correct |
3 ms |
7252 KB |
Output is correct |
8 |
Correct |
83 ms |
24764 KB |
Output is correct |
9 |
Correct |
121 ms |
27736 KB |
Output is correct |
10 |
Correct |
53 ms |
21012 KB |
Output is correct |
11 |
Correct |
62 ms |
22988 KB |
Output is correct |
12 |
Correct |
3 ms |
7252 KB |
Output is correct |
13 |
Correct |
3 ms |
7356 KB |
Output is correct |
14 |
Correct |
3 ms |
7356 KB |
Output is correct |
15 |
Correct |
3 ms |
7252 KB |
Output is correct |
16 |
Correct |
27 ms |
18264 KB |
Output is correct |
17 |
Correct |
28 ms |
18244 KB |
Output is correct |
18 |
Correct |
55 ms |
22376 KB |
Output is correct |
19 |
Correct |
64 ms |
24760 KB |
Output is correct |
20 |
Correct |
55 ms |
22396 KB |
Output is correct |
21 |
Correct |
66 ms |
24148 KB |
Output is correct |
22 |
Correct |
54 ms |
22464 KB |
Output is correct |
23 |
Correct |
61 ms |
24112 KB |
Output is correct |
24 |
Correct |
27 ms |
18308 KB |
Output is correct |
25 |
Correct |
34 ms |
18404 KB |
Output is correct |
26 |
Correct |
38 ms |
18532 KB |
Output is correct |
27 |
Correct |
37 ms |
19056 KB |
Output is correct |
28 |
Correct |
55 ms |
20612 KB |
Output is correct |
29 |
Correct |
51 ms |
20536 KB |
Output is correct |
30 |
Correct |
69 ms |
20632 KB |
Output is correct |
31 |
Correct |
52 ms |
20564 KB |
Output is correct |
32 |
Correct |
3 ms |
7252 KB |
Output is correct |
33 |
Correct |
3 ms |
7256 KB |
Output is correct |
34 |
Correct |
3 ms |
7252 KB |
Output is correct |
35 |
Correct |
4 ms |
7380 KB |
Output is correct |
36 |
Correct |
3 ms |
7252 KB |
Output is correct |
37 |
Correct |
3 ms |
7356 KB |
Output is correct |
38 |
Correct |
3 ms |
7252 KB |
Output is correct |
39 |
Correct |
3 ms |
7252 KB |
Output is correct |
40 |
Correct |
4 ms |
7380 KB |
Output is correct |
41 |
Correct |
4 ms |
7508 KB |
Output is correct |
42 |
Correct |
4 ms |
7380 KB |
Output is correct |
43 |
Correct |
4 ms |
7380 KB |
Output is correct |
44 |
Correct |
3 ms |
7356 KB |
Output is correct |
45 |
Correct |
4 ms |
7380 KB |
Output is correct |
46 |
Correct |
3 ms |
7380 KB |
Output is correct |
47 |
Correct |
4 ms |
7408 KB |
Output is correct |
48 |
Correct |
17 ms |
10324 KB |
Output is correct |
49 |
Correct |
17 ms |
10580 KB |
Output is correct |
50 |
Correct |
17 ms |
10568 KB |
Output is correct |
51 |
Correct |
18 ms |
10564 KB |
Output is correct |
52 |
Correct |
17 ms |
10452 KB |
Output is correct |
53 |
Correct |
35 ms |
13632 KB |
Output is correct |
54 |
Correct |
6 ms |
7892 KB |
Output is correct |
55 |
Correct |
13 ms |
9300 KB |
Output is correct |
56 |
Correct |
5 ms |
7368 KB |
Output is correct |
57 |
Correct |
6 ms |
7852 KB |
Output is correct |
58 |
Correct |
5 ms |
7764 KB |
Output is correct |
59 |
Correct |
71 ms |
22680 KB |
Output is correct |
60 |
Correct |
115 ms |
28476 KB |
Output is correct |
61 |
Correct |
108 ms |
28344 KB |
Output is correct |
62 |
Correct |
101 ms |
28108 KB |
Output is correct |
63 |
Correct |
95 ms |
28904 KB |
Output is correct |
64 |
Correct |
102 ms |
28128 KB |
Output is correct |
65 |
Correct |
102 ms |
28108 KB |
Output is correct |
66 |
Correct |
41 ms |
15564 KB |
Output is correct |
67 |
Correct |
102 ms |
28684 KB |
Output is correct |
68 |
Correct |
57 ms |
22104 KB |
Output is correct |
69 |
Correct |
45 ms |
17612 KB |
Output is correct |
70 |
Correct |
85 ms |
28004 KB |
Output is correct |
71 |
Correct |
3 ms |
7352 KB |
Output is correct |
72 |
Correct |
3 ms |
7252 KB |
Output is correct |
73 |
Correct |
3 ms |
7252 KB |
Output is correct |
74 |
Correct |
3 ms |
7252 KB |
Output is correct |
75 |
Correct |
3 ms |
7252 KB |
Output is correct |
76 |
Correct |
3 ms |
7252 KB |
Output is correct |
77 |
Correct |
27 ms |
18220 KB |
Output is correct |
78 |
Correct |
27 ms |
18260 KB |
Output is correct |
79 |
Correct |
27 ms |
18292 KB |
Output is correct |
80 |
Correct |
27 ms |
18260 KB |
Output is correct |
81 |
Correct |
66 ms |
24768 KB |
Output is correct |
82 |
Correct |
95 ms |
31700 KB |
Output is correct |
83 |
Correct |
100 ms |
32596 KB |
Output is correct |
84 |
Correct |
136 ms |
37784 KB |
Output is correct |
85 |
Correct |
138 ms |
38156 KB |
Output is correct |
86 |
Correct |
152 ms |
38176 KB |
Output is correct |
87 |
Correct |
155 ms |
39144 KB |
Output is correct |
88 |
Correct |
3 ms |
7252 KB |
Output is correct |
89 |
Correct |
152 ms |
39136 KB |
Output is correct |
90 |
Correct |
135 ms |
37984 KB |
Output is correct |
91 |
Correct |
130 ms |
37964 KB |
Output is correct |