이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
/* be name khoda */
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> pii;
#define forifrom(i, s, n) for (ll i = s; i < n; ++i)
#define fori(i, n) forifrom(i, 0, n)
#define F first
#define S second
// ------------------------------------------------------------------
const ll maxn = 300010;
ll n, m, ans;
vector<pii> subt[maxn];
ll A[maxn * 2];
pii B[maxn];
void dfs(ll x) {
for (auto [y, w] : subt[x]) {
dfs(y);
}
ll i = 0;
for (auto [y, w] : subt[x]) {
A[i * 2] = B[y].F + w;
A[i * 2 + 1] = B[y].S + w;
++i;
}
if (!i) return;
sort(A, A + i * 2);
for (auto [y, w] : subt[x]) {
ans += max(0LL, B[y].F + w - A[i]);
ans += max(0LL, A[i] - B[y].S - w);
}
B[x] = {A[i - 1], A[i]};
}
int main() {
ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
cin >> n >> m;
n += m;
fori (i, n - 1) {
ll p, w; cin >> p >> w; --p;
subt[p].emplace_back(i + 1, w);
}
dfs(0);
cout << ans << '\n';
}
컴파일 시 표준 에러 (stderr) 메시지
fireworks.cpp: In function 'void dfs(ll)':
fireworks.cpp:24:20: warning: unused variable 'w' [-Wunused-variable]
for (auto [y, w] : subt[x]) {
^| # | 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... |