Submission #532819

#TimeUsernameProblemLanguageResultExecution timeMemory
532819fhvirusWorst Reporter 4 (JOI21_worst_reporter4)C++17
0 / 100
10 ms10756 KiB
// 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; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...