#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
/*
5 4
0 2 5
1 1 2
4 4 1
3 3 3
*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
25 ms |
12568 KB |
Output is correct |
2 |
Correct |
32 ms |
14016 KB |
Output is correct |
3 |
Correct |
3 ms |
7420 KB |
Output is correct |
4 |
Correct |
3 ms |
7248 KB |
Output is correct |
5 |
Correct |
89 ms |
27848 KB |
Output is correct |
6 |
Correct |
90 ms |
31048 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
7488 KB |
Output is correct |
2 |
Correct |
48 ms |
18068 KB |
Output is correct |
3 |
Correct |
59 ms |
20404 KB |
Output is correct |
4 |
Correct |
25 ms |
12740 KB |
Output is correct |
5 |
Correct |
31 ms |
14020 KB |
Output is correct |
6 |
Correct |
2 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 |
28 ms |
12732 KB |
Output is correct |
13 |
Correct |
32 ms |
14008 KB |
Output is correct |
14 |
Correct |
35 ms |
12628 KB |
Output is correct |
15 |
Correct |
31 ms |
13500 KB |
Output is correct |
16 |
Correct |
26 ms |
12904 KB |
Output is correct |
17 |
Correct |
29 ms |
13252 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
7248 KB |
Output is correct |
2 |
Correct |
3 ms |
7484 KB |
Output is correct |
3 |
Correct |
23 ms |
14612 KB |
Output is correct |
4 |
Correct |
19 ms |
12332 KB |
Output is correct |
5 |
Correct |
46 ms |
20772 KB |
Output is correct |
6 |
Correct |
37 ms |
20040 KB |
Output is correct |
7 |
Correct |
46 ms |
20544 KB |
Output is correct |
8 |
Correct |
43 ms |
20552 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 |
7248 KB |
Output is correct |
4 |
Correct |
3 ms |
7248 KB |
Output is correct |
5 |
Correct |
3 ms |
7248 KB |
Output is correct |
6 |
Correct |
2 ms |
7248 KB |
Output is correct |
7 |
Correct |
3 ms |
7416 KB |
Output is correct |
8 |
Incorrect |
3 ms |
7416 KB |
1st lines differ - on the 1st token, expected: '2', found: '1' |
9 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
7248 KB |
Output is correct |
2 |
Correct |
3 ms |
7248 KB |
Output is correct |
3 |
Correct |
3 ms |
7248 KB |
Output is correct |
4 |
Correct |
3 ms |
7248 KB |
Output is correct |
5 |
Correct |
3 ms |
7248 KB |
Output is correct |
6 |
Correct |
2 ms |
7248 KB |
Output is correct |
7 |
Correct |
3 ms |
7416 KB |
Output is correct |
8 |
Incorrect |
3 ms |
7416 KB |
1st lines differ - on the 1st token, expected: '2', found: '1' |
9 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
7248 KB |
Output is correct |
2 |
Correct |
3 ms |
7248 KB |
Output is correct |
3 |
Correct |
3 ms |
7248 KB |
Output is correct |
4 |
Correct |
3 ms |
7248 KB |
Output is correct |
5 |
Correct |
3 ms |
7248 KB |
Output is correct |
6 |
Correct |
2 ms |
7248 KB |
Output is correct |
7 |
Correct |
3 ms |
7416 KB |
Output is correct |
8 |
Incorrect |
3 ms |
7416 KB |
1st lines differ - on the 1st token, expected: '2', found: '1' |
9 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
7248 KB |
Output is correct |
2 |
Correct |
3 ms |
7484 KB |
Output is correct |
3 |
Correct |
23 ms |
14612 KB |
Output is correct |
4 |
Correct |
19 ms |
12332 KB |
Output is correct |
5 |
Correct |
46 ms |
20772 KB |
Output is correct |
6 |
Correct |
37 ms |
20040 KB |
Output is correct |
7 |
Correct |
46 ms |
20544 KB |
Output is correct |
8 |
Correct |
43 ms |
20552 KB |
Output is correct |
9 |
Correct |
39 ms |
20556 KB |
Output is correct |
10 |
Incorrect |
36 ms |
16200 KB |
1st lines differ - on the 1st token, expected: '36454348383152', found: '36364983059693' |
11 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
25 ms |
12568 KB |
Output is correct |
2 |
Correct |
32 ms |
14016 KB |
Output is correct |
3 |
Correct |
3 ms |
7420 KB |
Output is correct |
4 |
Correct |
3 ms |
7248 KB |
Output is correct |
5 |
Correct |
89 ms |
27848 KB |
Output is correct |
6 |
Correct |
90 ms |
31048 KB |
Output is correct |
7 |
Correct |
2 ms |
7488 KB |
Output is correct |
8 |
Correct |
48 ms |
18068 KB |
Output is correct |
9 |
Correct |
59 ms |
20404 KB |
Output is correct |
10 |
Correct |
25 ms |
12740 KB |
Output is correct |
11 |
Correct |
31 ms |
14020 KB |
Output is correct |
12 |
Correct |
2 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 |
28 ms |
12732 KB |
Output is correct |
19 |
Correct |
32 ms |
14008 KB |
Output is correct |
20 |
Correct |
35 ms |
12628 KB |
Output is correct |
21 |
Correct |
31 ms |
13500 KB |
Output is correct |
22 |
Correct |
26 ms |
12904 KB |
Output is correct |
23 |
Correct |
29 ms |
13252 KB |
Output is correct |
24 |
Correct |
4 ms |
7248 KB |
Output is correct |
25 |
Correct |
3 ms |
7484 KB |
Output is correct |
26 |
Correct |
23 ms |
14612 KB |
Output is correct |
27 |
Correct |
19 ms |
12332 KB |
Output is correct |
28 |
Correct |
46 ms |
20772 KB |
Output is correct |
29 |
Correct |
37 ms |
20040 KB |
Output is correct |
30 |
Correct |
46 ms |
20544 KB |
Output is correct |
31 |
Correct |
43 ms |
20552 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 |
7248 KB |
Output is correct |
35 |
Correct |
3 ms |
7248 KB |
Output is correct |
36 |
Correct |
3 ms |
7248 KB |
Output is correct |
37 |
Correct |
2 ms |
7248 KB |
Output is correct |
38 |
Correct |
3 ms |
7416 KB |
Output is correct |
39 |
Incorrect |
3 ms |
7416 KB |
1st lines differ - on the 1st token, expected: '2', found: '1' |
40 |
Halted |
0 ms |
0 KB |
- |