結果
| 問題 | No.1154 シュークリームゲーム(Hard) |
| コンテスト | |
| ユーザー |
raincity
|
| 提出日時 | 2026-01-27 09:42:29 |
| 言語 | C++17 (gcc 15.2.0 + boost 1.89.0) |
| 結果 |
WA
|
| 実行時間 | - |
| コード長 | 1,231 bytes |
| 記録 | |
| コンパイル時間 | 2,721 ms |
| コンパイル使用メモリ | 244,620 KB |
| 実行使用メモリ | 16,096 KB |
| 最終ジャッジ日時 | 2026-01-27 09:42:34 |
| 合計ジャッジ時間 | 3,922 ms |
|
ジャッジサーバーID (参考情報) |
judge2 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 2 |
| other | AC * 14 WA * 26 |
ソースコード
#include <bits/stdc++.h>
#include <ext/pb_ds/priority_queue.hpp>
using namespace std;
#define rep(i, a, b) for (int i = (a); i <= (b); i++)
#define sz(x) (int)(x).size()
using ll = long long;
const int kn = 2e5 + 10;
int n;
ll a[kn], b[kn];
__gnu_pbds::priority_queue<ll> pq[kn];
vector<int> g[kn];
void dfs(int u, int fa) {
for (int v : g[u]) {
if (v == fa) continue;
dfs(v, u);
pq[u].join(pq[v]);
b[u] += b[v];
}
ll x = a[u];
while (sz(pq[u]) && x < pq[u].top()) {
x -= pq[u].top();
pq[u].pop();
if (!sz(pq[u])) {
b[u] += x;
return;
}
x += pq[u].top();
pq[u].pop();
}
if (x >= 0) pq[u].push(x);
else b[u] += x;
}
int main() {
cin.tie(0)->sync_with_stdio(0);
cin >> n;
rep (i, 1, n) cin >> a[i];
rep (i, 1, n - 1) {
int u, v;
cin >> u >> v;
g[u].push_back(v), g[v].push_back(u);
}
dfs(1, 0);
assert(b[1] <= 0);
pq[1].push(b[1]);
ll ans = 0;
int phase = 0;
while (sz(pq[1])) {
if (phase == 0) ans += pq[1].top();
else ans -= pq[1].top();
pq[1].pop();
phase ^= 1;
}
cout << ans << "\n";
}
raincity