Submission #1026521

#TimeUsernameProblemLanguageResultExecution timeMemory
1026521mansurJobs (BOI24_jobs)C++17
11 / 100
74 ms50972 KiB
#include<bits/stdc++.h>

using namespace std;

#define rall(s) s.rbegin(), s.rend()
#define all(s) s.begin(), s.end()
#define sz(s) (int)s.size()
#define s second 
#define f first

using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;

const int N = 1e6;
const ll inf = 1e18;

vector<int> g[N];
ll dp[N], x[N], p[N];

void dfs(int u) {
	dp[u] = x[u];
	for (int to: g[u]) {
		dfs(to);
		dp[u] += dp[to];
	}
	dp[u] = max(dp[u], 0ll);
}

void solve() {
	int n, ok = 1;
	ll s;
	cin >> n >> s;
	for (int i = 1; i <= n; i++) {
		cin >> x[i] >> p[i];
		if (p[i] && p[i] != i - 1) ok = 0;
	}
	if (ok) {
		vector<int> h;
		ll ans = 0, z = s;
		p[n + 1] = 0;
		for (int i = 1; i <= n + 1; i++) {
			if (p[i]) h.push_back(i);
			else {
				if (sz(h)) {
					ll pr[sz(h)], mn = 0;
					for (int j = 0; j < sz(h); j++) {
						if (j) pr[j] = pr[j - 1];
						else pr[j] = 0;
						pr[j] += x[h[j]];
						mn = min(mn, pr[j]);					
					}
					s += max(0ll, pr[sz(h) - 1]);
					ans += pr[sz(h) - 1] - mn;
				}
				h = {i};
			}
		}
		cout << max(ans, s) - z;
		return;
	}
	for (int i = 1; i <= n; i++) {
		g[p[i]].push_back(i);
	}
	dfs(0);
	cout << dp[0];
}

int main() {
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	solve();
}
#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...