結果

問題 No.1154 シュークリームゲーム(Hard)
ユーザー fastmathfastmath
提出日時 2020-08-12 03:56:06
言語 C++17(clang)
(17.0.6 + boost 1.83.0)
結果
AC  
実行時間 11 ms / 2,000 ms
コード長 1,776 bytes
コンパイル時間 2,605 ms
コンパイル使用メモリ 164,408 KB
実行使用メモリ 8,704 KB
最終ジャッジ日時 2024-04-17 18:59:50
合計ジャッジ時間 3,119 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 1 ms
5,376 KB
testcase_03 AC 1 ms
5,376 KB
testcase_04 AC 1 ms
5,376 KB
testcase_05 AC 1 ms
5,376 KB
testcase_06 AC 1 ms
5,376 KB
testcase_07 AC 2 ms
5,376 KB
testcase_08 AC 2 ms
5,376 KB
testcase_09 AC 1 ms
5,376 KB
testcase_10 AC 2 ms
5,376 KB
testcase_11 AC 2 ms
5,376 KB
testcase_12 AC 2 ms
5,376 KB
testcase_13 AC 1 ms
5,376 KB
testcase_14 AC 2 ms
5,376 KB
testcase_15 AC 1 ms
5,376 KB
testcase_16 AC 2 ms
5,376 KB
testcase_17 AC 2 ms
5,376 KB
testcase_18 AC 2 ms
5,376 KB
testcase_19 AC 2 ms
5,376 KB
testcase_20 AC 2 ms
5,376 KB
testcase_21 AC 2 ms
5,376 KB
testcase_22 AC 2 ms
5,376 KB
testcase_23 AC 2 ms
5,376 KB
testcase_24 AC 2 ms
5,376 KB
testcase_25 AC 2 ms
5,376 KB
testcase_26 AC 2 ms
5,376 KB
testcase_27 AC 2 ms
5,376 KB
testcase_28 AC 2 ms
5,376 KB
testcase_29 AC 2 ms
5,376 KB
testcase_30 AC 2 ms
5,376 KB
testcase_31 AC 2 ms
5,376 KB
testcase_32 AC 2 ms
5,376 KB
testcase_33 AC 2 ms
5,376 KB
testcase_34 AC 2 ms
5,376 KB
testcase_35 AC 2 ms
5,376 KB
testcase_36 AC 2 ms
5,376 KB
testcase_37 AC 2 ms
5,376 KB
testcase_38 AC 1 ms
5,376 KB
testcase_39 AC 1 ms
5,376 KB
testcase_40 AC 11 ms
8,704 KB
testcase_41 AC 2 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;
#define int long long
#define ii pair <int, int>
#define app push_back
#define all(a) a.begin(), a.end()
#define bp __builtin_popcountll
#define ll long long
#define mp make_pair
#define f first
#define s second
#define Time (double)clock()/CLOCKS_PER_SEC
#define debug(x) std::cout << #x << ": " << x << '\n';
const int N = 1007;
vector <int> g[N];
int n, a[N];
vector <int> dp[N];
int ad[N];
void dfs(int u, int p) {
    for (int v : g[u]) {
        if (v != p) {
            dfs(v, u);
            for (auto e : dp[v])
                dp[u].app(e);
            ad[u] += ad[v];
        }   
    }   
    sort(all(dp[u]));
    reverse(all(dp[u]));
    int cur = a[u], f = -1;
    for (int i = 0; i < dp[u].size(); ++i) {
        if (i % 2 == 0 && cur >= dp[u][i]) {
            vector <int> su = {cur};
            for (int j = i; j < dp[u].size(); ++j)
                su.app(dp[u][j]);
            dp[u] = su;
            return;                                         
        }   
        cur += dp[u][i] * f;
        f = -f;
    }       
    if (dp[u].size() % 2 == 0) {
        dp[u] = {cur};
    }   
    else {
        dp[u].clear();
        ad[u] += cur;
    }   
}   
int get(int u) {
    int f = 1, ans = 0;
    dp[u].app(ad[u]);
    for (auto e : dp[u]) {
        ans += e * f;
        f = -f;
    }   
    return ans;
}   
signed main() {
    #ifdef HOME
    freopen("input.txt", "r", stdin);
    #else
    #define endl '\n'
    ios_base::sync_with_stdio(0); cin.tie(0);
    #endif
    cin >> n;
    for (int i = 1; i <= n; ++i)
        cin >> a[i];
    for (int i = 0; i < n - 1; ++i) {
        int u, v;
        cin >> u >> v;
        g[u].app(v); g[v].app(u);
    }   
    dfs(1, 1);
    cout << get(1) << endl;
}
0