제출 #1291061

#제출 시각아이디문제언어결과실행 시간메모리
1291061qweqerNestabilnost (COI23_nestabilnost)C++20
0 / 100
1595 ms1080 KiB
#include <bits/stdc++.h>
#define FOR(i, beg, ed, etr) for(ll i = beg;i <= ed;i += etr)
#define FORN(i, beg, ed, etr) for(ll i = beg;i >= ed;i -= etr)
#define all(x) x.begin(), x.end()
#define F first
#define pb push_back
#define con continue
#define S second
using namespace std;
typedef long long ll;
typedef double db;
typedef long double ld;
typedef pair<int,int> pii;


const ll inf = 1e9;
const ll INF = 1e13;
const ll N = 5e3 + 5;
const ll maxn = 5e6;
const ll MOD = 998244353;
ll a[N];
ll f[N];
vector <int> g[N];
ll dep[N],id[N];
ll dp[N],suf[N];
void dfs(int v,int p = 0){
    id[dep[v]] = a[v];
    for(int to : g[v]){
        if(to != p){
            dep[to] = dep[v] + 1;
            dfs(to,v);
        }
    }
}
ll ql[N],qr[N];
void solve(){
    int n;cin >> n;
    FOR(i, 1, n, 1){
        cin >> a[i];
        dp[i] = INF;
    }
    FOR(i, 1, n, 1){
        cin >> f[i];
    }
    suf[n] = f[n];
    FORN(i, n-1, 1, 1){
        suf[i] = min(suf[i+1],f[i]);
    }
    FOR(i, 2, n, 1){
        ll u,v;cin >> u >> v;
        g[u].pb(v);
        g[v].pb(u);
    }
    dep[1] = 1;
    dfs(1);
    dp[1] = suf[id[1] + 1];
    FOR(i, 2, n, 1){
        ql[i] = i;
        dp[i] = dp[i-1] + suf[id[i]+1];
        while(ql[i] > 1){
            if(a[ql[i] - 1] + 1 == a[ql[i]]){
                ql[i] = ql[ql[i] - 1];
            }
        }
        dp[i] = min(dp[i],dp[ql[i]-1] + suf[id[i]+1]);
        qr[i] = ql[i];
        if(qr[i] > 1){
            if(id[qr[i] - 1] + 1 > id[i]){
                int moad = a[qr[i] - 1] + 1;
                qr[i] = qr[qr[i] - 1];
                dp[i] = min(dp[i],dp[qr[i] - 1] + f[moad]);
            }
        }
    }
    cout << dp[n];
}
main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    ll abd = 1;
//    cin >> abd;
    FOR(i, 1, abd, 1){
        solve();
    }
}

컴파일 시 표준 에러 (stderr) 메시지

code1.cpp:77:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
   77 | main(){
      | ^~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...