이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
#define F first
#define S second
#define pb push_back
#define ppb pop_back
#define ep insert
#define endl '\n'
#define elif else if
#define pow pwr
#define sqrt sqrtt
#define int long long
#define ll long long
#define y1 YONE
typedef unsigned long long ull;
using namespace std;
const int N=3e5+5;
int a[N],n,dp[N],c[N],p[N];
vector<vector<int>> v;
int32_t main(){
ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
cin>>n>>a[0];
int ans=a[0];
for (int i=1;i<=n;i++){
cin>>a[i]>>p[i];
c[p[i]]=i;
}
for (int i=1;i<=n;i++){
if (p[i]) continue;
vector<int> b;
b.pb(a[i]);
int x=i;
while (c[x]){
x=c[x];
b.pb(a[x]);
}
vector<int> d;
for (int i=0;i<b.size();i++){
if (!b[i]) continue;
int r=i,x=0;
for (int j=i;j<b.size();j++){
if (!b[j]) continue;
if (b[j]<0 && b[i]<0) r=j,x+=b[j];
elif (b[j]>0 && b[i]>0) r=j,x+=b[j];
else break;
}
d.pb(x);
i=r;
}
if (d.empty()) continue;
if (d[0]>0){
ans+=d[0];
d.erase(d.begin());
}
if (d.empty()) continue;
if (d.back()<0) d.ppb();
if (d.empty()) continue;
reverse(d.begin(),d.end());
v.pb(d);
}
set<vector<int>> s;
for (int i=0;i<v.size();i++){
int sz=v[i].size();
s.ep({v[i][sz-1],v[i][sz-1]+v[i][sz-2],i});
}
while (!s.empty()){
vector<int> x=*--s.end();
s.erase(--s.end());
if (ans+x[0]<0) break;
ans+=x[1];
v[x[2]].ppb();
v[x[2]].ppb();
if (v[x[2]].empty()) continue;
int sz=v[x[2]].size();
s.ep({v[x[2]][sz-1],v[x[2]][sz-2]+v[x[2]][sz-1],x[2]});
}
cout<<ans-a[0]<<endl;
return 0;
}
컴파일 시 표준 에러 (stderr) 메시지
Main.cpp: In function 'int32_t main()':
Main.cpp:37:23: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
37 | for (int i=0;i<b.size();i++){
| ~^~~~~~~~~
Main.cpp:40:27: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
40 | for (int j=i;j<b.size();j++){
| ~^~~~~~~~~
Main.cpp:61:19: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::vector<long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
61 | for (int i=0;i<v.size();i++){
| ~^~~~~~~~~
# | 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... |