# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
819802 | Cyber_Wolf | Friend (IOI14_friend) | C++17 | 0 ms | 0 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#pragma GCC optimize("Ofast")
using namespace std;
using namespace __gnu_pbds;
#define lg long long
#define ordered_set tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update>
#define fastio ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
const lg N = 2e5+5;
lg par[N];
vector<set<lg>> adj(N);
lg get(lg src)
{
if(src == par[src]) return src;
return par[src] = get(par[src]);
}
void join(lg u, lg v)
{
par[u] = v;
return;
}
int findSample(int n, int c[], int h[], int p[])
{
int ans = c[0];
// for(int i = 1; i < n; i++)
// {
// h[i]++;
// }
for(int i = 1; i < n; i++)
{
if(h[i]) ans += c[i];
}
for(int i = 1; i < n; i++)
{
int sum = c[i];
for(int j = i-1; j >= 0; j--)
{
if(j == h[i]) continue;
// cout << j << ' ';
sum += c[j];
}
for(int j = i+1; j < n; j++)
{
if(h[j] == i) continue;
// cout << j << ' ';
sum += c[j];
}
// cout << ans << '\n';
ans = max(ans, sum);
}
return ans;
}
int main()
{
fastio;
int n;
cin >> n;
int c[n+1], h[n+1], p[n+1];
for(int i = 0; i < n; i++)
{
cin >> c[i];
}
for(int i = 1; i < n; i++)
{
cin >> h[i] >> p[i];
}
cout << findSample(n, c, h, p) << '\n';
return 0;
}