#ifndef mikevui
// #include "task.h"
#endif // mikevui
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double dou;
#define pii pair<int, int>
#define fi first
#define se second
#define pb push_back
#define MASK(x) (1LL<<(x))
#define BITj(x, j) (((x)>>(j))&1)
template<typename T> bool maximize(T &x, const T &y) {
if (x < y) {x = y; return 1;}
return 0;
}
template<typename T> bool minimize(T &x, const T &y) {
if (x > y) {x = y; return 1;}
return 0;
}
const int maxn = 1e5+5;
const ll INF = (ll)3e18+7;
int n, m;
vector<pii> a[maxn];
vector<ll> dp1[maxn], dp0[maxn];
ll ans = 0;
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];
a[x].pb({y, w});
}
for (int i = 1; i <= n; i++) {
sort(a[i].begin(), a[i].end());
}
ll ma10, ma11, ma0;
ma10 = ma11 = ma0 = 0;
//dp
for (int pos= 1; pos <= n; pos++) {
//init
dp1[pos].assign((int)a[pos].size(), -INF);
dp0[pos].assign((int)a[pos].size(), -INF);
int len = (int)a[pos].size();
//inc -> 0
if (pos < n) {
int j = -1;
ll malas = 0;
for (int i = 0; i < len; i++) {
dp0[pos][i] = max(ma10, ma0);
if (i > 0) maximize(dp0[pos][i], dp0[pos][i-1]);
if (pos > 1) {
while (j+1 < (int)a[pos-1].size() && a[pos-1][j+1].fi < a[pos][i].fi) {
++j;
maximize(malas, dp0[pos-1][j]);
}
if (j > -1 && a[pos-1][j].fi < a[pos][i].fi) {
maximize(dp0[pos][i], malas);
}
}
dp0[pos][i] += a[pos][i].se;
}
}
//dec -> 1
if (pos > 1) {
int j = (int)a[pos-1].size();
ll malas = 0;
for (int i = len-1; i >= 0; i--) {
dp1[pos][i] = max(ma11, ma0);
if (i < len-1) maximize(dp1[pos][i], dp1[pos][i+1]);
while (j-1 >= 0 && a[pos-1][j-1].fi > a[pos][i].fi) {
--j;
maximize(malas, dp1[pos-1][j]);
}
if (j < (int)a[pos-1].size() && a[pos-1][j].fi > a[pos][i].fi) {
maximize(dp1[pos][i], malas);
}
dp1[pos][i] += a[pos][i].se;
}
}
//update ma
for (int i = 0; i < len; i++) {
maximize(ma10, dp1[pos][i]);
maximize(ans, dp1[pos][i]);
maximize(ans, dp0[pos][i]);
}
if (pos > 1) {
for (int i = 0; i < (int)a[pos-1].size(); i++) {
maximize(ma11, dp1[pos-1][i]);
maximize(ma0, dp0[pos-1][i]);
}
}
}
// for (int pos = 1; pos <= n; pos++) {
// cout << "curpos " << pos << "\n";
// for (int i= 0; i < (int)a[pos].size(); i++) {
// cout << a[pos][i].fi << ' ' << a[pos][i].se << ' ' << (dp0[pos][i] <= -INF ? -1 : dp0[pos][i]) << ' ' << (dp1[pos][i] <= -INF ? -1 : dp1[pos][i]) << "\n";
// }
// }
return ans;
}
#ifdef mikevui
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
// #define name "task"
// if (fopen(name".inp", "r")) {
// freopen(name".inp", "r", stdin);
// freopen(name".out", "w", stdout);
// }
int N, M;
vector<int> X, Y, W;
cin >> N >> M;
for (int i = 0; i < M; i++) {
int x, y, w;
cin >> x >> y >> w;
X.pb(x);
Y.pb(y);
W.pb(w);
}
cout << max_weights(N, M, X, Y, W);
}
#endif // mikevui
/*
3 3
2 1 5
1 2 1
1 0 2
6
5 4
0 2 5
1 1 2
4 4 1
3 3 3
*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
42 ms |
12344 KB |
Output is correct |
2 |
Correct |
33 ms |
13244 KB |
Output is correct |
3 |
Correct |
4 ms |
7248 KB |
Output is correct |
4 |
Correct |
4 ms |
7248 KB |
Output is correct |
5 |
Correct |
94 ms |
26540 KB |
Output is correct |
6 |
Correct |
98 ms |
29512 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
7248 KB |
Output is correct |
2 |
Correct |
53 ms |
17308 KB |
Output is correct |
3 |
Correct |
61 ms |
20148 KB |
Output is correct |
4 |
Correct |
26 ms |
12228 KB |
Output is correct |
5 |
Correct |
30 ms |
12228 KB |
Output is correct |
6 |
Correct |
3 ms |
7248 KB |
Output is correct |
7 |
Correct |
2 ms |
7248 KB |
Output is correct |
8 |
Correct |
2 ms |
7248 KB |
Output is correct |
9 |
Correct |
2 ms |
7248 KB |
Output is correct |
10 |
Correct |
3 ms |
7248 KB |
Output is correct |
11 |
Correct |
3 ms |
7248 KB |
Output is correct |
12 |
Correct |
27 ms |
12212 KB |
Output is correct |
13 |
Correct |
32 ms |
13880 KB |
Output is correct |
14 |
Correct |
26 ms |
12372 KB |
Output is correct |
15 |
Correct |
29 ms |
13524 KB |
Output is correct |
16 |
Correct |
26 ms |
12740 KB |
Output is correct |
17 |
Correct |
29 ms |
12740 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
7248 KB |
Output is correct |
2 |
Correct |
3 ms |
7248 KB |
Output is correct |
3 |
Correct |
24 ms |
14408 KB |
Output is correct |
4 |
Correct |
22 ms |
12104 KB |
Output is correct |
5 |
Correct |
43 ms |
20808 KB |
Output is correct |
6 |
Correct |
42 ms |
19800 KB |
Output is correct |
7 |
Correct |
42 ms |
20656 KB |
Output is correct |
8 |
Correct |
45 ms |
20040 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
7248 KB |
Output is correct |
2 |
Correct |
3 ms |
7248 KB |
Output is correct |
3 |
Correct |
3 ms |
7416 KB |
Output is correct |
4 |
Correct |
3 ms |
7248 KB |
Output is correct |
5 |
Correct |
4 ms |
7248 KB |
Output is correct |
6 |
Correct |
3 ms |
7248 KB |
Output is correct |
7 |
Correct |
3 ms |
7248 KB |
Output is correct |
8 |
Correct |
2 ms |
7248 KB |
Output is correct |
9 |
Correct |
3 ms |
7672 KB |
Output is correct |
10 |
Correct |
3 ms |
7504 KB |
Output is correct |
11 |
Correct |
3 ms |
7504 KB |
Output is correct |
12 |
Correct |
3 ms |
7504 KB |
Output is correct |
13 |
Correct |
3 ms |
7504 KB |
Output is correct |
14 |
Correct |
3 ms |
7668 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
7248 KB |
Output is correct |
2 |
Correct |
3 ms |
7248 KB |
Output is correct |
3 |
Correct |
3 ms |
7416 KB |
Output is correct |
4 |
Correct |
3 ms |
7248 KB |
Output is correct |
5 |
Correct |
4 ms |
7248 KB |
Output is correct |
6 |
Correct |
3 ms |
7248 KB |
Output is correct |
7 |
Correct |
3 ms |
7248 KB |
Output is correct |
8 |
Correct |
2 ms |
7248 KB |
Output is correct |
9 |
Correct |
3 ms |
7672 KB |
Output is correct |
10 |
Correct |
3 ms |
7504 KB |
Output is correct |
11 |
Correct |
3 ms |
7504 KB |
Output is correct |
12 |
Correct |
3 ms |
7504 KB |
Output is correct |
13 |
Correct |
3 ms |
7504 KB |
Output is correct |
14 |
Correct |
3 ms |
7668 KB |
Output is correct |
15 |
Correct |
3 ms |
7504 KB |
Output is correct |
16 |
Correct |
3 ms |
7504 KB |
Output is correct |
17 |
Correct |
14 ms |
10316 KB |
Output is correct |
18 |
Correct |
16 ms |
10576 KB |
Output is correct |
19 |
Correct |
16 ms |
10676 KB |
Output is correct |
20 |
Correct |
15 ms |
10744 KB |
Output is correct |
21 |
Correct |
17 ms |
10432 KB |
Output is correct |
22 |
Correct |
28 ms |
13648 KB |
Output is correct |
23 |
Correct |
6 ms |
8016 KB |
Output is correct |
24 |
Correct |
12 ms |
9296 KB |
Output is correct |
25 |
Correct |
3 ms |
7504 KB |
Output is correct |
26 |
Correct |
5 ms |
8220 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
7248 KB |
Output is correct |
2 |
Correct |
3 ms |
7248 KB |
Output is correct |
3 |
Correct |
3 ms |
7416 KB |
Output is correct |
4 |
Correct |
3 ms |
7248 KB |
Output is correct |
5 |
Correct |
4 ms |
7248 KB |
Output is correct |
6 |
Correct |
3 ms |
7248 KB |
Output is correct |
7 |
Correct |
3 ms |
7248 KB |
Output is correct |
8 |
Correct |
2 ms |
7248 KB |
Output is correct |
9 |
Correct |
3 ms |
7672 KB |
Output is correct |
10 |
Correct |
3 ms |
7504 KB |
Output is correct |
11 |
Correct |
3 ms |
7504 KB |
Output is correct |
12 |
Correct |
3 ms |
7504 KB |
Output is correct |
13 |
Correct |
3 ms |
7504 KB |
Output is correct |
14 |
Correct |
3 ms |
7668 KB |
Output is correct |
15 |
Correct |
3 ms |
7504 KB |
Output is correct |
16 |
Correct |
3 ms |
7504 KB |
Output is correct |
17 |
Correct |
14 ms |
10316 KB |
Output is correct |
18 |
Correct |
16 ms |
10576 KB |
Output is correct |
19 |
Correct |
16 ms |
10676 KB |
Output is correct |
20 |
Correct |
15 ms |
10744 KB |
Output is correct |
21 |
Correct |
17 ms |
10432 KB |
Output is correct |
22 |
Correct |
28 ms |
13648 KB |
Output is correct |
23 |
Correct |
6 ms |
8016 KB |
Output is correct |
24 |
Correct |
12 ms |
9296 KB |
Output is correct |
25 |
Correct |
3 ms |
7504 KB |
Output is correct |
26 |
Correct |
5 ms |
8220 KB |
Output is correct |
27 |
Correct |
3 ms |
7760 KB |
Output is correct |
28 |
Correct |
61 ms |
22668 KB |
Output is correct |
29 |
Correct |
98 ms |
28488 KB |
Output is correct |
30 |
Correct |
95 ms |
27976 KB |
Output is correct |
31 |
Correct |
88 ms |
27976 KB |
Output is correct |
32 |
Correct |
89 ms |
29000 KB |
Output is correct |
33 |
Correct |
96 ms |
28004 KB |
Output is correct |
34 |
Correct |
94 ms |
28008 KB |
Output is correct |
35 |
Correct |
38 ms |
15432 KB |
Output is correct |
36 |
Correct |
92 ms |
28488 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
7248 KB |
Output is correct |
2 |
Correct |
3 ms |
7248 KB |
Output is correct |
3 |
Correct |
24 ms |
14408 KB |
Output is correct |
4 |
Correct |
22 ms |
12104 KB |
Output is correct |
5 |
Correct |
43 ms |
20808 KB |
Output is correct |
6 |
Correct |
42 ms |
19800 KB |
Output is correct |
7 |
Correct |
42 ms |
20656 KB |
Output is correct |
8 |
Correct |
45 ms |
20040 KB |
Output is correct |
9 |
Correct |
40 ms |
20552 KB |
Output is correct |
10 |
Correct |
38 ms |
15504 KB |
Output is correct |
11 |
Correct |
84 ms |
24904 KB |
Output is correct |
12 |
Correct |
3 ms |
7248 KB |
Output is correct |
13 |
Correct |
3 ms |
7416 KB |
Output is correct |
14 |
Correct |
3 ms |
7248 KB |
Output is correct |
15 |
Correct |
2 ms |
7416 KB |
Output is correct |
16 |
Correct |
3 ms |
7248 KB |
Output is correct |
17 |
Correct |
3 ms |
7504 KB |
Output is correct |
18 |
Correct |
3 ms |
7248 KB |
Output is correct |
19 |
Correct |
4 ms |
7248 KB |
Output is correct |
20 |
Correct |
4 ms |
7248 KB |
Output is correct |
21 |
Correct |
6 ms |
7248 KB |
Output is correct |
22 |
Correct |
46 ms |
18120 KB |
Output is correct |
23 |
Correct |
77 ms |
25048 KB |
Output is correct |
24 |
Correct |
74 ms |
25424 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
42 ms |
12344 KB |
Output is correct |
2 |
Correct |
33 ms |
13244 KB |
Output is correct |
3 |
Correct |
4 ms |
7248 KB |
Output is correct |
4 |
Correct |
4 ms |
7248 KB |
Output is correct |
5 |
Correct |
94 ms |
26540 KB |
Output is correct |
6 |
Correct |
98 ms |
29512 KB |
Output is correct |
7 |
Correct |
2 ms |
7248 KB |
Output is correct |
8 |
Correct |
53 ms |
17308 KB |
Output is correct |
9 |
Correct |
61 ms |
20148 KB |
Output is correct |
10 |
Correct |
26 ms |
12228 KB |
Output is correct |
11 |
Correct |
30 ms |
12228 KB |
Output is correct |
12 |
Correct |
3 ms |
7248 KB |
Output is correct |
13 |
Correct |
2 ms |
7248 KB |
Output is correct |
14 |
Correct |
2 ms |
7248 KB |
Output is correct |
15 |
Correct |
2 ms |
7248 KB |
Output is correct |
16 |
Correct |
3 ms |
7248 KB |
Output is correct |
17 |
Correct |
3 ms |
7248 KB |
Output is correct |
18 |
Correct |
27 ms |
12212 KB |
Output is correct |
19 |
Correct |
32 ms |
13880 KB |
Output is correct |
20 |
Correct |
26 ms |
12372 KB |
Output is correct |
21 |
Correct |
29 ms |
13524 KB |
Output is correct |
22 |
Correct |
26 ms |
12740 KB |
Output is correct |
23 |
Correct |
29 ms |
12740 KB |
Output is correct |
24 |
Correct |
3 ms |
7248 KB |
Output is correct |
25 |
Correct |
3 ms |
7248 KB |
Output is correct |
26 |
Correct |
24 ms |
14408 KB |
Output is correct |
27 |
Correct |
22 ms |
12104 KB |
Output is correct |
28 |
Correct |
43 ms |
20808 KB |
Output is correct |
29 |
Correct |
42 ms |
19800 KB |
Output is correct |
30 |
Correct |
42 ms |
20656 KB |
Output is correct |
31 |
Correct |
45 ms |
20040 KB |
Output is correct |
32 |
Correct |
3 ms |
7248 KB |
Output is correct |
33 |
Correct |
3 ms |
7248 KB |
Output is correct |
34 |
Correct |
3 ms |
7416 KB |
Output is correct |
35 |
Correct |
3 ms |
7248 KB |
Output is correct |
36 |
Correct |
4 ms |
7248 KB |
Output is correct |
37 |
Correct |
3 ms |
7248 KB |
Output is correct |
38 |
Correct |
3 ms |
7248 KB |
Output is correct |
39 |
Correct |
2 ms |
7248 KB |
Output is correct |
40 |
Correct |
3 ms |
7672 KB |
Output is correct |
41 |
Correct |
3 ms |
7504 KB |
Output is correct |
42 |
Correct |
3 ms |
7504 KB |
Output is correct |
43 |
Correct |
3 ms |
7504 KB |
Output is correct |
44 |
Correct |
3 ms |
7504 KB |
Output is correct |
45 |
Correct |
3 ms |
7668 KB |
Output is correct |
46 |
Correct |
3 ms |
7504 KB |
Output is correct |
47 |
Correct |
3 ms |
7504 KB |
Output is correct |
48 |
Correct |
14 ms |
10316 KB |
Output is correct |
49 |
Correct |
16 ms |
10576 KB |
Output is correct |
50 |
Correct |
16 ms |
10676 KB |
Output is correct |
51 |
Correct |
15 ms |
10744 KB |
Output is correct |
52 |
Correct |
17 ms |
10432 KB |
Output is correct |
53 |
Correct |
28 ms |
13648 KB |
Output is correct |
54 |
Correct |
6 ms |
8016 KB |
Output is correct |
55 |
Correct |
12 ms |
9296 KB |
Output is correct |
56 |
Correct |
3 ms |
7504 KB |
Output is correct |
57 |
Correct |
5 ms |
8220 KB |
Output is correct |
58 |
Correct |
3 ms |
7760 KB |
Output is correct |
59 |
Correct |
61 ms |
22668 KB |
Output is correct |
60 |
Correct |
98 ms |
28488 KB |
Output is correct |
61 |
Correct |
95 ms |
27976 KB |
Output is correct |
62 |
Correct |
88 ms |
27976 KB |
Output is correct |
63 |
Correct |
89 ms |
29000 KB |
Output is correct |
64 |
Correct |
96 ms |
28004 KB |
Output is correct |
65 |
Correct |
94 ms |
28008 KB |
Output is correct |
66 |
Correct |
38 ms |
15432 KB |
Output is correct |
67 |
Correct |
92 ms |
28488 KB |
Output is correct |
68 |
Correct |
40 ms |
20552 KB |
Output is correct |
69 |
Correct |
38 ms |
15504 KB |
Output is correct |
70 |
Correct |
84 ms |
24904 KB |
Output is correct |
71 |
Correct |
3 ms |
7248 KB |
Output is correct |
72 |
Correct |
3 ms |
7416 KB |
Output is correct |
73 |
Correct |
3 ms |
7248 KB |
Output is correct |
74 |
Correct |
2 ms |
7416 KB |
Output is correct |
75 |
Correct |
3 ms |
7248 KB |
Output is correct |
76 |
Correct |
3 ms |
7504 KB |
Output is correct |
77 |
Correct |
3 ms |
7248 KB |
Output is correct |
78 |
Correct |
4 ms |
7248 KB |
Output is correct |
79 |
Correct |
4 ms |
7248 KB |
Output is correct |
80 |
Correct |
6 ms |
7248 KB |
Output is correct |
81 |
Correct |
46 ms |
18120 KB |
Output is correct |
82 |
Correct |
77 ms |
25048 KB |
Output is correct |
83 |
Correct |
74 ms |
25424 KB |
Output is correct |
84 |
Correct |
105 ms |
27980 KB |
Output is correct |
85 |
Correct |
97 ms |
27528 KB |
Output is correct |
86 |
Correct |
121 ms |
32072 KB |
Output is correct |
87 |
Correct |
107 ms |
32584 KB |
Output is correct |
88 |
Correct |
3 ms |
7248 KB |
Output is correct |
89 |
Correct |
101 ms |
32616 KB |
Output is correct |
90 |
Correct |
94 ms |
29540 KB |
Output is correct |
91 |
Correct |
92 ms |
28488 KB |
Output is correct |