# include <bits/stdc++.h>
using namespace std;
#define f first
#define s second
#define pii pair <int, int>
#define pb push_back
const int N = 3e5 + 5;
const long long inf = 1e17;
int n,m,x[N],y[N],w[N];
vector <pii> v[N];
long long ans;
vector <long long> pot[N],pr[N],pr_dp[N],sf_dp[N], dp[N],dp_inc[N],dp_dec[N];
int go(int idx, long long val) {
int le = 0;
int ri = pot[idx].size() - 1;
int ans = - 1;
while (le <= ri) {
int mid = (le + ri) / 2;
if (pot[idx][mid] <= val) {
ans = mid;
le = mid + 1;
} else ri = mid - 1;
}
return ans;
}
long long max_weights(int N, int M, std::vector<int> X, std::vector<int> Y, std::vector<int> W) {
m = M;
n = N;
for (int i = 0; i < m; i++) {
x[i] = X[i] + 1;
y[i] = Y[i] + 1;
w[i] = W[i];
v[x[i]].pb({y[i] - 1, w[i]});
pot[x[i]].pb(y[i] - 1);
}
for (int i = 1; i <= n; i++) {
v[i].pb({0,0}); v[i].pb({n,0}); sort(v[i].begin(),v[i].end());
pot[i].pb(0); pot[i].pb(n); sort(pot[i].begin(),pot[i].end());
pr[i] = vector <long long> (v[i].size() + 5, 0);
for (int j = 0; j < v[i].size(); j++) {
pr[i][j] = pr[i][j - (j != 0)] + v[i][j].s;
}
}
/*
for (int i = 1; i <= n; i++) {
cout<<i<<" ----- > ";
for (int po : pot[i]) cout<<po<<" ";
cout<<"\n";
}*/
for (int i = 1; i <= n; i++) {
dp[i] = vector <long long> (pot[i].size() + 5, 0);
dp_inc[i] = dp_dec[i] = vector < long long > (pot[i].size() + 5, 0);
sf_dp[i] = pr_dp[i] = vector <long long> (pot[i].size() + 5, -inf);
if (i > 1) {
// if ( a >= b)
long long mx = 0;
for (int j = 0; j < v[i - 1].size(); j++) mx = max(mx, dp_inc[i - 1][j]), mx = max(mx, dp_dec[i - 1][j]);
dp_inc[i][0] = max(dp_inc[i][0], mx);
dp_dec[i][0] = max(dp_dec[i][0], mx);
for (int j = 0; j < pot[i].size(); j++) {
int a = pot[i][j];
int prid = go(i - 1, a - 1);
// dp[i][j] = dp[i - 1][k] + (pr[i - 1][j] - pr[i - 1][k])
// dp[i][j] = (dp[i - 1][k] - pr[i - 1][k]) + pr[i - 1][j]
if (prid == -1) {
dp_inc[i][j] = mx;
continue;
}
dp_inc[i][j] = max(dp_inc[i][j], pr_dp[i - 1][prid] + pr[i - 1][prid]);
}
dp_inc[i][pot[i].size() - 1] = max(dp_inc[i][pot[i].size() - 1], max(dp_inc[i - 1][pot[i - 1].size() - 1], dp_dec[i - 1][pot[i - 1].size() - 1]));
dp_dec[i][pot[i].size() - 1] = dp_inc[i][pot[i].size() - 1];
// if ( a < b )
for (int j = 0; j < pot[i].size(); j++) {
int a = pot[i][j];
int prid = go(i - 1, a - 1); prid++;
dp_dec[i][j] = max(dp_dec[i][j], sf_dp[i - 1][prid] - (j ? pr[i][j - 1] : 0)); // ?
}
}
/*for (int j = 0; j < pot[i].size(); j++) {
if (dp_dec[i][j])cout<<i<<" "<<pot[i][j]<<" "<<dp_dec[i][j]<<"\n";
}*/
if (i == n) continue;
pr_dp[i][0] = dp_inc[i][0];
for (int j = 1; j < pot[i].size(); j++) {
pr_dp[i][j] = max(pr_dp[i][j - 1], dp_inc[i][j] - pr[i][j - 1]); // j - 1
}
set <pii> s;
long long sf_sum = 0;
for (pii x : v[i + 1]) {
s.insert({x.f, x.s});
sf_sum += x.s;
}
int sz = pot[i].size();
for (int j = sz - 1; j >= 0; j--) {
while (s.size() && (*--s.end()).f >= pot[i][j]) {
sf_sum -= (*--s.end()).s;
s.erase(--s.end());
}
sf_dp[i][j] = max(sf_dp[i][j + 1], dp_dec[i][j] + sf_sum);
}
}
for (int i = 1; i <= n; i++) {
for (int j = 0; j < pot[i].size(); j++) {
ans = max(ans, dp_inc[i][j]);
ans = max(ans, dp_dec[i][j]);
}
}
return ans;
}
/*
signed main() {
int N, M;
assert(2 == scanf("%d %d", &N, &M));
std::vector<int> X(M), Y(M), W(M);
for (int i = 0; i < M; ++i) {
assert(3 == scanf("%d %d %d", &X[i], &Y[i], &W[i]));
}
long long result = max_weights(N, M, X, Y, W);
printf("%lld\n", result);
return 0;
}*/
Compilation message
fish.cpp: In function 'long long int max_weights(int, int, std::vector<int>, std::vector<int>, std::vector<int>)':
fish.cpp:40:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
40 | for (int j = 0; j < v[i].size(); j++) {
| ~~^~~~~~~~~~~~~
fish.cpp:58:31: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
58 | for (int j = 0; j < v[i - 1].size(); j++) mx = max(mx, dp_inc[i - 1][j]), mx = max(mx, dp_dec[i - 1][j]);
| ~~^~~~~~~~~~~~~~~~~
fish.cpp:61:31: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
61 | for (int j = 0; j < pot[i].size(); j++) {
| ~~^~~~~~~~~~~~~~~
fish.cpp:76:31: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
76 | for (int j = 0; j < pot[i].size(); j++) {
| ~~^~~~~~~~~~~~~~~
fish.cpp:92:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
92 | for (int j = 1; j < pot[i].size(); j++) {
| ~~^~~~~~~~~~~~~~~
fish.cpp:111:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
111 | for (int j = 0; j < pot[i].size(); j++) {
| ~~^~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
130 ms |
104504 KB |
Output is correct |
2 |
Correct |
153 ms |
110864 KB |
Output is correct |
3 |
Correct |
96 ms |
100436 KB |
Output is correct |
4 |
Correct |
100 ms |
100520 KB |
Output is correct |
5 |
Correct |
313 ms |
130348 KB |
Output is correct |
6 |
Correct |
399 ms |
134092 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
33 ms |
56660 KB |
Output is correct |
2 |
Correct |
197 ms |
112512 KB |
Output is correct |
3 |
Correct |
244 ms |
120592 KB |
Output is correct |
4 |
Correct |
126 ms |
104436 KB |
Output is correct |
5 |
Correct |
148 ms |
110812 KB |
Output is correct |
6 |
Correct |
26 ms |
56612 KB |
Output is correct |
7 |
Correct |
30 ms |
56568 KB |
Output is correct |
8 |
Correct |
27 ms |
56684 KB |
Output is correct |
9 |
Correct |
27 ms |
56660 KB |
Output is correct |
10 |
Correct |
97 ms |
100588 KB |
Output is correct |
11 |
Correct |
97 ms |
100500 KB |
Output is correct |
12 |
Correct |
158 ms |
104484 KB |
Output is correct |
13 |
Correct |
181 ms |
110876 KB |
Output is correct |
14 |
Correct |
154 ms |
104524 KB |
Output is correct |
15 |
Correct |
167 ms |
109936 KB |
Output is correct |
16 |
Correct |
156 ms |
104612 KB |
Output is correct |
17 |
Correct |
161 ms |
109776 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
105 ms |
100444 KB |
Output is correct |
2 |
Correct |
104 ms |
100396 KB |
Output is correct |
3 |
Correct |
141 ms |
105416 KB |
Output is correct |
4 |
Correct |
130 ms |
106780 KB |
Output is correct |
5 |
Correct |
172 ms |
118056 KB |
Output is correct |
6 |
Correct |
184 ms |
118732 KB |
Output is correct |
7 |
Correct |
180 ms |
118676 KB |
Output is correct |
8 |
Correct |
174 ms |
118636 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
26 ms |
56652 KB |
Output is correct |
2 |
Correct |
26 ms |
56636 KB |
Output is correct |
3 |
Correct |
29 ms |
56628 KB |
Output is correct |
4 |
Correct |
26 ms |
56660 KB |
Output is correct |
5 |
Correct |
28 ms |
56664 KB |
Output is correct |
6 |
Correct |
27 ms |
56660 KB |
Output is correct |
7 |
Correct |
26 ms |
56580 KB |
Output is correct |
8 |
Correct |
30 ms |
56660 KB |
Output is correct |
9 |
Correct |
31 ms |
56740 KB |
Output is correct |
10 |
Correct |
29 ms |
57044 KB |
Output is correct |
11 |
Correct |
29 ms |
56808 KB |
Output is correct |
12 |
Correct |
29 ms |
56908 KB |
Output is correct |
13 |
Correct |
28 ms |
56736 KB |
Output is correct |
14 |
Correct |
27 ms |
56872 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
26 ms |
56652 KB |
Output is correct |
2 |
Correct |
26 ms |
56636 KB |
Output is correct |
3 |
Correct |
29 ms |
56628 KB |
Output is correct |
4 |
Correct |
26 ms |
56660 KB |
Output is correct |
5 |
Correct |
28 ms |
56664 KB |
Output is correct |
6 |
Correct |
27 ms |
56660 KB |
Output is correct |
7 |
Correct |
26 ms |
56580 KB |
Output is correct |
8 |
Correct |
30 ms |
56660 KB |
Output is correct |
9 |
Correct |
31 ms |
56740 KB |
Output is correct |
10 |
Correct |
29 ms |
57044 KB |
Output is correct |
11 |
Correct |
29 ms |
56808 KB |
Output is correct |
12 |
Correct |
29 ms |
56908 KB |
Output is correct |
13 |
Correct |
28 ms |
56736 KB |
Output is correct |
14 |
Correct |
27 ms |
56872 KB |
Output is correct |
15 |
Correct |
26 ms |
56732 KB |
Output is correct |
16 |
Correct |
27 ms |
56908 KB |
Output is correct |
17 |
Correct |
60 ms |
61896 KB |
Output is correct |
18 |
Correct |
53 ms |
62424 KB |
Output is correct |
19 |
Correct |
54 ms |
62412 KB |
Output is correct |
20 |
Correct |
54 ms |
62412 KB |
Output is correct |
21 |
Correct |
51 ms |
62416 KB |
Output is correct |
22 |
Correct |
80 ms |
67388 KB |
Output is correct |
23 |
Correct |
36 ms |
57676 KB |
Output is correct |
24 |
Correct |
46 ms |
60196 KB |
Output is correct |
25 |
Correct |
27 ms |
56904 KB |
Output is correct |
26 |
Correct |
33 ms |
57656 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
26 ms |
56652 KB |
Output is correct |
2 |
Correct |
26 ms |
56636 KB |
Output is correct |
3 |
Correct |
29 ms |
56628 KB |
Output is correct |
4 |
Correct |
26 ms |
56660 KB |
Output is correct |
5 |
Correct |
28 ms |
56664 KB |
Output is correct |
6 |
Correct |
27 ms |
56660 KB |
Output is correct |
7 |
Correct |
26 ms |
56580 KB |
Output is correct |
8 |
Correct |
30 ms |
56660 KB |
Output is correct |
9 |
Correct |
31 ms |
56740 KB |
Output is correct |
10 |
Correct |
29 ms |
57044 KB |
Output is correct |
11 |
Correct |
29 ms |
56808 KB |
Output is correct |
12 |
Correct |
29 ms |
56908 KB |
Output is correct |
13 |
Correct |
28 ms |
56736 KB |
Output is correct |
14 |
Correct |
27 ms |
56872 KB |
Output is correct |
15 |
Correct |
26 ms |
56732 KB |
Output is correct |
16 |
Correct |
27 ms |
56908 KB |
Output is correct |
17 |
Correct |
60 ms |
61896 KB |
Output is correct |
18 |
Correct |
53 ms |
62424 KB |
Output is correct |
19 |
Correct |
54 ms |
62412 KB |
Output is correct |
20 |
Correct |
54 ms |
62412 KB |
Output is correct |
21 |
Correct |
51 ms |
62416 KB |
Output is correct |
22 |
Correct |
80 ms |
67388 KB |
Output is correct |
23 |
Correct |
36 ms |
57676 KB |
Output is correct |
24 |
Correct |
46 ms |
60196 KB |
Output is correct |
25 |
Correct |
27 ms |
56904 KB |
Output is correct |
26 |
Correct |
33 ms |
57656 KB |
Output is correct |
27 |
Correct |
29 ms |
58516 KB |
Output is correct |
28 |
Correct |
156 ms |
81384 KB |
Output is correct |
29 |
Correct |
213 ms |
90068 KB |
Output is correct |
30 |
Correct |
207 ms |
89588 KB |
Output is correct |
31 |
Correct |
198 ms |
89556 KB |
Output is correct |
32 |
Correct |
202 ms |
91540 KB |
Output is correct |
33 |
Correct |
201 ms |
89768 KB |
Output is correct |
34 |
Correct |
199 ms |
89520 KB |
Output is correct |
35 |
Correct |
97 ms |
70984 KB |
Output is correct |
36 |
Correct |
232 ms |
93600 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
105 ms |
100444 KB |
Output is correct |
2 |
Correct |
104 ms |
100396 KB |
Output is correct |
3 |
Correct |
141 ms |
105416 KB |
Output is correct |
4 |
Correct |
130 ms |
106780 KB |
Output is correct |
5 |
Correct |
172 ms |
118056 KB |
Output is correct |
6 |
Correct |
184 ms |
118732 KB |
Output is correct |
7 |
Correct |
180 ms |
118676 KB |
Output is correct |
8 |
Correct |
174 ms |
118636 KB |
Output is correct |
9 |
Correct |
176 ms |
118704 KB |
Output is correct |
10 |
Correct |
126 ms |
89964 KB |
Output is correct |
11 |
Correct |
228 ms |
123980 KB |
Output is correct |
12 |
Correct |
26 ms |
56588 KB |
Output is correct |
13 |
Correct |
28 ms |
56572 KB |
Output is correct |
14 |
Correct |
25 ms |
56668 KB |
Output is correct |
15 |
Correct |
27 ms |
56592 KB |
Output is correct |
16 |
Correct |
26 ms |
56660 KB |
Output is correct |
17 |
Correct |
25 ms |
56660 KB |
Output is correct |
18 |
Correct |
95 ms |
100484 KB |
Output is correct |
19 |
Correct |
104 ms |
100504 KB |
Output is correct |
20 |
Correct |
97 ms |
100496 KB |
Output is correct |
21 |
Correct |
100 ms |
100436 KB |
Output is correct |
22 |
Correct |
203 ms |
114648 KB |
Output is correct |
23 |
Correct |
237 ms |
123112 KB |
Output is correct |
24 |
Correct |
250 ms |
124956 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
130 ms |
104504 KB |
Output is correct |
2 |
Correct |
153 ms |
110864 KB |
Output is correct |
3 |
Correct |
96 ms |
100436 KB |
Output is correct |
4 |
Correct |
100 ms |
100520 KB |
Output is correct |
5 |
Correct |
313 ms |
130348 KB |
Output is correct |
6 |
Correct |
399 ms |
134092 KB |
Output is correct |
7 |
Correct |
33 ms |
56660 KB |
Output is correct |
8 |
Correct |
197 ms |
112512 KB |
Output is correct |
9 |
Correct |
244 ms |
120592 KB |
Output is correct |
10 |
Correct |
126 ms |
104436 KB |
Output is correct |
11 |
Correct |
148 ms |
110812 KB |
Output is correct |
12 |
Correct |
26 ms |
56612 KB |
Output is correct |
13 |
Correct |
30 ms |
56568 KB |
Output is correct |
14 |
Correct |
27 ms |
56684 KB |
Output is correct |
15 |
Correct |
27 ms |
56660 KB |
Output is correct |
16 |
Correct |
97 ms |
100588 KB |
Output is correct |
17 |
Correct |
97 ms |
100500 KB |
Output is correct |
18 |
Correct |
158 ms |
104484 KB |
Output is correct |
19 |
Correct |
181 ms |
110876 KB |
Output is correct |
20 |
Correct |
154 ms |
104524 KB |
Output is correct |
21 |
Correct |
167 ms |
109936 KB |
Output is correct |
22 |
Correct |
156 ms |
104612 KB |
Output is correct |
23 |
Correct |
161 ms |
109776 KB |
Output is correct |
24 |
Correct |
105 ms |
100444 KB |
Output is correct |
25 |
Correct |
104 ms |
100396 KB |
Output is correct |
26 |
Correct |
141 ms |
105416 KB |
Output is correct |
27 |
Correct |
130 ms |
106780 KB |
Output is correct |
28 |
Correct |
172 ms |
118056 KB |
Output is correct |
29 |
Correct |
184 ms |
118732 KB |
Output is correct |
30 |
Correct |
180 ms |
118676 KB |
Output is correct |
31 |
Correct |
174 ms |
118636 KB |
Output is correct |
32 |
Correct |
26 ms |
56652 KB |
Output is correct |
33 |
Correct |
26 ms |
56636 KB |
Output is correct |
34 |
Correct |
29 ms |
56628 KB |
Output is correct |
35 |
Correct |
26 ms |
56660 KB |
Output is correct |
36 |
Correct |
28 ms |
56664 KB |
Output is correct |
37 |
Correct |
27 ms |
56660 KB |
Output is correct |
38 |
Correct |
26 ms |
56580 KB |
Output is correct |
39 |
Correct |
30 ms |
56660 KB |
Output is correct |
40 |
Correct |
31 ms |
56740 KB |
Output is correct |
41 |
Correct |
29 ms |
57044 KB |
Output is correct |
42 |
Correct |
29 ms |
56808 KB |
Output is correct |
43 |
Correct |
29 ms |
56908 KB |
Output is correct |
44 |
Correct |
28 ms |
56736 KB |
Output is correct |
45 |
Correct |
27 ms |
56872 KB |
Output is correct |
46 |
Correct |
26 ms |
56732 KB |
Output is correct |
47 |
Correct |
27 ms |
56908 KB |
Output is correct |
48 |
Correct |
60 ms |
61896 KB |
Output is correct |
49 |
Correct |
53 ms |
62424 KB |
Output is correct |
50 |
Correct |
54 ms |
62412 KB |
Output is correct |
51 |
Correct |
54 ms |
62412 KB |
Output is correct |
52 |
Correct |
51 ms |
62416 KB |
Output is correct |
53 |
Correct |
80 ms |
67388 KB |
Output is correct |
54 |
Correct |
36 ms |
57676 KB |
Output is correct |
55 |
Correct |
46 ms |
60196 KB |
Output is correct |
56 |
Correct |
27 ms |
56904 KB |
Output is correct |
57 |
Correct |
33 ms |
57656 KB |
Output is correct |
58 |
Correct |
29 ms |
58516 KB |
Output is correct |
59 |
Correct |
156 ms |
81384 KB |
Output is correct |
60 |
Correct |
213 ms |
90068 KB |
Output is correct |
61 |
Correct |
207 ms |
89588 KB |
Output is correct |
62 |
Correct |
198 ms |
89556 KB |
Output is correct |
63 |
Correct |
202 ms |
91540 KB |
Output is correct |
64 |
Correct |
201 ms |
89768 KB |
Output is correct |
65 |
Correct |
199 ms |
89520 KB |
Output is correct |
66 |
Correct |
97 ms |
70984 KB |
Output is correct |
67 |
Correct |
232 ms |
93600 KB |
Output is correct |
68 |
Correct |
176 ms |
118704 KB |
Output is correct |
69 |
Correct |
126 ms |
89964 KB |
Output is correct |
70 |
Correct |
228 ms |
123980 KB |
Output is correct |
71 |
Correct |
26 ms |
56588 KB |
Output is correct |
72 |
Correct |
28 ms |
56572 KB |
Output is correct |
73 |
Correct |
25 ms |
56668 KB |
Output is correct |
74 |
Correct |
27 ms |
56592 KB |
Output is correct |
75 |
Correct |
26 ms |
56660 KB |
Output is correct |
76 |
Correct |
25 ms |
56660 KB |
Output is correct |
77 |
Correct |
95 ms |
100484 KB |
Output is correct |
78 |
Correct |
104 ms |
100504 KB |
Output is correct |
79 |
Correct |
97 ms |
100496 KB |
Output is correct |
80 |
Correct |
100 ms |
100436 KB |
Output is correct |
81 |
Correct |
203 ms |
114648 KB |
Output is correct |
82 |
Correct |
237 ms |
123112 KB |
Output is correct |
83 |
Correct |
250 ms |
124956 KB |
Output is correct |
84 |
Correct |
313 ms |
130956 KB |
Output is correct |
85 |
Correct |
319 ms |
133300 KB |
Output is correct |
86 |
Correct |
414 ms |
134084 KB |
Output is correct |
87 |
Correct |
404 ms |
136724 KB |
Output is correct |
88 |
Correct |
38 ms |
56680 KB |
Output is correct |
89 |
Correct |
387 ms |
138804 KB |
Output is correct |
90 |
Correct |
367 ms |
135008 KB |
Output is correct |
91 |
Correct |
303 ms |
134160 KB |
Output is correct |