Submission #791652

# Submission time Handle Problem Language Result Execution time Memory
791652 2023-07-24T08:33:37 Z 이성호(#10048) Nestabilnost (COI23_nestabilnost) C++17
0 / 100
325 ms 48708 KB
#include <iostream>
#include <vector>
#define int long long
using namespace std;

vector<int> adj[300005];
int N, a[300005];
int f[300005];
int suf[300005];
vector<int> arr;
void dfs(int v, int p)
{
    arr.push_back(a[v]);
    for (int i:adj[v]) {
        if (i != p) dfs(i, v);
    }
}
int solve(int l, int r)
{
    vector<int> vc;
    for (int i = l; i <= r; i++) vc.push_back(arr[i]);
    int n = r - l + 1; //0~n-1
    vector<int> pk;
    for (int i = 1; i < n; i++) {
        if (vc[i] == 0) {
            pk.push_back(vc[i-1]+1);
        }
    }
    pk.push_back(vc[n-1]+1);
    int ans = 0;
    for (int i = 0; i < (int)pk.size(); i++) {
        int s = i;
        while (i + 1 < (int)pk.size() && pk[i] == pk[i+1]) i++;
        int e = i;
        ans += min((e - s + 1) * suf[pk[i]], f[pk[i]]);
    }
    return ans;
}
signed main()
{
    cin >> N;
    for (int i = 1; i <= N; i++) cin >> a[i];
    for (int i = 1; i <= N; i++) cin >> f[i];
    for (int i = 1; i < N; i++) {
        int u, v; cin >> u >> v;
        adj[u].push_back(v);
        adj[v].push_back(u);
    }
    suf[N] = f[N];
    for (int i = N - 1; i >= 1; i--) {
        suf[i] = min(suf[i+1], f[i]);
    }
    dfs(1, 0);
    int prv = 0;
    int ans = 0;
    for (int i = 1; i < N; i++) {
        if (arr[i] != arr[i-1] + 1 && arr[i]) {
            ans += solve(prv, i-1);
            prv = i;
        }
    }
    ans += solve(prv, N-1);
    cout << ans << '\n';
    return 0;
}
# Verdict Execution time Memory Grader output
1 Incorrect 8 ms 8020 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Correct 325 ms 48708 KB Output is correct
2 Incorrect 297 ms 46412 KB Output isn't correct
3 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 3 ms 7252 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 8 ms 8020 KB Output isn't correct
2 Halted 0 ms 0 KB -
# Verdict Execution time Memory Grader output
1 Incorrect 8 ms 8020 KB Output isn't correct
2 Halted 0 ms 0 KB -