#include "fish.h"
#include <bits/stdc++.h>
#define fr(i, a, b) for (int i = a; i <= b; i++)
#define rf(i, a, b) for (int i = a; i >= b; i--)
#define fe(x, y) for (auto& x : y)
#define fi first
#define se second
#define pb push_back
#define all(x) x.begin(), x.end()
#define pw(x) (1LL << (x))
#define sz(x) (int)x.size()
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
#define fbo find_by_order
#define ook order_of_key
template <typename T>
using oset = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
template <typename T>
using ve = vector<T>;
template <typename T>
bool umx(T& a, T b) {
return a < b ? a = b, 1 : 0;
}
template <typename T>
bool umn(T& a, T b) {
return a > b ? a = b, 1 : 0;
}
using ll = long long;
using ld = long double;
using pll = pair<ll, ll>;
using pii = pair<int, int>;
const int oo = 1e9;
const ll OO = 1e18;
const int N = 1e5 + 100;
const int M = 3000;
const int K = 110;
const int mod = 1e9 + 7;
const bool TEST = 1;
void precalc() {}
ve <ll> dp1[N]; // before the i-th is filled, back -- all filled
ve <ll> dp2[N]; // before the i-th is filled, back -- all filled
ve <ll> zero[N]; // i points is used
ve <pii> YY[N];
bool vs[M][M];
long long max_weights(int N, int M, vector<int> X, vector<int> Y, vector<int> W) {
// freopen("output.txt", "w", stdout);
// if(N <= 4e3) {
// for (int i = 0; i < M; i++) {
// vs[X[i]][Y[i]] = 1;
// }
// for (int i = 0; i < N; i++) {
// for (int j = 0; j < N; j++) {
// if (!vs[i][j]) {
// X.pb(i);
// Y.pb(j);
// W.pb(0);
// }
// }
// }
// }
for (int i = 0; i < sz(Y); i++) {
dp1[X[i]].pb(-OO);
dp2[X[i]].pb(-OO);
zero[X[i]].pb(-OO);
YY[X[i]].pb({Y[i], W[i]});
}
for (int i = 0; i < N; i++) {
sort(all(YY[i]));
dp1[i].pb(-OO);
dp2[i].pb(-OO);
zero[i].pb(-OO);
}
for (int i = 0; i < sz(dp1[0]); i++) {
dp1[0][i] = 0;
dp2[0][i] = 0;
zero[0][i] = 0;
}
for (int i = 1; i < N; i++) {
// from dp1
{ // to zero
int Z = 0;
ll S = 0;
for (int j = 0; j < sz(YY[i - 1]); j++) {
while (Z < sz(YY[i]) && YY[i - 1][j].fi - 1 >= YY[i][Z].fi) {
S += YY[i][Z].se;
Z++;
}
umx(zero[i][Z], dp1[i - 1][j] + S);
}
for (; Z < sz(YY[i]); Z++) S += YY[i][Z].se;
umx(zero[i].back(), dp1[i - 1].back() + S);
}
{ // to dp1
umx(dp1[i].back(), dp1[i - 1].back());
ll S = 0;
int ptr = sz(YY[i - 1]) - 1;
ll mx = dp1[i - 1].back();
for (int j = sz(YY[i]) - 1; j >= 0; j--) {
while(ptr >= 0 && YY[i - 1][ptr].fi - 1 >= YY[i][j].fi) {
mx = max(mx, dp1[i - 1][ptr] - S);
ptr--;
}
S += YY[i][j].se;
umx(dp1[i][j], mx + S);
}
}
// from zero
for (int j = 0; j <= sz(YY[i - 1]); j++) umx(zero[i][0], zero[i - 1][j]); // to zero
{ // to dp1 & dp2
{ // back
ll S = 0;
for (int j = sz(YY[i - 1]); j >= 0; j--) {
umx(dp1[i].back(), S + zero[i - 1][j]);
umx(dp2[i].back(), S + zero[i - 1][j]);
if (j - 1 >= 0) S += YY[i - 1][j - 1].se;
}
}
ll res = 0;
for (int j = 0; j <= sz(YY[i - 1]); j++) umx(res, zero[i - 1][j]);
ll S = 0;
ll mx = zero[i - 1][0];
int ptr = 0;
for (int j = 0; j < sz(YY[i]); j++) {
while (ptr < sz(YY[i - 1]) && YY[i - 1][ptr].fi <= YY[i][j].fi - 1) {
S += YY[i - 1][ptr].se;
ptr++;
umx (mx, zero[i - 1][ptr] - S);
}
umx(dp1[i][j], max(res, mx + S));
umx(dp2[i][j], max(res, mx + S));
}
}
// from dp2
{ // to zero
int Z = 0;
ll S = 0;
for (int j = 0; j < sz(YY[i - 1]); j++) {
while (Z < sz(YY[i]) && YY[i - 1][j].fi - 1 >= YY[i][Z].fi) {
S += YY[i][Z].se;
Z++;
}
umx(zero[i][Z], dp2[i - 1][j] + S);
}
for (; Z < sz(YY[i]); Z++) S += YY[i][Z].se;
umx(zero[i].back(), dp2[i - 1].back() + S);
}
{ // to dp2 & dp1
ll S = 0;
ll mx = 0;
int ptr = 0;
for (int j = 0; j < sz(YY[i]); j++) {
while (ptr < sz(YY[i - 1]) && YY[i - 1][ptr].fi <= YY[i][j].fi - 1) {
umx (mx, dp2[i - 1][ptr] - S);
S += YY[i - 1][ptr].se;
ptr++;
}
umx(dp2[i][j], mx + S);
umx(dp1[i][j], mx + S);
}
for (; ptr < sz(YY[i - 1]); ptr++) {
umx(mx, dp2[i - 1][ptr] - S);
S += YY[i - 1][ptr].se;
}
umx(mx, dp2[i - 1].back() - S);
umx(dp2[i].back(), mx + S);
umx(dp1[i].back(), mx + S);
}
}
// cout << zero[2][1] << endl;
ll ans = 0;
fe(x, dp1[N - 1]) umx(ans, x);
fe(x, dp2[N - 1]) umx(ans, x);
fe(x, zero[N - 1]) umx(ans, x);
return ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
42 ms |
22516 KB |
Output is correct |
2 |
Correct |
49 ms |
24536 KB |
Output is correct |
3 |
Correct |
17 ms |
19028 KB |
Output is correct |
4 |
Correct |
18 ms |
19040 KB |
Output is correct |
5 |
Correct |
115 ms |
35512 KB |
Output is correct |
6 |
Correct |
206 ms |
39092 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
9684 KB |
Output is correct |
2 |
Correct |
68 ms |
27020 KB |
Output is correct |
3 |
Correct |
83 ms |
30068 KB |
Output is correct |
4 |
Correct |
45 ms |
22564 KB |
Output is correct |
5 |
Correct |
48 ms |
24568 KB |
Output is correct |
6 |
Correct |
5 ms |
9684 KB |
Output is correct |
7 |
Correct |
4 ms |
9684 KB |
Output is correct |
8 |
Correct |
4 ms |
9684 KB |
Output is correct |
9 |
Correct |
4 ms |
9684 KB |
Output is correct |
10 |
Correct |
17 ms |
19028 KB |
Output is correct |
11 |
Correct |
17 ms |
19028 KB |
Output is correct |
12 |
Correct |
41 ms |
22640 KB |
Output is correct |
13 |
Correct |
49 ms |
24584 KB |
Output is correct |
14 |
Correct |
41 ms |
22812 KB |
Output is correct |
15 |
Correct |
46 ms |
24104 KB |
Output is correct |
16 |
Correct |
42 ms |
22636 KB |
Output is correct |
17 |
Correct |
49 ms |
24136 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
17 ms |
19108 KB |
Output is correct |
2 |
Correct |
17 ms |
18984 KB |
Output is correct |
3 |
Correct |
50 ms |
21012 KB |
Output is correct |
4 |
Correct |
38 ms |
21076 KB |
Output is correct |
5 |
Correct |
110 ms |
24588 KB |
Output is correct |
6 |
Correct |
113 ms |
24580 KB |
Output is correct |
7 |
Correct |
111 ms |
24572 KB |
Output is correct |
8 |
Correct |
87 ms |
24524 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
9684 KB |
Output is correct |
2 |
Correct |
4 ms |
9684 KB |
Output is correct |
3 |
Correct |
4 ms |
9684 KB |
Output is correct |
4 |
Correct |
4 ms |
9684 KB |
Output is correct |
5 |
Correct |
4 ms |
9684 KB |
Output is correct |
6 |
Correct |
4 ms |
9708 KB |
Output is correct |
7 |
Correct |
4 ms |
9684 KB |
Output is correct |
8 |
Correct |
4 ms |
9684 KB |
Output is correct |
9 |
Correct |
5 ms |
9764 KB |
Output is correct |
10 |
Correct |
7 ms |
9864 KB |
Output is correct |
11 |
Correct |
6 ms |
9684 KB |
Output is correct |
12 |
Correct |
5 ms |
9812 KB |
Output is correct |
13 |
Correct |
5 ms |
9684 KB |
Output is correct |
14 |
Correct |
6 ms |
9812 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
9684 KB |
Output is correct |
2 |
Correct |
4 ms |
9684 KB |
Output is correct |
3 |
Correct |
4 ms |
9684 KB |
Output is correct |
4 |
Correct |
4 ms |
9684 KB |
Output is correct |
5 |
Correct |
4 ms |
9684 KB |
Output is correct |
6 |
Correct |
4 ms |
9708 KB |
Output is correct |
7 |
Correct |
4 ms |
9684 KB |
Output is correct |
8 |
Correct |
4 ms |
9684 KB |
Output is correct |
9 |
Correct |
5 ms |
9764 KB |
Output is correct |
10 |
Correct |
7 ms |
9864 KB |
Output is correct |
11 |
Correct |
6 ms |
9684 KB |
Output is correct |
12 |
Correct |
5 ms |
9812 KB |
Output is correct |
13 |
Correct |
5 ms |
9684 KB |
Output is correct |
14 |
Correct |
6 ms |
9812 KB |
Output is correct |
15 |
Correct |
6 ms |
9712 KB |
Output is correct |
16 |
Correct |
6 ms |
9852 KB |
Output is correct |
17 |
Correct |
19 ms |
13200 KB |
Output is correct |
18 |
Correct |
19 ms |
14420 KB |
Output is correct |
19 |
Correct |
18 ms |
14200 KB |
Output is correct |
20 |
Correct |
23 ms |
14212 KB |
Output is correct |
21 |
Correct |
19 ms |
14076 KB |
Output is correct |
22 |
Correct |
33 ms |
18516 KB |
Output is correct |
23 |
Correct |
10 ms |
10452 KB |
Output is correct |
24 |
Correct |
15 ms |
12232 KB |
Output is correct |
25 |
Correct |
5 ms |
9812 KB |
Output is correct |
26 |
Correct |
8 ms |
10432 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
9684 KB |
Output is correct |
2 |
Correct |
4 ms |
9684 KB |
Output is correct |
3 |
Correct |
4 ms |
9684 KB |
Output is correct |
4 |
Correct |
4 ms |
9684 KB |
Output is correct |
5 |
Correct |
4 ms |
9684 KB |
Output is correct |
6 |
Correct |
4 ms |
9708 KB |
Output is correct |
7 |
Correct |
4 ms |
9684 KB |
Output is correct |
8 |
Correct |
4 ms |
9684 KB |
Output is correct |
9 |
Correct |
5 ms |
9764 KB |
Output is correct |
10 |
Correct |
7 ms |
9864 KB |
Output is correct |
11 |
Correct |
6 ms |
9684 KB |
Output is correct |
12 |
Correct |
5 ms |
9812 KB |
Output is correct |
13 |
Correct |
5 ms |
9684 KB |
Output is correct |
14 |
Correct |
6 ms |
9812 KB |
Output is correct |
15 |
Correct |
6 ms |
9712 KB |
Output is correct |
16 |
Correct |
6 ms |
9852 KB |
Output is correct |
17 |
Correct |
19 ms |
13200 KB |
Output is correct |
18 |
Correct |
19 ms |
14420 KB |
Output is correct |
19 |
Correct |
18 ms |
14200 KB |
Output is correct |
20 |
Correct |
23 ms |
14212 KB |
Output is correct |
21 |
Correct |
19 ms |
14076 KB |
Output is correct |
22 |
Correct |
33 ms |
18516 KB |
Output is correct |
23 |
Correct |
10 ms |
10452 KB |
Output is correct |
24 |
Correct |
15 ms |
12232 KB |
Output is correct |
25 |
Correct |
5 ms |
9812 KB |
Output is correct |
26 |
Correct |
8 ms |
10432 KB |
Output is correct |
27 |
Correct |
5 ms |
10068 KB |
Output is correct |
28 |
Correct |
78 ms |
31044 KB |
Output is correct |
29 |
Correct |
109 ms |
37300 KB |
Output is correct |
30 |
Correct |
126 ms |
35576 KB |
Output is correct |
31 |
Correct |
126 ms |
35612 KB |
Output is correct |
32 |
Correct |
118 ms |
40908 KB |
Output is correct |
33 |
Correct |
117 ms |
35608 KB |
Output is correct |
34 |
Correct |
114 ms |
35624 KB |
Output is correct |
35 |
Correct |
47 ms |
20640 KB |
Output is correct |
36 |
Correct |
114 ms |
38956 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
17 ms |
19108 KB |
Output is correct |
2 |
Correct |
17 ms |
18984 KB |
Output is correct |
3 |
Correct |
50 ms |
21012 KB |
Output is correct |
4 |
Correct |
38 ms |
21076 KB |
Output is correct |
5 |
Correct |
110 ms |
24588 KB |
Output is correct |
6 |
Correct |
113 ms |
24580 KB |
Output is correct |
7 |
Correct |
111 ms |
24572 KB |
Output is correct |
8 |
Correct |
87 ms |
24524 KB |
Output is correct |
9 |
Correct |
109 ms |
24580 KB |
Output is correct |
10 |
Correct |
64 ms |
21968 KB |
Output is correct |
11 |
Correct |
200 ms |
34260 KB |
Output is correct |
12 |
Correct |
4 ms |
9684 KB |
Output is correct |
13 |
Correct |
4 ms |
9684 KB |
Output is correct |
14 |
Correct |
5 ms |
9684 KB |
Output is correct |
15 |
Correct |
4 ms |
9684 KB |
Output is correct |
16 |
Correct |
4 ms |
9684 KB |
Output is correct |
17 |
Correct |
4 ms |
9684 KB |
Output is correct |
18 |
Correct |
25 ms |
19016 KB |
Output is correct |
19 |
Correct |
26 ms |
19016 KB |
Output is correct |
20 |
Correct |
21 ms |
19028 KB |
Output is correct |
21 |
Correct |
17 ms |
19084 KB |
Output is correct |
22 |
Correct |
89 ms |
25108 KB |
Output is correct |
23 |
Correct |
233 ms |
37764 KB |
Output is correct |
24 |
Correct |
173 ms |
38312 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
42 ms |
22516 KB |
Output is correct |
2 |
Correct |
49 ms |
24536 KB |
Output is correct |
3 |
Correct |
17 ms |
19028 KB |
Output is correct |
4 |
Correct |
18 ms |
19040 KB |
Output is correct |
5 |
Correct |
115 ms |
35512 KB |
Output is correct |
6 |
Correct |
206 ms |
39092 KB |
Output is correct |
7 |
Correct |
4 ms |
9684 KB |
Output is correct |
8 |
Correct |
68 ms |
27020 KB |
Output is correct |
9 |
Correct |
83 ms |
30068 KB |
Output is correct |
10 |
Correct |
45 ms |
22564 KB |
Output is correct |
11 |
Correct |
48 ms |
24568 KB |
Output is correct |
12 |
Correct |
5 ms |
9684 KB |
Output is correct |
13 |
Correct |
4 ms |
9684 KB |
Output is correct |
14 |
Correct |
4 ms |
9684 KB |
Output is correct |
15 |
Correct |
4 ms |
9684 KB |
Output is correct |
16 |
Correct |
17 ms |
19028 KB |
Output is correct |
17 |
Correct |
17 ms |
19028 KB |
Output is correct |
18 |
Correct |
41 ms |
22640 KB |
Output is correct |
19 |
Correct |
49 ms |
24584 KB |
Output is correct |
20 |
Correct |
41 ms |
22812 KB |
Output is correct |
21 |
Correct |
46 ms |
24104 KB |
Output is correct |
22 |
Correct |
42 ms |
22636 KB |
Output is correct |
23 |
Correct |
49 ms |
24136 KB |
Output is correct |
24 |
Correct |
17 ms |
19108 KB |
Output is correct |
25 |
Correct |
17 ms |
18984 KB |
Output is correct |
26 |
Correct |
50 ms |
21012 KB |
Output is correct |
27 |
Correct |
38 ms |
21076 KB |
Output is correct |
28 |
Correct |
110 ms |
24588 KB |
Output is correct |
29 |
Correct |
113 ms |
24580 KB |
Output is correct |
30 |
Correct |
111 ms |
24572 KB |
Output is correct |
31 |
Correct |
87 ms |
24524 KB |
Output is correct |
32 |
Correct |
4 ms |
9684 KB |
Output is correct |
33 |
Correct |
4 ms |
9684 KB |
Output is correct |
34 |
Correct |
4 ms |
9684 KB |
Output is correct |
35 |
Correct |
4 ms |
9684 KB |
Output is correct |
36 |
Correct |
4 ms |
9684 KB |
Output is correct |
37 |
Correct |
4 ms |
9708 KB |
Output is correct |
38 |
Correct |
4 ms |
9684 KB |
Output is correct |
39 |
Correct |
4 ms |
9684 KB |
Output is correct |
40 |
Correct |
5 ms |
9764 KB |
Output is correct |
41 |
Correct |
7 ms |
9864 KB |
Output is correct |
42 |
Correct |
6 ms |
9684 KB |
Output is correct |
43 |
Correct |
5 ms |
9812 KB |
Output is correct |
44 |
Correct |
5 ms |
9684 KB |
Output is correct |
45 |
Correct |
6 ms |
9812 KB |
Output is correct |
46 |
Correct |
6 ms |
9712 KB |
Output is correct |
47 |
Correct |
6 ms |
9852 KB |
Output is correct |
48 |
Correct |
19 ms |
13200 KB |
Output is correct |
49 |
Correct |
19 ms |
14420 KB |
Output is correct |
50 |
Correct |
18 ms |
14200 KB |
Output is correct |
51 |
Correct |
23 ms |
14212 KB |
Output is correct |
52 |
Correct |
19 ms |
14076 KB |
Output is correct |
53 |
Correct |
33 ms |
18516 KB |
Output is correct |
54 |
Correct |
10 ms |
10452 KB |
Output is correct |
55 |
Correct |
15 ms |
12232 KB |
Output is correct |
56 |
Correct |
5 ms |
9812 KB |
Output is correct |
57 |
Correct |
8 ms |
10432 KB |
Output is correct |
58 |
Correct |
5 ms |
10068 KB |
Output is correct |
59 |
Correct |
78 ms |
31044 KB |
Output is correct |
60 |
Correct |
109 ms |
37300 KB |
Output is correct |
61 |
Correct |
126 ms |
35576 KB |
Output is correct |
62 |
Correct |
126 ms |
35612 KB |
Output is correct |
63 |
Correct |
118 ms |
40908 KB |
Output is correct |
64 |
Correct |
117 ms |
35608 KB |
Output is correct |
65 |
Correct |
114 ms |
35624 KB |
Output is correct |
66 |
Correct |
47 ms |
20640 KB |
Output is correct |
67 |
Correct |
114 ms |
38956 KB |
Output is correct |
68 |
Correct |
109 ms |
24580 KB |
Output is correct |
69 |
Correct |
64 ms |
21968 KB |
Output is correct |
70 |
Correct |
200 ms |
34260 KB |
Output is correct |
71 |
Correct |
4 ms |
9684 KB |
Output is correct |
72 |
Correct |
4 ms |
9684 KB |
Output is correct |
73 |
Correct |
5 ms |
9684 KB |
Output is correct |
74 |
Correct |
4 ms |
9684 KB |
Output is correct |
75 |
Correct |
4 ms |
9684 KB |
Output is correct |
76 |
Correct |
4 ms |
9684 KB |
Output is correct |
77 |
Correct |
25 ms |
19016 KB |
Output is correct |
78 |
Correct |
26 ms |
19016 KB |
Output is correct |
79 |
Correct |
21 ms |
19028 KB |
Output is correct |
80 |
Correct |
17 ms |
19084 KB |
Output is correct |
81 |
Correct |
89 ms |
25108 KB |
Output is correct |
82 |
Correct |
233 ms |
37764 KB |
Output is correct |
83 |
Correct |
173 ms |
38312 KB |
Output is correct |
84 |
Correct |
143 ms |
41884 KB |
Output is correct |
85 |
Correct |
126 ms |
42268 KB |
Output is correct |
86 |
Correct |
274 ms |
45004 KB |
Output is correct |
87 |
Correct |
241 ms |
45728 KB |
Output is correct |
88 |
Correct |
6 ms |
9712 KB |
Output is correct |
89 |
Correct |
257 ms |
45788 KB |
Output is correct |
90 |
Correct |
181 ms |
44240 KB |
Output is correct |
91 |
Correct |
234 ms |
44508 KB |
Output is correct |