Submission #791837

# Submission time Handle Problem Language Result Execution time Memory
791837 2023-07-24T10:44:30 Z 이동현(#10049) Nestabilnost (COI23_nestabilnost) C++17
41 / 100
1500 ms 39332 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], len[NS];
vector<int> way[NS];
int md;
vector<int> pb;

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

        if(a[x] + 1 == a[nxt]){
            sol3(nxt, x);
            len[x] = max(len[x], len[nxt]);
        }
        else{
            pb.push_back(a[x]);
        }
    }
}

int sol2(int x, int pr){
    int rv = 0;
    for(auto&nxt:way[x]){
        if(nxt == pr){
            continue;
        }

        if((a[x] + 1) % md == a[nxt]){
            rv += sol2(nxt, x);
        }
        else{
            rv += dp[nxt];
        }
    }
    if(dp[x]){
        rv = min(rv, dp[x]);
    }
    return rv;
}

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

        sol(nxt, x);
    }

    md = (int)1e18;
    pb.clear();
    sol3(x, pr);
    dp[x] = sol2(x, pr) + mn[len[x]];
    md = len[x] + 1;
    dp[x] = min(dp[x], sol2(x, pr) + f[len[x]]);
    for(int i = (int)pb.size() - 1; i >= max(0ll, (int)pb.size() - 10); --i){
        md = pb[i] + 1;
        dp[x] = min(dp[x], sol2(x, pr) + f[pb[i]]);
    }
}

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

    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 172 ms 8140 KB Output is correct
2 Correct 168 ms 8200 KB Output is correct
3 Correct 210 ms 8140 KB Output is correct
4 Correct 66 ms 8184 KB Output is correct
5 Correct 11 ms 8020 KB Output is correct
6 Correct 7 ms 8180 KB Output is correct
# Verdict Execution time Memory Grader output
1 Execution timed out 1525 ms 39332 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 4 ms 7380 KB Output is correct
2 Correct 3 ms 7300 KB Output is correct
3 Correct 4 ms 7380 KB Output is correct
4 Correct 3 ms 7380 KB Output is correct
5 Correct 4 ms 7404 KB Output is correct
6 Correct 3 ms 7380 KB Output is correct
7 Correct 4 ms 7380 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 172 ms 8140 KB Output is correct
2 Correct 168 ms 8200 KB Output is correct
3 Correct 210 ms 8140 KB Output is correct
4 Correct 66 ms 8184 KB Output is correct
5 Correct 11 ms 8020 KB Output is correct
6 Correct 7 ms 8180 KB Output is correct
7 Correct 4 ms 7380 KB Output is correct
8 Correct 3 ms 7300 KB Output is correct
9 Correct 4 ms 7380 KB Output is correct
10 Correct 3 ms 7380 KB Output is correct
11 Correct 4 ms 7404 KB Output is correct
12 Correct 3 ms 7380 KB Output is correct
13 Correct 4 ms 7380 KB Output is correct
14 Correct 9 ms 7892 KB Output is correct
15 Correct 10 ms 7816 KB Output is correct
16 Correct 7 ms 7892 KB Output is correct
17 Correct 8 ms 7788 KB Output is correct
18 Correct 7 ms 7892 KB Output is correct
19 Correct 6 ms 7820 KB Output is correct
20 Correct 6 ms 7892 KB Output is correct
21 Correct 6 ms 7884 KB Output is correct
22 Correct 5 ms 7824 KB Output is correct
23 Correct 5 ms 7892 KB Output is correct
24 Correct 15 ms 7948 KB Output is correct
25 Correct 17 ms 7892 KB Output is correct
26 Correct 22 ms 7892 KB Output is correct
27 Correct 29 ms 7960 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 172 ms 8140 KB Output is correct
2 Correct 168 ms 8200 KB Output is correct
3 Correct 210 ms 8140 KB Output is correct
4 Correct 66 ms 8184 KB Output is correct
5 Correct 11 ms 8020 KB Output is correct
6 Correct 7 ms 8180 KB Output is correct
7 Execution timed out 1525 ms 39332 KB Time limit exceeded
8 Halted 0 ms 0 KB -