이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
#define mp make_pair
#define X first
#define Y second
#define taskname "FIREWORKS"
using namespace std;
typedef long long ll;
typedef pair <int, int> ii;
const int N = 3e5 + 1;
const ll INF = 5e14;
struct data{
ll a, b;
priority_queue <ll> pq; // slope changing points
void operator += (data &other){
a += other.a;
b += other.b;
if (pq.size() < other.pq.size())
swap(pq, other.pq);
while (!other.pq.empty()){
pq.push(other.pq.top());
other.pq.pop();
}
}
};
int n, m, p[N], c[N];
data d[N];
void readInput(){
cin >> n >> m;
for(int i = 2; i <= n + m; i++)
cin >> p[i] >> c[i];
}
void solve(){
for(int i = 1; i <= n; i++){
d[i].a = 0;
d[i].b = 0;
}
for(int i = n + 1; i <= n + m; i++){
d[i].a = 1;
d[i].b = -c[i];
d[i].pq.push(c[i]);
d[i].pq.push(c[i]);
d[p[i]] += d[i];
}
for(int i = n; i > 1; i--){
while (d[i].a > 1){
d[i].a--;
d[i].b += d[i].pq.top();
d[i].pq.pop();
}
ll p1 = d[i].pq.top();
d[i].pq.pop();
ll p0 = d[i].pq.top();
d[i].pq.pop();
d[i].pq.push(p0 + c[i]);
d[i].pq.push(p1 + c[i]);
d[i].b -= c[i];
d[p[i]] += d[i];
}
while (d[1].a > 0){
d[1].a--;
d[1].b += d[1].pq.top();
d[1].pq.pop();
}
cout << d[1].b;
}
int main(){
readInput();
solve();
return 0;
}
# | 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... |