Submission #40845

#TimeUsernameProblemLanguageResultExecution timeMemory
40845ssnsarang2023Fireworks (APIO16_fireworks)C++14
100 / 100
245 ms132484 KiB
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> ii;

#define SZ(x) ((int)x.size())

const int N = (int)3e5+5;

struct data {
	ll a, b;
	priority_queue<ll> *pq;
} a[N];

data merge(const data &x, const data &y) {
	data re;
	re.a = x.a + y.a;
	re.b = x.b + y.b;
	if (x.pq->size() > y.pq->size()) {
		re.pq = x.pq;
		while (y.pq->size()) {
			re.pq->push(y.pq->top());
			y.pq->pop();
		}
	} else {
		re.pq = y.pq;
		while (x.pq->size()) {
			re.pq->push(x.pq->top());
			x.pq->pop();
		}
	}
	return re;
}

int n, m, p[N];
ll c[N];

int main() {
	scanf("%d%d", &n, &m);
	for (int i = 2; i <= n+m; ++i) scanf("%d%lld", &p[i], &c[i]);
	for (int i = 1; i <= n+m; ++i) {
		a[i].pq = new priority_queue<ll>();
		a[i].a = a[i].b = 0;
	}
	for (int i = n+m; i >= n+1; --i) {
		a[i].a = 1;
		a[i].b = -c[i];
		a[i].pq->push(c[i]);
		a[i].pq->push(c[i]);
		a[p[i]] = merge(a[p[i]], a[i]);
	}
	for (int i = n; i >= 2; --i) {
		while (a[i].a > 1) {
			--a[i].a;
			a[i].b += a[i].pq->top();
			a[i].pq->pop();
		}
		ll x = a[i].pq->top();
		a[i].pq->pop();
		ll y = a[i].pq->top();
		a[i].pq->pop();
		a[i].pq->push(x + c[i]);
		a[i].pq->push(y + c[i]);
		a[i].b -= a[i].a*c[i];
		a[p[i]] = merge(a[p[i]], a[i]);
	}
	while (a[1].a > 0) {
		--a[1].a;
		a[1].b += a[1].pq->top();
		a[1].pq->pop();
	}
	printf("%lld", a[1].b);
	return 0;
}

Compilation message (stderr)

fireworks.cpp: In function 'int main()':
fireworks.cpp:42:23: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  scanf("%d%d", &n, &m);
                       ^
fireworks.cpp:43:62: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  for (int i = 2; i <= n+m; ++i) scanf("%d%lld", &p[i], &c[i]);
                                                              ^
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...