This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#define wiwihorz
#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#pragma GCC target("sse")
#pragma loop-opt(on)
#define rep(i, a, b) for(int i = a; i <= b; i ++)
#define rrep(i, a, b) for(int i = b; i >= a; i --)
#define all(x) x.begin(), x.end()
#define ceil(a, b) ((a + b - 1) / (b))
#define lld long double
#define pii pair<int, int>
#define random mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count())
#define INF 1000000000000000000
#define MOD 1000000007
#define eps (1e-9)
using namespace std;
#ifdef wiwihorz
#define print(a...)cerr<<"Line "<<__LINE__<<":",kout("["+string(#a)+"] = ", a)
void vprint(auto L,auto R){while(L<R)cerr<<*L<<" \n"[next(L) == R], ++L; }
void kout() { cerr << endl; }
template<class T1,class ... T2>void kout(T1 a,T2 ... e){cerr<<a<<" ",kout(e...);}
#else
#define print(...) 0
#define vprint(...) 0
#endif
#define x first
#define y second
namespace solver {
int n, m;
vector<vector<pii>> mp;
vector<multiset<int>> s;
vector<int> cnt;
void init_(int _n, int _m) {
n = _n, m = _m;
mp.assign(n + m + 1, vector<pii>());
s.assign(n + m + 1, multiset<int>());
cnt.assign(n + m + 1, 0);
}
void dfs(int x, int cost) {
for(auto i : mp[x]) {
dfs(i.x, i.y);
if(s[i.x].size() > s[x].size())
s[x].swap(s[i.x]);
cnt[x] += cnt[i.x];
for(auto j : s[i.x]) {
s[x].insert(j);
}
s[i.x].clear();
}
if(cost != -1) {
if(x > n) {
cnt[x] = 1;
s[x].insert(0);
s[x].insert(0);
}
while(cnt[x] > 1) {
cnt[x] --;
s[x].erase(prev(s[x].end()));
}
int L = *next(s[x].rbegin());
int R = *s[x].rbegin();
s[x].erase(prev(s[x].end()));
s[x].erase(prev(s[x].end()));
s[x].insert(L + cost);
s[x].insert(R + cost);
}
}
}
using namespace solver;
signed main() {
ios::sync_with_stdio(false), cin.tie(0);
int n, m;
cin >> n >> m;
init_(n, m);
int sum = 0;
rep(i, 2, n + m) {
int p, c;
cin >> p >> c;
sum += c;
mp[p].push_back({i, c});
}
dfs(1, -1);
int cur = cnt[1] - s[1].size(), pos = 0;
auto it = s[1].begin();
while(cur < 0) {
sum += cur * (*it - pos);
cur ++, pos = *it;
it = next(it);
}
cout << sum << "\n";
return 0;
}
Compilation message (stderr)
fireworks.cpp:5: warning: ignoring '#pragma loop ' [-Wunknown-pragmas]
5 | #pragma loop-opt(on)
|
fireworks.cpp:19:13: warning: use of 'auto' in parameter declaration only available with '-fconcepts-ts'
19 | void vprint(auto L,auto R){while(L<R)cerr<<*L<<" \n"[next(L) == R], ++L; }
| ^~~~
fireworks.cpp:19:20: warning: use of 'auto' in parameter declaration only available with '-fconcepts-ts'
19 | void vprint(auto L,auto R){while(L<R)cerr<<*L<<" \n"[next(L) == R], ++L; }
| ^~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |