codejunk's blog

By codejunk, history, 4 years ago, In English

Below mentioned code gives error and i have not seen a single python code successfully submitted .

1420C2

for i in range(int(input())): n,q=map(int,input().split()) a=list(map(int,input().split())) a=[-1]+a[0:n]+[-1] amin=[] amax=[] k=0 for i in range(1,n+1): if a[i-1]>a[i] and a[i]<a[i+1]: amin.append(a[i]) k-=a[i] elif a[i-1]<a[i] and a[i]>a[i+1]: amax.append(a[i])
k+=a[i] print(k) for j in range(q): l,r=map(int,input().split()) a[l],a[r]=a[r],a[l] d= sorted([l-1,l,l+1,r-1,r,r+1]) for i in set(d): if i>0 and i<n+1:
if a[i] in amax : if a[i-1]>a[i] and a[i]<a[i+1]: amin.append(a[i]) k-=a[i] amax.pop(i) k-=a[i] elif a[i-1]<a[i] and a[i]>a[i+1]: continue else: amax.pop(i) k-=a[i] elif a[i] in amin: if a[i-1]>a[i] and a[i]<a[i+1]: continue elif a[i-1]<a[i] and a[i]>a[i+1]: amax.append(a[i])
k+=a[i] amin.pop(i) k+=a[i] else:
amin.pop(i) k+=a[i] else: if a[i-1]>a[i] and a[i]<a[i+1]: amin.append(a[i]) k-=a[i] elif a[i-1]<a[i] and a[i]>a[i+1]: amax.append(a[i])
k+=a[i]
print(k)

  • Vote: I like it
  • -19
  • Vote: I do not like it

| Write comment?