# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
305950 | aZvezda | Biochips (IZhO12_biochips) | C++14 | 218 ms | 401912 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>
using namespace std;
//#pragma GCC optimize ("O3")
//#pragma GCC target ("sse4")
#define endl "\n"
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
template<class T, class T2> inline bool chkmax(T &x, const T2 &y) { return x < y ? x = y, 1 : 0; }
template<class T, class T2> inline bool chkmin(T &x, const T2 &y) { return x > y ? x = y, 1 : 0; }
const ll mod = 1e9 + 7;
template<class T> inline void fix(T &x) {if(x >= mod | x <= -mod) {x %= mod;} if(x < 0) {x += mod;}}
#define out(x) cout << __LINE__ << ": " << (#x) << " = " << (x) << endl
const ll MAX_N = 1e5 + 10;
const ll MAX_W = 510;
vector<ll> g[MAX_N];
ll in[MAX_N], out[MAX_N], arr[MAX_N];
ll tme = -1;
ll dp[MAX_N][MAX_W];
void dfs(ll x) {
in[x] = tme; tme ++;
for(auto it : g[x]) {
dfs(it);
}
out[x] = tme;
}
bool cmp(ll a, ll b) {
return in[a] < in[b];
}
signed main() {
ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
ll n, w;
cin >> n >> w;
vector<ll> ord;
for(ll i = 1; i <= n; i ++) {
ll p;
cin >> p >> arr[i];
g[p].push_back(i);
ord.push_back(i);
}
dfs(0);
sort(ord.begin(), ord.end(), cmp);
for(ll i = 0; i < MAX_N; i ++) {fill_n(dp[i], MAX_W, -mod);}
dp[0][0] = 0;
for(ll i = 0; i < ord.size(); i ++) {
for(ll j = 0; j < MAX_W; j ++) {
chkmax(dp[i + 1][j], dp[i][j]);
chkmax(dp[out[ord[i]]][j + 1], dp[i][j] + arr[ord[i]]);
}
}
cout << dp[ord.size() - 1][w] << endl;
return 0;
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |