제출 #650688

#제출 시각아이디문제언어결과실행 시간메모리
650688TobFireworks (APIO16_fireworks)C++14
100 / 100
498 ms113228 KiB
#include <bits/stdc++.h> #define ll long long #define F first #define S second #define all(x) x.begin(), x.end() #define pb push_back #define FIO ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0) using namespace std; ll nxt() {ll num; cin >> num; return num;} typedef pair <ll, ll> pii; const int N = 300007; int n, m; ll nag[N]; vector <pii> adj[N]; multiset <ll> dfs(int x) { if (adj[x].empty()) return {}; multiset <ll> s, tmp; for (auto it : adj[x]) { pii xx = {0, 0}; tmp = dfs(it.F); auto p = tmp.end(); if (!tmp.empty()) --p; ll nn = nag[it.F]; nag[x] += nn; //MODIFY for (int i = tmp.size(); i >= nn; i--) { if (i == nn + 1) xx.S = *p; if (i == nn) xx.F = *p; p = tmp.erase(p); if (!tmp.empty()) --p; } tmp.insert(xx.F + it.S); tmp.insert(xx.S + it.S); //COMBINE if (s.size() < tmp.size()) swap(s, tmp); for (auto itt : tmp) s.insert(itt); } return s; } int main () { cin >> n >> m; ll poc = 0, nagib = -m, res, la = 0; for (int i = 2; i <= n+m; i++) { ll p, c; cin >> p >> c; poc += c; adj[p].pb({i, c}); if (i > n) nag[i] = 1; } res = poc; multiset <ll> s = dfs(1); for (auto it : s) { poc += nagib * (it - la); res = min(res, poc); nagib++; la = it; } cout << res; return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...