QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#404136 | #6298. Coloring | 275307894a | WA | 4ms | 34848kb | C++14 | 2.2kb | 2024-05-03 13:04:45 | 2024-05-03 13:04:46 |
Judging History
answer
#include<bits/stdc++.h>
#define Gc() getchar()
#define Me(x,y) memset(x,y,sizeof(x))
#define Mc(x,y) memcpy(x,y,sizeof(x))
#define d(x,y) ((m)*(x-1)+(y))
#define R(n) (rnd()%(n)+1)
#define Pc(x) putchar(x)
#define LB lower_bound
#define UB upper_bound
#define fi first
#define se second
#define eb emplace_back
#define all(x) x.begin(),x.end()
using namespace std;using ll=long long;using db=double;using lb=long db;using ui=unsigned;using ull=unsigned long long;using pii=pair<int,int>;
const int N=5000+5,M=N*4+5,K=1000+5,mod=998244353,Mod=mod-1;const db eps=1e-8;const ll INF=1e18+7;mt19937 rnd(263082);
#define Tp template<typename T>
#define Ts template<typename T,typename... Ar>
namespace Debug{
Tp void _debug(char* f,T t){cerr<<f<<'='<<t<<endl;}
Ts void _debug(char* f,T x,Ar... y){while(*f!=',') cerr<<*f++;cerr<<'='<<x<<",";_debug(f+1,y...);}
#ifdef LOCAL
#define gdb(...) _debug((char*)#__VA_ARGS__,__VA_ARGS__)
#else
#define gdb(...) void()
#endif
}using namespace Debug;
int vis[N],n,s,w[N],p[N],A[N];vector<int> S[N];
ll dp[N][N];
void DP(int x){
for(int i=0;i<=n+2;i+=2) dp[x][i]=1ll*-p[x]*i;
for(int i=1;i<=n+2;i+=2) dp[x][i]=1ll*-p[x]*i+w[x];
for(int i:S[x]) if(!vis[i]){
DP(i);
ll mx=-INF;
for(int j=0;j<=n+2;j++){
mx=max(mx,dp[i][j]);
dp[x][j]+=mx;
}
}
}
int B[N],Bh;
ll f[N][3];
void Solve(){
int i,j;scanf("%d%d",&n,&s);
for(i=1;i<=n;i++) scanf("%d",&w[i]);
for(i=1;i<=n;i++) scanf("%d",&p[i]);
for(i=1;i<=n;i++) scanf("%d",&A[i]),S[A[i]].emplace_back(i);
int x=s;while(!vis[x]) vis[x]=1,x=A[x];
if(x^s){
DP(s);
printf("%lld\n",dp[s][1]+p[s]);
return;
}
if(A[A[s]]==s){
DP(s);
printf("%lld\n",max({dp[s][1],dp[s][2],dp[s][1]+dp[A[s]][1]})+p[s]);
return;
}
B[Bh=1]=s;x=A[s];while(x^s) B[++Bh]=x,x=A[x];
reverse(B+2,B+Bh+1);
for(i=1;i<=Bh;i++) DP(B[i]);
ll ans=-INF;
for(i=0;i<=n;i++){
Me(f,0x3f);Me(f[Bh+1],0);
for(j=Bh;j;j--){
ll mx=-INF;
for(int x:{0,1,2}){
mx=max(mx,f[j+1][x]);
f[j][x]=mx+dp[B[j]][i+x];
}
}
ans=max({ans,i?f[1][0]:-INF,f[1][1],f[1][2]});
}
printf("%lld\n",ans+p[s]);
}
int main(){
int t=1;
// scanf("%d",&t);
while(t--) Solve();
cerr<<clock()*1.0/CLOCKS_PER_SEC<<'\n';
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 4180kb
input:
3 1 -1 -1 2 1 0 0 3 1 2
output:
1
result:
ok 1 number(s): "1"
Test #2:
score: 0
Accepted
time: 0ms
memory: 4232kb
input:
10 8 36175808 53666444 14885614 -14507677 -92588511 52375931 -87106420 -7180697 -158326918 98234152 17550389 45695943 55459378 18577244 93218347 64719200 84319188 34410268 20911746 49221094 8 1 2 2 8 8 4 7 8 4
output:
35343360
result:
ok 1 number(s): "35343360"
Test #3:
score: -100
Wrong Answer
time: 4ms
memory: 34848kb
input:
5000 1451 531302480 400140870 -664321146 -376787089 -440627168 -672055995 924309614 2764785 -225700856 880835131 -435550509 162278080 -635253658 251803267 -499868931 213283508 603121701 -603347266 541062018 -502078443 -585620031 486788884 864390909 -670529282 -63580194 512939729 691685896 481123612 ...
output:
80519799290
result:
wrong answer 1st numbers differ - expected: '83045140866', found: '80519799290'