Submission #421564

#TimeUsernameProblemLanguageResultExecution timeMemory
421564maomao90Fireworks (APIO16_fireworks)C++17
100 / 100
202 ms43912 KiB
#include <bits/stdc++.h> 
using namespace std;

template <class T>
inline bool mnto(T& a, T b) {return a > b ? a = b, 1 : 0;}
template <class T>
inline bool mxto(T& a, T b) {return a < b ? a = b, 1: 0;}
#define REP(i, s, e) for (int i = s; i < e; i++)
#define RREP(i, s, e) for (int i = s; i >= e; i--)
typedef long long ll;
typedef long double ld;
#define MP make_pair
#define FI first
#define SE second
typedef pair<int, int> ii;
typedef pair<ll, ll> pll;
#define MT make_tuple
typedef tuple<int, int, int> iii;
#define ALL(_a) _a.begin(), _a.end()
#define pb emplace_back
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<ii> vii;

#define INF 1000000005
#define LINF 1000000000000000005
#define MOD 1000000007
#define MAXN 300005

int n, m;
int p[MAXN], c[MAXN];

struct lines {
	ll m, c; // gradient and y-intercept of the right-most line
	priority_queue<ll> pts; // points where slope changes
	void join(lines& o) {
		if (o.pts.size() > pts.size()) swap(o.pts, pts);
		while (!o.pts.empty()) {
			pts.push(o.pts.top());
			o.pts.pop();
		}
		m += o.m;
		c += o.c;
	}
} dp[MAXN];


int main() {
	scanf("%d%d", &n, &m);
	REP (i, 2, n + m + 1) {
		scanf("%d%d", &p[i], &c[i]);
	}
	REP (i, n + 1, n + m + 1) {
		dp[i].pts.push(c[i]);
		dp[i].pts.push(c[i]);
		dp[i].m = 1;
		dp[i].c = -c[i];
		dp[p[i]].join(dp[i]);
	}
	RREP (i, n, 2) {
		while (dp[i].m > 1) {
			dp[i].m--;
			dp[i].c += dp[i].pts.top();
			dp[i].pts.pop();
		}
		ll fi = dp[i].pts.top(); dp[i].pts.pop();
		ll se = dp[i].pts.top(); dp[i].pts.pop();
		dp[i].pts.push(fi + c[i]);
		dp[i].pts.push(se + c[i]);
		dp[i].c -= c[i];
		dp[p[i]].join(dp[i]);
	}
	while (dp[1].m > 0) {
		dp[1].m--;
		dp[1].c += dp[1].pts.top();
		dp[1].pts.pop();
	}
	printf("%lld\n", dp[1].c);
	return 0;
}

/*
4 6
1 5
2 5
2 8
3 3
3 2
3 3
2 9
4 4
4 3
*/

Compilation message (stderr)

fireworks.cpp: In function 'int main()':
fireworks.cpp:49:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   49 |  scanf("%d%d", &n, &m);
      |  ~~~~~^~~~~~~~~~~~~~~~
fireworks.cpp:51:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
   51 |   scanf("%d%d", &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...