/**
* Author: Nicholas Winschel
* Created: 05.09.2023 22:40:58
**/
// screw this problem man. had [most of] the main insights almost immediately, couldn't work out the implementation details (i am allergic to 2 ptrs)
// for over 2 days. just gave up, checked online and someone had a 50 line solution that did pretty much exactly what I expected
// i'm not mad, you're mad.
#include <bits/stdc++.h>
using namespace std;
using ll=long long;
using db=long double;
template<class T> using V=vector<T>;
using vi = V<int>;
using vl = V<ll>;
using pi = pair<int,int>;
#define f first
#define s second
#define sz(x) (int)((x).size())
#include "fish.h"
// THIS IMPL IS NOT ORIGINAL.
const int MAXN = 1e5+5;
const ll inf = 1e18;
map<int, ll> c[MAXN], blo[MAXN];
vl dp[MAXN];
vi pos[MAXN];
V<pair<int,ll>> pre[MAXN];
ll lsum(int ind, int ub) {
auto i = lower_bound(pre[ind].begin(),pre[ind].end(), pair<int,ll>{ub+1, -inf});
if (i == pre[ind].begin()) return 0;
return (--i)->second;
}
long long max_weights(int N, int M, vi X, vi Y, vi W) {
for (int i = 0; i < M; i++)
c[X[i]+1][Y[i]+1] += W[i], pos[X[i]+1].push_back(Y[i]);
for (int i = 1; i <= N; i++) {
sort(pos[i].begin(),pos[i].end()); pos[i].push_back(N);
ll tot = 0;
for (auto [po, val] : c[i]) tot += val, pre[i].emplace_back(po,tot);
}
for (int j : pos[1]) blo[1][j]=0;
dp[1]=vl(sz(pos[1]));
for (int i = 2; i <= N; i++) {
dp[i].resize(sz(pos[i]));
// handle case where left is greater
int l = pos[i-1].size()-1;
ll bsf = -inf;
for (int r = sz(pos[i]) - 1; r >= 0; r--) {
while (l >= 0 && pos[i-1][l] >= pos[i][r])
bsf = max(bsf, dp[i-1][l] + lsum(i, pos[i-1][l])),--l;
dp[i][r]=bsf-lsum(i,pos[i][r]);
}
auto it = blo[i-1].begin();
bsf=-inf;
for (int r = 0; r < sz(pos[i]); r++) {
while (it != blo[i-1].end() && it->f <= pos[i][r])
bsf = max(bsf, it->s - lsum(i-1, it->f)),++it;
dp[i][r]=max(dp[i][r],
blo[i][pos[i][r]]= bsf + lsum(i-1,pos[i][r])); // why does this work again?
}
for (int j = 0; j < sz(pos[i-1]); j++)
blo[i][pos[i-1][j]] = max(blo[i][pos[i-1][j]], dp[i-1][j] + lsum(i,pos[i-1][j])); // OH
}
ll ret = 0;
for (ll v : dp[N]) ret = max(ret, v);
return ret;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
135 ms |
48412 KB |
Output is correct |
2 |
Correct |
194 ms |
54268 KB |
Output is correct |
3 |
Correct |
28 ms |
29268 KB |
Output is correct |
4 |
Correct |
27 ms |
29248 KB |
Output is correct |
5 |
Correct |
617 ms |
102028 KB |
Output is correct |
6 |
Correct |
463 ms |
103608 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
16724 KB |
Output is correct |
2 |
Correct |
278 ms |
63192 KB |
Output is correct |
3 |
Correct |
350 ms |
71876 KB |
Output is correct |
4 |
Correct |
139 ms |
48340 KB |
Output is correct |
5 |
Correct |
175 ms |
54316 KB |
Output is correct |
6 |
Correct |
9 ms |
16724 KB |
Output is correct |
7 |
Correct |
9 ms |
16736 KB |
Output is correct |
8 |
Correct |
10 ms |
16716 KB |
Output is correct |
9 |
Correct |
8 ms |
16632 KB |
Output is correct |
10 |
Correct |
29 ms |
29188 KB |
Output is correct |
11 |
Correct |
30 ms |
29248 KB |
Output is correct |
12 |
Correct |
138 ms |
48508 KB |
Output is correct |
13 |
Correct |
173 ms |
54240 KB |
Output is correct |
14 |
Correct |
151 ms |
48420 KB |
Output is correct |
15 |
Correct |
154 ms |
49308 KB |
Output is correct |
16 |
Correct |
135 ms |
48552 KB |
Output is correct |
17 |
Correct |
157 ms |
51824 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
26 ms |
29252 KB |
Output is correct |
2 |
Correct |
28 ms |
29268 KB |
Output is correct |
3 |
Correct |
71 ms |
38840 KB |
Output is correct |
4 |
Correct |
69 ms |
36728 KB |
Output is correct |
5 |
Correct |
136 ms |
48844 KB |
Output is correct |
6 |
Correct |
110 ms |
48124 KB |
Output is correct |
7 |
Correct |
118 ms |
48792 KB |
Output is correct |
8 |
Correct |
122 ms |
48804 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
16724 KB |
Output is correct |
2 |
Correct |
10 ms |
16692 KB |
Output is correct |
3 |
Correct |
8 ms |
16732 KB |
Output is correct |
4 |
Correct |
11 ms |
16744 KB |
Output is correct |
5 |
Correct |
11 ms |
16724 KB |
Output is correct |
6 |
Correct |
10 ms |
16728 KB |
Output is correct |
7 |
Correct |
8 ms |
16740 KB |
Output is correct |
8 |
Correct |
10 ms |
16672 KB |
Output is correct |
9 |
Correct |
9 ms |
16820 KB |
Output is correct |
10 |
Correct |
12 ms |
17268 KB |
Output is correct |
11 |
Correct |
9 ms |
16852 KB |
Output is correct |
12 |
Correct |
9 ms |
16980 KB |
Output is correct |
13 |
Correct |
8 ms |
16724 KB |
Output is correct |
14 |
Correct |
9 ms |
16980 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
16724 KB |
Output is correct |
2 |
Correct |
10 ms |
16692 KB |
Output is correct |
3 |
Correct |
8 ms |
16732 KB |
Output is correct |
4 |
Correct |
11 ms |
16744 KB |
Output is correct |
5 |
Correct |
11 ms |
16724 KB |
Output is correct |
6 |
Correct |
10 ms |
16728 KB |
Output is correct |
7 |
Correct |
8 ms |
16740 KB |
Output is correct |
8 |
Correct |
10 ms |
16672 KB |
Output is correct |
9 |
Correct |
9 ms |
16820 KB |
Output is correct |
10 |
Correct |
12 ms |
17268 KB |
Output is correct |
11 |
Correct |
9 ms |
16852 KB |
Output is correct |
12 |
Correct |
9 ms |
16980 KB |
Output is correct |
13 |
Correct |
8 ms |
16724 KB |
Output is correct |
14 |
Correct |
9 ms |
16980 KB |
Output is correct |
15 |
Correct |
10 ms |
16852 KB |
Output is correct |
16 |
Correct |
11 ms |
17108 KB |
Output is correct |
17 |
Correct |
57 ms |
26316 KB |
Output is correct |
18 |
Correct |
55 ms |
26444 KB |
Output is correct |
19 |
Correct |
56 ms |
26344 KB |
Output is correct |
20 |
Correct |
54 ms |
26208 KB |
Output is correct |
21 |
Correct |
49 ms |
26060 KB |
Output is correct |
22 |
Correct |
101 ms |
35304 KB |
Output is correct |
23 |
Correct |
18 ms |
19056 KB |
Output is correct |
24 |
Correct |
43 ms |
23884 KB |
Output is correct |
25 |
Correct |
10 ms |
16964 KB |
Output is correct |
26 |
Correct |
18 ms |
18792 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
16724 KB |
Output is correct |
2 |
Correct |
10 ms |
16692 KB |
Output is correct |
3 |
Correct |
8 ms |
16732 KB |
Output is correct |
4 |
Correct |
11 ms |
16744 KB |
Output is correct |
5 |
Correct |
11 ms |
16724 KB |
Output is correct |
6 |
Correct |
10 ms |
16728 KB |
Output is correct |
7 |
Correct |
8 ms |
16740 KB |
Output is correct |
8 |
Correct |
10 ms |
16672 KB |
Output is correct |
9 |
Correct |
9 ms |
16820 KB |
Output is correct |
10 |
Correct |
12 ms |
17268 KB |
Output is correct |
11 |
Correct |
9 ms |
16852 KB |
Output is correct |
12 |
Correct |
9 ms |
16980 KB |
Output is correct |
13 |
Correct |
8 ms |
16724 KB |
Output is correct |
14 |
Correct |
9 ms |
16980 KB |
Output is correct |
15 |
Correct |
10 ms |
16852 KB |
Output is correct |
16 |
Correct |
11 ms |
17108 KB |
Output is correct |
17 |
Correct |
57 ms |
26316 KB |
Output is correct |
18 |
Correct |
55 ms |
26444 KB |
Output is correct |
19 |
Correct |
56 ms |
26344 KB |
Output is correct |
20 |
Correct |
54 ms |
26208 KB |
Output is correct |
21 |
Correct |
49 ms |
26060 KB |
Output is correct |
22 |
Correct |
101 ms |
35304 KB |
Output is correct |
23 |
Correct |
18 ms |
19056 KB |
Output is correct |
24 |
Correct |
43 ms |
23884 KB |
Output is correct |
25 |
Correct |
10 ms |
16964 KB |
Output is correct |
26 |
Correct |
18 ms |
18792 KB |
Output is correct |
27 |
Correct |
10 ms |
17876 KB |
Output is correct |
28 |
Correct |
317 ms |
63280 KB |
Output is correct |
29 |
Correct |
508 ms |
83432 KB |
Output is correct |
30 |
Correct |
530 ms |
95176 KB |
Output is correct |
31 |
Correct |
494 ms |
94964 KB |
Output is correct |
32 |
Correct |
419 ms |
77848 KB |
Output is correct |
33 |
Correct |
502 ms |
95548 KB |
Output is correct |
34 |
Correct |
439 ms |
95764 KB |
Output is correct |
35 |
Correct |
155 ms |
45900 KB |
Output is correct |
36 |
Correct |
465 ms |
93924 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
26 ms |
29252 KB |
Output is correct |
2 |
Correct |
28 ms |
29268 KB |
Output is correct |
3 |
Correct |
71 ms |
38840 KB |
Output is correct |
4 |
Correct |
69 ms |
36728 KB |
Output is correct |
5 |
Correct |
136 ms |
48844 KB |
Output is correct |
6 |
Correct |
110 ms |
48124 KB |
Output is correct |
7 |
Correct |
118 ms |
48792 KB |
Output is correct |
8 |
Correct |
122 ms |
48804 KB |
Output is correct |
9 |
Correct |
133 ms |
54816 KB |
Output is correct |
10 |
Correct |
105 ms |
41932 KB |
Output is correct |
11 |
Correct |
225 ms |
67112 KB |
Output is correct |
12 |
Correct |
8 ms |
16680 KB |
Output is correct |
13 |
Correct |
9 ms |
16724 KB |
Output is correct |
14 |
Correct |
9 ms |
16672 KB |
Output is correct |
15 |
Correct |
8 ms |
16740 KB |
Output is correct |
16 |
Correct |
9 ms |
16744 KB |
Output is correct |
17 |
Correct |
8 ms |
16740 KB |
Output is correct |
18 |
Correct |
28 ms |
29268 KB |
Output is correct |
19 |
Correct |
27 ms |
29248 KB |
Output is correct |
20 |
Correct |
28 ms |
29252 KB |
Output is correct |
21 |
Correct |
28 ms |
29160 KB |
Output is correct |
22 |
Correct |
153 ms |
55036 KB |
Output is correct |
23 |
Correct |
249 ms |
73512 KB |
Output is correct |
24 |
Correct |
246 ms |
73996 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
135 ms |
48412 KB |
Output is correct |
2 |
Correct |
194 ms |
54268 KB |
Output is correct |
3 |
Correct |
28 ms |
29268 KB |
Output is correct |
4 |
Correct |
27 ms |
29248 KB |
Output is correct |
5 |
Correct |
617 ms |
102028 KB |
Output is correct |
6 |
Correct |
463 ms |
103608 KB |
Output is correct |
7 |
Correct |
9 ms |
16724 KB |
Output is correct |
8 |
Correct |
278 ms |
63192 KB |
Output is correct |
9 |
Correct |
350 ms |
71876 KB |
Output is correct |
10 |
Correct |
139 ms |
48340 KB |
Output is correct |
11 |
Correct |
175 ms |
54316 KB |
Output is correct |
12 |
Correct |
9 ms |
16724 KB |
Output is correct |
13 |
Correct |
9 ms |
16736 KB |
Output is correct |
14 |
Correct |
10 ms |
16716 KB |
Output is correct |
15 |
Correct |
8 ms |
16632 KB |
Output is correct |
16 |
Correct |
29 ms |
29188 KB |
Output is correct |
17 |
Correct |
30 ms |
29248 KB |
Output is correct |
18 |
Correct |
138 ms |
48508 KB |
Output is correct |
19 |
Correct |
173 ms |
54240 KB |
Output is correct |
20 |
Correct |
151 ms |
48420 KB |
Output is correct |
21 |
Correct |
154 ms |
49308 KB |
Output is correct |
22 |
Correct |
135 ms |
48552 KB |
Output is correct |
23 |
Correct |
157 ms |
51824 KB |
Output is correct |
24 |
Correct |
26 ms |
29252 KB |
Output is correct |
25 |
Correct |
28 ms |
29268 KB |
Output is correct |
26 |
Correct |
71 ms |
38840 KB |
Output is correct |
27 |
Correct |
69 ms |
36728 KB |
Output is correct |
28 |
Correct |
136 ms |
48844 KB |
Output is correct |
29 |
Correct |
110 ms |
48124 KB |
Output is correct |
30 |
Correct |
118 ms |
48792 KB |
Output is correct |
31 |
Correct |
122 ms |
48804 KB |
Output is correct |
32 |
Correct |
8 ms |
16724 KB |
Output is correct |
33 |
Correct |
10 ms |
16692 KB |
Output is correct |
34 |
Correct |
8 ms |
16732 KB |
Output is correct |
35 |
Correct |
11 ms |
16744 KB |
Output is correct |
36 |
Correct |
11 ms |
16724 KB |
Output is correct |
37 |
Correct |
10 ms |
16728 KB |
Output is correct |
38 |
Correct |
8 ms |
16740 KB |
Output is correct |
39 |
Correct |
10 ms |
16672 KB |
Output is correct |
40 |
Correct |
9 ms |
16820 KB |
Output is correct |
41 |
Correct |
12 ms |
17268 KB |
Output is correct |
42 |
Correct |
9 ms |
16852 KB |
Output is correct |
43 |
Correct |
9 ms |
16980 KB |
Output is correct |
44 |
Correct |
8 ms |
16724 KB |
Output is correct |
45 |
Correct |
9 ms |
16980 KB |
Output is correct |
46 |
Correct |
10 ms |
16852 KB |
Output is correct |
47 |
Correct |
11 ms |
17108 KB |
Output is correct |
48 |
Correct |
57 ms |
26316 KB |
Output is correct |
49 |
Correct |
55 ms |
26444 KB |
Output is correct |
50 |
Correct |
56 ms |
26344 KB |
Output is correct |
51 |
Correct |
54 ms |
26208 KB |
Output is correct |
52 |
Correct |
49 ms |
26060 KB |
Output is correct |
53 |
Correct |
101 ms |
35304 KB |
Output is correct |
54 |
Correct |
18 ms |
19056 KB |
Output is correct |
55 |
Correct |
43 ms |
23884 KB |
Output is correct |
56 |
Correct |
10 ms |
16964 KB |
Output is correct |
57 |
Correct |
18 ms |
18792 KB |
Output is correct |
58 |
Correct |
10 ms |
17876 KB |
Output is correct |
59 |
Correct |
317 ms |
63280 KB |
Output is correct |
60 |
Correct |
508 ms |
83432 KB |
Output is correct |
61 |
Correct |
530 ms |
95176 KB |
Output is correct |
62 |
Correct |
494 ms |
94964 KB |
Output is correct |
63 |
Correct |
419 ms |
77848 KB |
Output is correct |
64 |
Correct |
502 ms |
95548 KB |
Output is correct |
65 |
Correct |
439 ms |
95764 KB |
Output is correct |
66 |
Correct |
155 ms |
45900 KB |
Output is correct |
67 |
Correct |
465 ms |
93924 KB |
Output is correct |
68 |
Correct |
133 ms |
54816 KB |
Output is correct |
69 |
Correct |
105 ms |
41932 KB |
Output is correct |
70 |
Correct |
225 ms |
67112 KB |
Output is correct |
71 |
Correct |
8 ms |
16680 KB |
Output is correct |
72 |
Correct |
9 ms |
16724 KB |
Output is correct |
73 |
Correct |
9 ms |
16672 KB |
Output is correct |
74 |
Correct |
8 ms |
16740 KB |
Output is correct |
75 |
Correct |
9 ms |
16744 KB |
Output is correct |
76 |
Correct |
8 ms |
16740 KB |
Output is correct |
77 |
Correct |
28 ms |
29268 KB |
Output is correct |
78 |
Correct |
27 ms |
29248 KB |
Output is correct |
79 |
Correct |
28 ms |
29252 KB |
Output is correct |
80 |
Correct |
28 ms |
29160 KB |
Output is correct |
81 |
Correct |
153 ms |
55036 KB |
Output is correct |
82 |
Correct |
249 ms |
73512 KB |
Output is correct |
83 |
Correct |
246 ms |
73996 KB |
Output is correct |
84 |
Correct |
544 ms |
102780 KB |
Output is correct |
85 |
Correct |
592 ms |
103428 KB |
Output is correct |
86 |
Correct |
421 ms |
106148 KB |
Output is correct |
87 |
Correct |
447 ms |
107996 KB |
Output is correct |
88 |
Correct |
8 ms |
16724 KB |
Output is correct |
89 |
Correct |
489 ms |
108180 KB |
Output is correct |
90 |
Correct |
427 ms |
102412 KB |
Output is correct |
91 |
Correct |
397 ms |
93716 KB |
Output is correct |