#include <bits/stdc++.h>
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("avx2")
using namespace std;
#define pb push_back
#define int long long
#define nl '\n'
int n;
vector<int> a , b , f;
vector<vector<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 , vector<int>(n + 1, -1));
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]);
dfs(0 , -1);
for(int i = 0 ; i < n ; i++)
a[i] = b[order[i]];
for(int i = 0 ; i < n ; i++)
{
for(int k = 1;k <= n ; k++)
{
if(possible_k[i][k] != -1)
continue;
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--)
{
for(int k = 1 ; k <= n;k++)
{
int last = possible_k[i][k];
if(last == -1)
continue;
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;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
180 ms |
196944 KB |
Output is correct |
2 |
Correct |
175 ms |
196964 KB |
Output is correct |
3 |
Correct |
227 ms |
196780 KB |
Output is correct |
4 |
Incorrect |
186 ms |
196988 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
201 ms |
524288 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
348 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
180 ms |
196944 KB |
Output is correct |
2 |
Correct |
175 ms |
196964 KB |
Output is correct |
3 |
Correct |
227 ms |
196780 KB |
Output is correct |
4 |
Incorrect |
186 ms |
196988 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
180 ms |
196944 KB |
Output is correct |
2 |
Correct |
175 ms |
196964 KB |
Output is correct |
3 |
Correct |
227 ms |
196780 KB |
Output is correct |
4 |
Incorrect |
186 ms |
196988 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |