// 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, int> dp[kN];
void add(map<int, int>& mp, int pos, int val) {
mp[pos] += val;
while (val > 0) {
auto it = mp.lower_bound(pos);
if (it == begin(mp)) break;
it = prev(it);
int d = min(val, it->ss);
val -= d;
it->ss -= d;
if (it->ss == 0) mp.erase(it);
}
}
void merge(map<int, int>& a, map<int, int>& 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;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
9728 KB |
Output is correct |
2 |
Correct |
5 ms |
9676 KB |
Output is correct |
3 |
Correct |
5 ms |
9712 KB |
Output is correct |
4 |
Correct |
5 ms |
9676 KB |
Output is correct |
5 |
Correct |
10 ms |
10756 KB |
Output is correct |
6 |
Incorrect |
10 ms |
10228 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
9728 KB |
Output is correct |
2 |
Correct |
5 ms |
9676 KB |
Output is correct |
3 |
Correct |
5 ms |
9712 KB |
Output is correct |
4 |
Correct |
5 ms |
9676 KB |
Output is correct |
5 |
Correct |
10 ms |
10756 KB |
Output is correct |
6 |
Incorrect |
10 ms |
10228 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
9728 KB |
Output is correct |
2 |
Correct |
5 ms |
9676 KB |
Output is correct |
3 |
Correct |
5 ms |
9712 KB |
Output is correct |
4 |
Correct |
5 ms |
9676 KB |
Output is correct |
5 |
Correct |
10 ms |
10756 KB |
Output is correct |
6 |
Incorrect |
10 ms |
10228 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |