Submission #791843

# Submission time Handle Problem Language Result Execution time Memory
791843 2023-07-24T10:57:09 Z 이동현(#10049) Nestabilnost (COI23_nestabilnost) C++17
0 / 100
146 ms 63820 KB
#include <bits/stdc++.h>
#define int long long
#define mi(x, y) (x = min(x, y))
#define ma(x, y) (x = max(x, y))
using namespace std;

const int NS = (int)3e5 + 4;
int n;
int a[NS], f[NS], mn[NS];
int dp[NS];
vector<int> way[NS];
int chk[NS];

deque<int> deq;
map<int, int> mp;
int diff = 0;
int pre[NS];

void sol(int x, int pr){
    dp[x] = mn[a[x]];
    for(auto&nxt:way[x]){
        if(nxt == pr){
            continue;
        }

        sol(nxt, x);
        pre[x] = nxt;

        if(a[x] + 1 == a[nxt]){
            dp[x] = dp[nxt];
        }
        else{
            dp[x] = dp[nxt] + mn[a[x]];

            if(a[nxt] == 0){
                while(diff > 2){
                    mp[a[deq[0]] - a[deq[1]]] -= 1;
                    if(!mp[a[deq[0]] - a[deq[1]]]) --diff;
                    deq.pop_front();
                }

                dp[x] = min(dp[x], f[a[x]] + (pre[deq.front()] != -1 ? dp[pre[deq.front()]] : 0));
            }
        }
    }

    if(pr != -1){
        deq.push_back(x);
        ++mp[a[x] - a[pr]];
        if(mp[a[x] - a[pr]] == 1){
            ++diff;
        }
    }
}



signed main(){
    ios_base::sync_with_stdio(false);
    cin.tie(0);

    memset(pre, -1, sizeof(pre));
    cin >> n;
    for(int i = 0; i < n; ++i){
        cin >> a[i];
    }
    for(int i = 0; i < n; ++i){
        cin >> f[i];
    }
    for(int i = n - 1; i >= 0; --i){
        mn[i] = f[i];
        if(i + 1 < n){
            mn[i] = min(mn[i], mn[i + 1]);
        }
    }

    for(int i = 1; i < n; ++i){
        int x, y;
        cin >> x >> y;
        --x, --y;
        way[x].push_back(y);
        way[y].push_back(x);
    }

    sol(0, -1);

    cout << dp[0] << '\n';
    
    return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 6 ms 10580 KB Output is correct
2 Correct 6 ms 10528 KB Output is correct
3 Correct 9 ms 10624 KB Output is correct
4 Correct 9 ms 10580 KB Output is correct
5 Correct 9 ms 10580 KB Output is correct
6 Incorrect 8 ms 10580 KB Output isn't correct
7 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 130 ms 63780 KB Output is correct
2 Correct 139 ms 63820 KB Output is correct
3 Correct 134 ms 63764 KB Output is correct
4 Correct 146 ms 63796 KB Output is correct
5 Correct 141 ms 62540 KB Output is correct
6 Correct 130 ms 61644 KB Output is correct
7 Correct 135 ms 61308 KB Output is correct
8 Incorrect 125 ms 61312 KB Output isn't correct
9 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 5 ms 9684 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 6 ms 10580 KB Output is correct
2 Correct 6 ms 10528 KB Output is correct
3 Correct 9 ms 10624 KB Output is correct
4 Correct 9 ms 10580 KB Output is correct
5 Correct 9 ms 10580 KB Output is correct
6 Incorrect 8 ms 10580 KB Output isn't correct
7 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 6 ms 10580 KB Output is correct
2 Correct 6 ms 10528 KB Output is correct
3 Correct 9 ms 10624 KB Output is correct
4 Correct 9 ms 10580 KB Output is correct
5 Correct 9 ms 10580 KB Output is correct
6 Incorrect 8 ms 10580 KB Output isn't correct
7 Halted 0 ms 0 KB -