| # | 제출 시각 | 아이디 | 문제 | 언어 | 결과 | 실행 시간 | 메모리 |
|---|---|---|---|---|---|---|---|
| 1362665 | cpdreamer | Jobs (BOI24_jobs) | C++20 | 1 ms | 580 KiB |
#include<bits/stdc++.h>
using namespace std;
void file() {
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
}
#define all(v) v.begin(),v.end()
#define pb push_back
#define V vector
#define P pair
#define F first
#define S second
typedef long long ll;
const ll MOD=998244353;
V<int>adj[2001];
bool b[2001];
ll v[2001];
int par[2001];
P<ll,int>f(int n,ll x) {
x+=v[n];
if (x>=0) {
return {x,n};
}
if (adj[n].empty()) {
return {x,-1};
}
ll maxd=LLONG_MIN;
int c=-1;
for (auto u:adj[n]) {
P<ll,int>p=f(u,x);
if (p.S!=-1) {
if (p.F>maxd) {
maxd=p.F;
c=p.S;
}
}
}
if (c==-1) {
return {x,-1};
}
return {min(maxd,x),c};
}
void solve() {
int n;
cin>>n;
for (int i=1;i<=n;i++) {
par[i]=-1;
b[i]=true;
}
ll s;
cin>>s;
ll in=s;
for (int i=1;i<=n;i++) {
cin>>v[i];
int p;
cin>>p;
if (p!=0) {
adj[p].pb(i);
par[i]=p;
}
}
set<P<ll,P<int,int>>,greater<>>st;
for (int i=1;i<=n;i++) {
if (par[i]==-1) {
P<ll,int>p=f(i,0);
if (p.S!=-1) {
st.insert({p.F,{p.S,i}});
}
}
}
while (!st.empty()) {
P<ll,P<int,int>>p=*st.begin();
st.erase(st.begin());
if (s+p.F<0) {
break;
}
int nd=p.S.F;
int r=p.S.S;
ll y=0;
while (nd!=par[r]) {
y+=v[nd];
b[nd]=false;
for (auto u:adj[nd]) {
if (b[u]) {
P<ll,int>o=f(u,0);
if (o.S!=-1) {
st.insert({o.F,{o.S,u}});
}
}
}
nd=par[nd];
}
s+=y;
}
cout<<s-in<<endl;
}
int main() {
// file();
solve();
}컴파일 시 표준 에러 (stderr) 메시지
| # | 결과 | 실행 시간 | 메모리 | 채점기 출력 |
|---|---|---|---|---|
| 결과를 불러오는 중입니다… | ||||
| # | 결과 | 실행 시간 | 메모리 | 채점기 출력 |
|---|---|---|---|---|
| 결과를 불러오는 중입니다… | ||||
| # | 결과 | 실행 시간 | 메모리 | 채점기 출력 |
|---|---|---|---|---|
| 결과를 불러오는 중입니다… | ||||
| # | 결과 | 실행 시간 | 메모리 | 채점기 출력 |
|---|---|---|---|---|
| 결과를 불러오는 중입니다… | ||||
| # | 결과 | 실행 시간 | 메모리 | 채점기 출력 |
|---|---|---|---|---|
| 결과를 불러오는 중입니다… | ||||
