(n,),a,(q,),*e=[[*map(int,s.split())]for s in open(0)] uf=[*range(n)] c=a[:] def find(x): q=[] while uf[x]^x:q+=x,;x=uf[x] for p in q:uf[p]=x return x def unite(x,y): x,y=find(x),find(y) if x^y:uf[x]=y;c[y]+=c[x];c[x]=0 for i in range(n-1): if a[i] and a[i+1]: unite(i,i+1) for x,b in e: x-=1 ans=c[find(x)]-a[x]+b if x and find(x-1)!=find(x): ans+=c[find(x-1)] if x+1