Submission #1013490

#TimeUsernameProblemLanguageResultExecution timeMemory
1013490amine_arouaNestabilnost (COI23_nestabilnost)C++17
0 / 100
1497 ms524288 KiB
#include <bits/stdc++.h> using namespace std; #define pb push_back #define int long long #define nl '\n' int n; vector<int> a , b , f; vector<map<int ,int>> possible_k; vector<int> dp; vector<vector<int>> adj; vector<int> order; void dfs(int x , int p) { order.pb(x); for(auto u : adj[x]) { if(u == p) continue; dfs(u , x); } } void read_in() { order.clear(); cin>>n; // n = rand()%1000 + 1; a.clear(); a.assign(n , 0); possible_k.clear(); possible_k.assign(n , map<int ,int>()); b = a; f.clear(); f.assign(n + 1 , 0); adj.assign(n , {}); for(int i = 0 ; i < n ; i++) { // b[i] = rand()%n; cin>>b[i]; } for(int i = 1 ; i <= n;i++) { cin>>f[i]; // f[i] = rand()%10000 + 1; } for(int i = 1 ; i <= n - 1 ; i++) { int u , v; cin>>u>>v; // u = i , v = i + 1; u-- , v--; adj[u].pb(v); adj[v].pb(u); } } int solve() { int mn = 1e9; for(int i = 1 ; i <= n ; i++) mn = min(mn , f[i]); if(n == 1) { return mn; } dfs(0 , -1); for(int i = 0 ; i < n ; i++) a[i] = b[order[i]]; for(int i = 0 ; i + 1 < n ; i++) { int diff = abs(a[i] + 1 - a[i + 1]); vector<int> vals; if(diff == 0) { for(int j = 1 ; j <= n;j++) { vals.pb(j); } } else { for(int d = 1 ; d* d <= diff ; d++) { if(d * d == diff) { vals.pb(d); } else if(diff % d == 0) { vals.pb(d); vals.pb(diff/d); } } } for(auto k : vals) { if(possible_k[i].count(k)) continue; assert(k != 0); int last = i + 1; for( ; last < n && (abs(a[last - 1] - a[last] + 1) % k == 0); last++); for(int j = i; j < last ; j++) { possible_k[j][k] = last; } } } dp.clear(); dp.assign(n + 1 , 1e18); dp[n] = 0; for(int i = n - 1 ; i >= 0 ; i--) { dp[i] = dp[i + 1] + mn; if(i + 1 < n) { for(auto [k , last] : possible_k[i]) { dp[i] = min(dp[i] , dp[last] + f[k]); } } } for(int i = 0 ; i < n;i++) adj[i].clear(); return dp[0]; } signed main() { read_in(); cout<<solve()<<nl; }
#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...