Why I get WA?Here is my program.
#include<stdio.h>
#include<stdlib.h>
#include<string>
#include<assert.h>
FILE *inp,*outp;
long a[14101];
long b[14101];
long g[25101];
long m,i;
void Change(long t1,long t2){
long k=a[t1];a[t1]=a[t2];a[t2]=k;
k=b[t1];b[t1]=b[t2];b[t2]=k;
g[b[t1]]=t1;g[b[t2]]=t2;
}
void Move(long i){
if (i>1&&a[i]>a[i/2]){
Change(i,i/2);
Move(i/2);
return;
}else
if (i*2<=m)
if (a[i]<a[i*2]||a[i]<a[i*2+1])
if (a[i*2]>a[i*2+1]){
Change(i,i*2);
Move(i*2);
}else{
Change(i,i*2+1);
Move(i*2+1);
};
}
main(){
inp=fopen("input.004","r");assert(inp);
outp=fopen("output.004","w");assert(outp);
memset(a,255,sizeof(a));
fscanf(inp,"%ld",&m);
for (i=1;i<=m;i++){
fscanf(inp,"%ld",&a[i]);
if (a[i]==-1){
fprintf(outp,"%ld\n",a[1]);
exit(-1);
};
g[i]=i;b[g[i]]=i;
Move(i);
}
i=m;
while(true){
fprintf(outp,"%ld\n",a[1]);
i++;fscanf(inp,"%ld",&a[g[i-m]]);
if (a[g[i-m]]==-1) exit(-1);
g[i]=g[i-m];b[g[i]]=i;
Move(g[i]);
}
fclose(outp);
}