// Knapsack DP is harder than FFT.
#include<bits/stdc++.h>
using namespace std;
typedef long long ll; typedef pair<int,int> pii;
#define ff first
#define ss second
#define pb emplace_back
#define AI(x) begin(x),end(x)
#ifdef OWO
#define debug(args...) SDF(#args, args)
#define OIU(args...) ostream& operator<<(ostream&O,args)
#define LKJ(S,B,E,F) template<class...T>OIU(S<T...>s){O<<B;int c=0;for(auto i:s)O<<(c++?", ":"")<<F;return O<<E;}
LKJ(vector,'[',']',i)LKJ(deque,'[',']',i)LKJ(set,'{','}',i)LKJ(multiset,'{','}',i)LKJ(unordered_set,'{','}',i)LKJ(map,'{','}',i.ff<<':'<<i.ss)LKJ(unordered_map,'{','}',i.ff<<':'<<i.ss)
template<class...T>void SDF(const char* s,T...a){int c=sizeof...(T);if(!c){cerr<<"\033[1;32mvoid\033[0m\n";return;}(cerr<<"\033[1;32m("<<s<<") = (",...,(cerr<<a<<(--c?", ":")\033[0m\n")));}
template<class T,size_t N>OIU(array<T,N>a){return O<<vector<T>(AI(a));}template<class...T>OIU(pair<T...>p){return O<<'('<<p.ff<<','<<p.ss<<')';}template<class...T>OIU(tuple<T...>t){return O<<'(',apply([&O](T...s){int c=0;(...,(O<<(c++?", ":"")<<s));},t),O<<')';}
#else
#define debug(...) ((void)0)
#endif
const int kN = 200002;
int N, A[kN], H[kN], C[kN];
map<int, ll> dp[kN];
void add(map<int, ll>& mp, int pos, ll val) {
mp[pos] += val;
while (val > 0) {
auto it = mp.lower_bound(pos);
if (it == begin(mp)) break;
it = prev(it);
ll d = min(val, it->ss);
val -= d;
it->ss -= d;
if (it->ss == 0) mp.erase(it);
}
}
void merge(map<int, ll>& a, map<int, ll>& b) {
if (a.size() < b.size()) swap(a, b);
for (const auto& [pos, val]: b)
a[pos] += val;
}
signed main() {
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
cin >> N;
for (int i = 1; i <= N; ++i)
cin >> A[i] >> H[i] >> C[i];
for (int i = N; i > 1; --i) {
add(dp[i], H[i], C[i]);
merge(dp[A[i]], dp[i]);
}
add(dp[1], H[1], C[1]);
ll ans = accumulate(C + 1, C + N + 1, 0ll);
for (const auto& [pos, val]: dp[1])
ans -= val;
cout << ans << '\n';
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
9676 KB |
Output is correct |
2 |
Correct |
6 ms |
9664 KB |
Output is correct |
3 |
Correct |
5 ms |
9604 KB |
Output is correct |
4 |
Correct |
5 ms |
9676 KB |
Output is correct |
5 |
Correct |
10 ms |
10888 KB |
Output is correct |
6 |
Correct |
8 ms |
10356 KB |
Output is correct |
7 |
Correct |
8 ms |
10232 KB |
Output is correct |
8 |
Correct |
11 ms |
11016 KB |
Output is correct |
9 |
Correct |
8 ms |
10444 KB |
Output is correct |
10 |
Correct |
7 ms |
10188 KB |
Output is correct |
11 |
Correct |
6 ms |
9992 KB |
Output is correct |
12 |
Correct |
8 ms |
9860 KB |
Output is correct |
13 |
Correct |
8 ms |
9856 KB |
Output is correct |
14 |
Correct |
8 ms |
9932 KB |
Output is correct |
15 |
Correct |
7 ms |
9872 KB |
Output is correct |
16 |
Correct |
13 ms |
11212 KB |
Output is correct |
17 |
Correct |
8 ms |
10628 KB |
Output is correct |
18 |
Correct |
8 ms |
9984 KB |
Output is correct |
19 |
Correct |
8 ms |
10188 KB |
Output is correct |
20 |
Correct |
8 ms |
10060 KB |
Output is correct |
21 |
Correct |
7 ms |
9988 KB |
Output is correct |
22 |
Correct |
8 ms |
10444 KB |
Output is correct |
23 |
Correct |
8 ms |
10120 KB |
Output is correct |
24 |
Correct |
9 ms |
10188 KB |
Output is correct |
25 |
Correct |
7 ms |
10060 KB |
Output is correct |
26 |
Correct |
7 ms |
9856 KB |
Output is correct |
27 |
Correct |
8 ms |
10376 KB |
Output is correct |
28 |
Correct |
8 ms |
10240 KB |
Output is correct |
29 |
Correct |
7 ms |
10188 KB |
Output is correct |
30 |
Correct |
8 ms |
10244 KB |
Output is correct |
31 |
Correct |
8 ms |
10248 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
9676 KB |
Output is correct |
2 |
Correct |
6 ms |
9664 KB |
Output is correct |
3 |
Correct |
5 ms |
9604 KB |
Output is correct |
4 |
Correct |
5 ms |
9676 KB |
Output is correct |
5 |
Correct |
10 ms |
10888 KB |
Output is correct |
6 |
Correct |
8 ms |
10356 KB |
Output is correct |
7 |
Correct |
8 ms |
10232 KB |
Output is correct |
8 |
Correct |
11 ms |
11016 KB |
Output is correct |
9 |
Correct |
8 ms |
10444 KB |
Output is correct |
10 |
Correct |
7 ms |
10188 KB |
Output is correct |
11 |
Correct |
6 ms |
9992 KB |
Output is correct |
12 |
Correct |
8 ms |
9860 KB |
Output is correct |
13 |
Correct |
8 ms |
9856 KB |
Output is correct |
14 |
Correct |
8 ms |
9932 KB |
Output is correct |
15 |
Correct |
7 ms |
9872 KB |
Output is correct |
16 |
Correct |
13 ms |
11212 KB |
Output is correct |
17 |
Correct |
8 ms |
10628 KB |
Output is correct |
18 |
Correct |
8 ms |
9984 KB |
Output is correct |
19 |
Correct |
8 ms |
10188 KB |
Output is correct |
20 |
Correct |
8 ms |
10060 KB |
Output is correct |
21 |
Correct |
7 ms |
9988 KB |
Output is correct |
22 |
Correct |
8 ms |
10444 KB |
Output is correct |
23 |
Correct |
8 ms |
10120 KB |
Output is correct |
24 |
Correct |
9 ms |
10188 KB |
Output is correct |
25 |
Correct |
7 ms |
10060 KB |
Output is correct |
26 |
Correct |
7 ms |
9856 KB |
Output is correct |
27 |
Correct |
8 ms |
10376 KB |
Output is correct |
28 |
Correct |
8 ms |
10240 KB |
Output is correct |
29 |
Correct |
7 ms |
10188 KB |
Output is correct |
30 |
Correct |
8 ms |
10244 KB |
Output is correct |
31 |
Correct |
8 ms |
10248 KB |
Output is correct |
32 |
Correct |
10 ms |
10956 KB |
Output is correct |
33 |
Correct |
326 ms |
83224 KB |
Output is correct |
34 |
Correct |
241 ms |
53324 KB |
Output is correct |
35 |
Correct |
296 ms |
81560 KB |
Output is correct |
36 |
Correct |
253 ms |
53160 KB |
Output is correct |
37 |
Correct |
125 ms |
28540 KB |
Output is correct |
38 |
Correct |
88 ms |
23052 KB |
Output is correct |
39 |
Correct |
118 ms |
17364 KB |
Output is correct |
40 |
Correct |
96 ms |
17144 KB |
Output is correct |
41 |
Correct |
71 ms |
17184 KB |
Output is correct |
42 |
Correct |
132 ms |
17372 KB |
Output is correct |
43 |
Correct |
102 ms |
17156 KB |
Output is correct |
44 |
Correct |
277 ms |
96592 KB |
Output is correct |
45 |
Correct |
190 ms |
60868 KB |
Output is correct |
46 |
Correct |
71 ms |
23284 KB |
Output is correct |
47 |
Correct |
178 ms |
30308 KB |
Output is correct |
48 |
Correct |
93 ms |
23360 KB |
Output is correct |
49 |
Correct |
73 ms |
23308 KB |
Output is correct |
50 |
Correct |
207 ms |
41332 KB |
Output is correct |
51 |
Correct |
85 ms |
28960 KB |
Output is correct |
52 |
Correct |
213 ms |
30648 KB |
Output is correct |
53 |
Correct |
88 ms |
23492 KB |
Output is correct |
54 |
Correct |
83 ms |
17288 KB |
Output is correct |
55 |
Correct |
142 ms |
34440 KB |
Output is correct |
56 |
Correct |
122 ms |
29360 KB |
Output is correct |
57 |
Correct |
137 ms |
28756 KB |
Output is correct |
58 |
Correct |
146 ms |
35356 KB |
Output is correct |
59 |
Correct |
146 ms |
35140 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
9676 KB |
Output is correct |
2 |
Correct |
6 ms |
9664 KB |
Output is correct |
3 |
Correct |
5 ms |
9604 KB |
Output is correct |
4 |
Correct |
5 ms |
9676 KB |
Output is correct |
5 |
Correct |
10 ms |
10888 KB |
Output is correct |
6 |
Correct |
8 ms |
10356 KB |
Output is correct |
7 |
Correct |
8 ms |
10232 KB |
Output is correct |
8 |
Correct |
11 ms |
11016 KB |
Output is correct |
9 |
Correct |
8 ms |
10444 KB |
Output is correct |
10 |
Correct |
7 ms |
10188 KB |
Output is correct |
11 |
Correct |
6 ms |
9992 KB |
Output is correct |
12 |
Correct |
8 ms |
9860 KB |
Output is correct |
13 |
Correct |
8 ms |
9856 KB |
Output is correct |
14 |
Correct |
8 ms |
9932 KB |
Output is correct |
15 |
Correct |
7 ms |
9872 KB |
Output is correct |
16 |
Correct |
13 ms |
11212 KB |
Output is correct |
17 |
Correct |
8 ms |
10628 KB |
Output is correct |
18 |
Correct |
8 ms |
9984 KB |
Output is correct |
19 |
Correct |
8 ms |
10188 KB |
Output is correct |
20 |
Correct |
8 ms |
10060 KB |
Output is correct |
21 |
Correct |
7 ms |
9988 KB |
Output is correct |
22 |
Correct |
8 ms |
10444 KB |
Output is correct |
23 |
Correct |
8 ms |
10120 KB |
Output is correct |
24 |
Correct |
9 ms |
10188 KB |
Output is correct |
25 |
Correct |
7 ms |
10060 KB |
Output is correct |
26 |
Correct |
7 ms |
9856 KB |
Output is correct |
27 |
Correct |
8 ms |
10376 KB |
Output is correct |
28 |
Correct |
8 ms |
10240 KB |
Output is correct |
29 |
Correct |
7 ms |
10188 KB |
Output is correct |
30 |
Correct |
8 ms |
10244 KB |
Output is correct |
31 |
Correct |
8 ms |
10248 KB |
Output is correct |
32 |
Correct |
10 ms |
10956 KB |
Output is correct |
33 |
Correct |
326 ms |
83224 KB |
Output is correct |
34 |
Correct |
241 ms |
53324 KB |
Output is correct |
35 |
Correct |
296 ms |
81560 KB |
Output is correct |
36 |
Correct |
253 ms |
53160 KB |
Output is correct |
37 |
Correct |
125 ms |
28540 KB |
Output is correct |
38 |
Correct |
88 ms |
23052 KB |
Output is correct |
39 |
Correct |
118 ms |
17364 KB |
Output is correct |
40 |
Correct |
96 ms |
17144 KB |
Output is correct |
41 |
Correct |
71 ms |
17184 KB |
Output is correct |
42 |
Correct |
132 ms |
17372 KB |
Output is correct |
43 |
Correct |
102 ms |
17156 KB |
Output is correct |
44 |
Correct |
277 ms |
96592 KB |
Output is correct |
45 |
Correct |
190 ms |
60868 KB |
Output is correct |
46 |
Correct |
71 ms |
23284 KB |
Output is correct |
47 |
Correct |
178 ms |
30308 KB |
Output is correct |
48 |
Correct |
93 ms |
23360 KB |
Output is correct |
49 |
Correct |
73 ms |
23308 KB |
Output is correct |
50 |
Correct |
207 ms |
41332 KB |
Output is correct |
51 |
Correct |
85 ms |
28960 KB |
Output is correct |
52 |
Correct |
213 ms |
30648 KB |
Output is correct |
53 |
Correct |
88 ms |
23492 KB |
Output is correct |
54 |
Correct |
83 ms |
17288 KB |
Output is correct |
55 |
Correct |
142 ms |
34440 KB |
Output is correct |
56 |
Correct |
122 ms |
29360 KB |
Output is correct |
57 |
Correct |
137 ms |
28756 KB |
Output is correct |
58 |
Correct |
146 ms |
35356 KB |
Output is correct |
59 |
Correct |
146 ms |
35140 KB |
Output is correct |
60 |
Correct |
5 ms |
9720 KB |
Output is correct |
61 |
Incorrect |
4 ms |
9676 KB |
Output isn't correct |
62 |
Halted |
0 ms |
0 KB |
- |