Submission #791845

# Submission time Handle Problem Language Result Execution time Memory
791845 2023-07-24T10:58:16 Z 이동현(#10049) Nestabilnost (COI23_nestabilnost) C++17
0 / 100
138 ms 63828 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 + !mp[1] > 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 10640 KB Output is correct
3 Correct 7 ms 10580 KB Output is correct
4 Correct 6 ms 10580 KB Output is correct
5 Correct 9 ms 10580 KB Output is correct
6 Incorrect 7 ms 10604 KB Output isn't correct
7 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 119 ms 63728 KB Output is correct
2 Correct 122 ms 63764 KB Output is correct
3 Correct 124 ms 63828 KB Output is correct
4 Correct 125 ms 63804 KB Output is correct
5 Correct 127 ms 62608 KB Output is correct
6 Correct 138 ms 61652 KB Output is correct
7 Correct 125 ms 61360 KB Output is correct
8 Incorrect 125 ms 61260 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 10640 KB Output is correct
3 Correct 7 ms 10580 KB Output is correct
4 Correct 6 ms 10580 KB Output is correct
5 Correct 9 ms 10580 KB Output is correct
6 Incorrect 7 ms 10604 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 10640 KB Output is correct
3 Correct 7 ms 10580 KB Output is correct
4 Correct 6 ms 10580 KB Output is correct
5 Correct 9 ms 10580 KB Output is correct
6 Incorrect 7 ms 10604 KB Output isn't correct
7 Halted 0 ms 0 KB -