#include <bits/stdc++.h>
using namespace std;
#define ll long long
struct fen{
vector <ll> bit;
ll n;
fen(ll _n){
n = _n;
bit = vector <ll>(n+1, -1e18);
}
//range max pt update fenwick tree
void upd(ll f, ll v){
while (f <= n){
bit[f] = max(bit[f], v);
f += (f&(-f));
}
}
ll qry(ll f){
ll maximum = -1e18;
while (f > 0){
maximum = max(maximum, bit[f]);
f -= (f&(-f));
}
return maximum;
}
};
int main()
{
ll n, s;
cin >> n >> s;
vector <ll> dp(n+1, -1e18);
ll cost[n+1], pre[n+1];
for (ll i = 1; i <= n; ++i){
cin >> cost[i] >> pre[i];
}
fen* tree = new fen(n);
ll runningMax = 0;
for (ll i = 1; i <= n; ++i){
ll dpVal = 0;
if (pre[i] == 0) dpVal = tree->qry(n);
else dpVal = dp[pre[i]];
dpVal = max(dpVal + cost[i], cost[i]);
dp[i] = dpVal;
tree->upd(i, dpVal);
runningMax = max(runningMax, dp[i]);
}
cout << runningMax << "\n";
}
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |