#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define debug(x) cerr << #x << " " << x << "\n"
#define debugs(x) cerr << #x << " " << x << " "
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
typedef pair <ll, ll> pii;
typedef pair <long double, pii> muchie;
const ll NMAX = 100001;
const ll VMAX = 1000001;
const ll INF = (1LL << 59);
const ll MOD = 998244353;
const ll BLOCK = 318;
const ll base = 31;
const ll nr_of_bits = 21;
vector <pii> v[NMAX];
int n, m;
multiset <int> st[NMAX];
int slope[NMAX];
int schema[NMAX];
void shift(int node, int d) {
int act = slope[node];
int last = 0;
while(act > 0) {
auto it = st[node].rbegin();
last = *it;
st[node].erase(st[node].find(*it));
act--;
}
int celalalt = *st[node].rbegin();
st[node].insert(last + d);
st[node].insert(celalalt + d);
slope[node] = 1;
}
void merge(multiset <int> &a, multiset <int> &b) {
if(a.size() < b.size()) {
swap(a, b);
}
for(auto x : b) {
a.insert(x);
}
b.clear();
}
void DFS(int node, int p) {
if(v[node].size() == 1 && node != 1) {
st[node].insert(0);
st[node].insert(0);
slope[node] = 1;
return;
}
for(auto y : v[node]) {
int x = y.first;
int d = y.second;
if(x == p)
continue;
DFS(x, node);
shift(x, d);
slope[node] += slope[x];
schema[node] += schema[x];
merge(st[node], st[x]);
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
ll sum = 0;
int i;
cin >> n >> m;
for(i = 2; i <= n + m; i++) {
int p, l;
cin >> p >> l;
v[i].push_back({p, l});
v[p].push_back({i, l});
sum += 1LL * l;
}
DFS(1, 0);
int last;
while(slope[1] > 0) {
auto it = st[1].rbegin();
last = *it;
st[1].erase(st[1].find(*it));
slope[1]--;
}
/// Acum cel din spate e primul cu slope 0
int panta = (n + m) / 2;
vector <pii> v;
v.push_back({0, 0});
for(auto x : st[1]){
debug(x);
if(x == 0){
panta--;
continue;
}
if(v.size() == 0 || v.back().first != x){
v.push_back({x, 1});
}else{
v.back().second++;
}
debug(x);
panta++;
}
panta = 6;
debug(panta);
for(int i = 1; i < v.size(); i++){
sum -= 1LL * panta * (v[i].first - v[i - 1].first);
debug(panta);
debugs(v[i - 1].first);
debug(v[i].first);
debug(sum);
panta -= v[i].second;
}
debug(sum);
cout << last;
return 0;
}
Compilation message
fireworks.cpp: In function 'int main()':
fireworks.cpp:114:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
114 | for(int i = 1; i < v.size(); i++){
| ~~^~~~~~~~~~
fireworks.cpp:123:13: warning: 'last' may be used uninitialized in this function [-Wmaybe-uninitialized]
123 | cout << last;
| ^~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
4 ms |
7244 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
6 ms |
7360 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
4 ms |
7244 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
4 ms |
7244 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |