ZhenlongDai/ACPR huggingface dataset:
Problem: p03229
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
N=int(input())
l=[]

for i in range(N):
    l.append(int(input()))

l=sorted(l)
i=0
sum=0
nr=0
nl=0
right=l.pop(0)
left=right

while l:
    if i%4==0:
        nr=l.pop(-1)
        sum+=nr-right
        right=nr        
    elif i%4==1:
        nl=l.pop(-1)
        sum+=nl-left
        left=nl
    elif i%4==2:
        nr=l.pop(0)
        sum-=nr-right
        right=nr       
    elif i%4==3:
        nl=l.pop(0)
        sum-=nl-left
        left=nl
        
    i+=1
        
print(sum)


Failing test case input:
5
156
-1
0
-1
0

Expected output:
316


Actual output:
315


Fixed submission (verdict: Accepted):
N=int(input())
l=[]

for i in range(N):
    l.append(int(input()))

l=sorted(l)
i=0
sum=0
nr=0
nl=0
right=l.pop(0)
left=right

while len(l)>1:
    if i%4==0:
        nr=l.pop(-1)
        sum+=nr-right
        right=nr        
    elif i%4==1:
        nl=l.pop(-1)
        sum+=nl-left
        left=nl
    elif i%4==2:
        nr=l.pop(0)
        sum-=nr-right
        right=nr       
    elif i%4==3:
        nl=l.pop(0)
        sum-=nl-left
        left=nl
        
    i+=1



        
print(sum+max(abs(right-l[0]),abs(left-l[0])))

Problem: p03229
Buggy submission (PyPy3 (2.4.0), verdict: Wrong Answer):
from collections import deque
N = int(input())

q = []
for i in range(N):
    A = int(input())
    q.append(A)

q.sort()
q = deque(q)
row = []

for i in range(N):
    if i % 2 == 0:
        row.append(q.pop())
    else:
        row.append(q.popleft())
row2 = [row[-1]] + row[:-1]
row3 = row[1:] + [row[0]]
row_diff = [abs(row[i] - row[i-1]) for i in range(1, N)]
row2_diff = [abs(row2[i] - row2[i-1]) for i in range(1, N)]
row3_diff = [abs(row3[i] - row3[i-1]) for i in range(1, N)]
print(max(sum(row_diff), sum(row2_diff), sum(row3_diff)))


Failing test case input:
5
37
-7
-25
0
-1

Expected output:
137


Actual output:
132


Fixed submission (verdict: Accepted):
N = int(input())

r = []
for i in range(N):
    A = int(input())
    r.append(A)

r_asc = sorted(r)
r_desc = sorted(r, reverse=True)
row = []
row2 = []

for i in range(N):
    if i % 2 == 0:
        row.append(r_asc[i
        row2.append(r_desc[i
    else:
        row.append(r_desc[i
        row2.append(r_asc[i
row3 = [row[-1]] + row[:-1]
row4 = [row2[-1]] + row2[:-1]
row_diff = [abs(row[i] - row[i-1]) for i in range(1, N)]
row2_diff = [abs(row2[i] - row2[i-1]) for i in range(1, N)]
row3_diff = [abs(row3[i] - row3[i-1]) for i in range(1, N)]
row4_diff = [abs(row4[i] - row4[i-1]) for i in range(1, N)]
print(max(sum(row_diff), sum(row2_diff), sum(row3_diff), sum(row4_diff)))
Problem: p03229
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
from collections import deque

N = int(input())
a = [int(input()) for _ in range(N)]

a.sort()
if a[0] != a[1]:
    dqa = deque(a[1:])
    dq = deque([a[0]])
    cnt = 0
else: 
    dqa = deque(a[:N-1])
    dq = deque([a[-1]])
    cnt = 2

while len(dq) < N:
    if cnt == 0:
        q = dqa.pop()
        dq.append(q)
    elif cnt == 1:
        q = dqa.pop()
        dq.appendleft(q)
    elif cnt == 2:
        q = dqa.popleft()
        dq.append(q)
    else:
        q = dqa.popleft()
        dq.appendleft(q)
        cnt = -1
    cnt += 1

ans = 0
for i in range(1, N):
    ans += abs(dq[i] - dq[i-1])
print(ans)

Failing test case input:
5
37
-7
-25
0
-1

Expected output:
137


Actual output:
132


Fixed submission (verdict: Accepted):
from collections import deque

N = int(input())
a = [int(input()) for _ in range(N)]

a.sort()
def calc(m):
    if m == 1:
        dqa = deque(a[1:])
        dq = deque([a[0]])
        cnt = 0
    else: 
        dqa = deque(a[:N-1])
        dq = deque([a[-1]])
        cnt = 2

    while len(dq) < N:
        if cnt == 0:
            q = dqa.pop()
            dq.append(q)
        elif cnt == 1:
            q = dqa.pop()
            dq.appendleft(q)
        elif cnt == 2:
            q = dqa.popleft()
            dq.append(q)
        else:
            q = dqa.popleft()
            dq.appendleft(q)
            cnt = -1
        cnt += 1

    ans = 0
    for i in range(1, N):
        ans += abs(dq[i] - dq[i-1])
    return ans
        
print(max(calc(0), calc(1)))

Problem: p03229
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
import sys
from collections import deque
read = sys.stdin.read

N, *A = map(int, read().split())

A.sort()
A = deque(A)
a = deque()
answer = 0
side = True
a.append(A.popleft())

while A:
    if side:
        a.append(A.pop())
        if A:
            a.appendleft(A.pop())
        side = False
    else:
        a.append(A.popleft())
        if A:
            a.appendleft(A.popleft())
        side = True

if side:
    tmp = a.popleft()
else:
    tmp = a.pop()

if tmp - a[0] < tmp - a[-1]:
    a.append(tmp)
else:
    a.appendleft(tmp)

a = list(a)
for i, j in zip(a, a[1:]):
    answer += abs(i - j)

print(answer)

Failing test case input:
5
115
-1
0
-2
-1

Expected output:
236


Actual output:
235


Fixed submission (verdict: Accepted):
import sys
from collections import deque
read = sys.stdin.read

N, *A = map(int, read().split())

A.sort()
A = deque(A)
a = deque()
side = True
a.append(A.popleft())

while A:
    if side:
        a.append(A.pop())
        if A:
            a.appendleft(A.pop())
        side = False
    else:
        a.append(A.popleft())
        if A:
            a.appendleft(A.popleft())
        side = True

if side:
    tmp = a.popleft()
else:
    tmp = a.pop()

if abs(tmp - a[0]) <= abs(tmp - a[-1]):
    a.append(tmp)
else:
    a.appendleft(tmp)

if not side:
    tmp = a.popleft()
else:
    tmp = a.pop()

if abs(tmp - a[0]) <= abs(tmp - a[-1]):
    a.append(tmp)
else:
    a.appendleft(tmp)

answer = 0
a = list(a)
for i, j in zip(a, a[1:]):
    answer += abs(i - j)

print(answer)
Problem: p03229
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
from collections import*
n,*a=map(int,open(0).read().split())
a=deque(sorted(a))
deq=deque([a.popleft()])
for i in range(n-1):
  if i%4==0:
    deq.appendleft(a.pop())
  elif i%4==1:
    deq.append(a.pop())
  elif i%4==2:
    deq.appendleft(a.popleft())
  else:
    deq.append(a.popleft())
l=list(deq)
print(sum(abs(i-j)for i,j in zip(l,l[1:])))

Failing test case input:
5
37
-3
-17
0
-1

Expected output:
113


Actual output:
112


Fixed submission (verdict: Accepted):
from collections import*
n,*a=map(int,open(0).read().split())
def f(a):
  a=deque(a)
  deq=deque([a.popleft()])
  for i in range(n-1):
    if i%4==0:
      deq.appendleft(a.pop())
    elif i%4==1:
      deq.append(a.pop())
    elif i%4==2:
      deq.appendleft(a.popleft())
    else:
      deq.append(a.popleft())
  l=list(deq)
  return sum(abs(i-j)for i,j in zip(l,l[1:]))
s=sorted(a)
print(max(f(s),f(s[::-1])))

Problem: p03229
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
import sys
from collections import deque
read = sys.stdin.read

N, *A = map(int, read().split())

A.sort()
A = deque(A)
a = deque()
answer = 0
side = True
a.append(A.popleft())

while A:
    if side:
        a.append(A.pop())
        if A:
            a.appendleft(A.pop())
        side = False
    else:
        a.append(A.popleft())
        if A:
            a.appendleft(A.popleft())
        side = True

if side:
    tmp = a.popleft()
else:
    tmp = a.pop()

if abs(tmp - a[0]) < abs(tmp - a[-1]):
    a.append(tmp)
else:
    a.appendleft(tmp)

a = list(a)
for i, j in zip(a, a[1:]):
    answer += abs(i - j)

print(answer)

Failing test case input:
3
-9
27
-3

Expected output:
66


Actual output:
42


Fixed submission (verdict: Accepted):
import sys
from collections import deque
read = sys.stdin.read

N, *A = map(int, read().split())

A.sort()
A = deque(A)
a = deque()
side = True
a.append(A.popleft())

while A:
    if side:
        a.append(A.pop())
        if A:
            a.appendleft(A.pop())
        side = False
    else:
        a.append(A.popleft())
        if A:
            a.appendleft(A.popleft())
        side = True

if side:
    tmp = a.popleft()
else:
    tmp = a.pop()

if abs(tmp - a[0]) <= abs(tmp - a[-1]):
    a.append(tmp)
else:
    a.appendleft(tmp)

if not side:
    tmp = a.popleft()
else:
    tmp = a.pop()

if abs(tmp - a[0]) <= abs(tmp - a[-1]):
    a.append(tmp)
else:
    a.appendleft(tmp)

answer = 0
a = list(a)
for i, j in zip(a, a[1:]):
    answer += abs(i - j)

print(answer)
Problem: p03229
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
n = int(input())
a = []
for i in range(n):
    a.append(int(input()))


ans = 0
a.sort()
l = a[0]
r = a[0]
a.pop(0)
for i in range(0, n-1):
    if (i % 4 == 0):
        ans += a[-1] - l
        l = a[-1]
        a.pop(-1)
    if (i % 4 == 1):
        ans += a[-1] - r
        r = a[-1]
        a.pop(-1)
    if (i % 4 == 2):
        ans += l - a[0]
        l = a[0]
        a.pop(0)
    if (i % 4 == 3):
        ans += r-a[0]
        r = a[0]
        a.pop(0)
print(ans)


Failing test case input:
3
-2
2
-2

Expected output:
8


Actual output:
4


Fixed submission (verdict: Accepted):
n = int(input())
a = sorted([int(input()) for i in range(n)])
b = list(a[::-1])



def f(A):
    l = r = A.pop(0)

    ans = 0

    for i in range(len(A)
        if i % 2 == 0:
            ans += abs(l - A[-1]); l = A.pop(-1);
            ans += abs(r - A[-1]); r = A.pop(-1);
        else:
            ans += abs(l - A[0]); l = A.pop(0);
            ans += abs(r - A[0]); r = A.pop(0);
    if (A):
        ans += abs(l - A[0])

    return ans


print(max(f(a), f(b)))

Problem: p03229
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
all_num = int(input())
nums = []
for i in range(all_num):
    nums.append(int(input()))
	
nums = sorted(nums)

sort_nums = []
loop_num = int(len(nums)/2)
for i in range( loop_num ):
    if i % 2 == 0:
        sort_nums.append(nums[i])
        sort_nums.insert(0,nums[-(i+1)])
    else:
        sort_nums.insert(0,nums[i])
        sort_nums.append(nums[-(i+1)])
    
if len( nums ) % 2 == 1:
    center_index = int(len(nums)/2)
    sort_nums.append(nums[center_index])

total = 0
for i in range( len(sort_nums)-1 ):
    total = total + abs(sort_nums[i] - sort_nums[i+1])


print(total)


Failing test case input:
5
46
-1
-4
-2
-1

Expected output:
102


Actual output:
101


Fixed submission (verdict: Accepted):
all_num = int(input())
nums = []
for i in range(all_num):
    nums.append( int(input()) )
	
nums = sorted(nums)

sort_nums = []
loop_num = int(all_num/2)
for i in range( loop_num ):
    if i % 2 == 0:
        sort_nums.append(nums[i])
        sort_nums.insert(0,nums[-(i+1)])
    else:
        sort_nums.insert(0,nums[i])
        sort_nums.append(nums[-(i+1)])
    
if len( nums ) % 2 == 1:
    center_index = int(len(nums)/2)
    center_num = nums[center_index]
    if abs(center_num - sort_nums[0]) > abs(center_num - sort_nums[-1]):
        sort_nums.insert(0,center_num)
    else :
        sort_nums.append(center_num)


total = 0
for i in range( len(sort_nums)-1 ):
    total = total + abs(sort_nums[i] - sort_nums[i+1])

print(total)
Problem: p03229
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
from collections import deque

n = int(input())
a = [int(input()) for _ in range(n)]

a.sort()
deq = deque(a)

ans = 0

left, right = deq.popleft(), 0
cnt = 1


def comparison(left, right, x, y):
    if y == 0:
        return max(abs(left - x), abs(right - x))

    if right == 0:
        return abs(left*2 - x - y)
    else:
        return abs(left+right-x-y)


while deq:
    if len(deq) == 1:
        x = deq.pop()
        y = 0
    else:
        if cnt:
            x = deq.pop()
            y = deq.pop()
        else:
            x = deq.popleft()
            y = deq.popleft()

    ans += comparison(left, right, x, y)
    left, right = x, y
    cnt = 1-cnt

print(ans)


Failing test case input:
5
5
-4
-16
0
11

Expected output:
68


Actual output:
63


Fixed submission (verdict: Accepted):
from collections import deque
n = int(input())
a = [int(input()) for _ in range(n)]
a.sort()
x = deque(a)
y = deque(a)


def align(a, fromLeft=1):
    if fromLeft:
        p = a.popleft()
    else:
        p = a.pop()
    q = p

    res = 0
    fromLeft = 1 - fromLeft
    while a:
        p1 = a.popleft() if fromLeft else a.pop()
        if a:
            q1 = a.popleft() if fromLeft else a.pop()
            res += abs(p - p1) + abs(q - q1)
            p = p1
            q = q1
        else:
            res += max(abs(p - p1), abs(q - p1))
        fromLeft = 1 - fromLeft
    return res


ans = max(align(x, 1), align(y, 0))
print(ans)

Problem: p03229
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
n=int(input())
a=sorted([int(input())for i in range(n)])
def f(x):
    p,q=1,n-2
    l,r=[a[0]],[a[n-1]]
    c=a[n-1]-a[0]
    t=0
    while p<=q:
        if t==x:
            r.append(a[q])
            c+=a[q]-l[0]
            q-=1
            l.pop()
        else:
            l.append(a[p])
            c+=r[0]-a[p]
            p+=1
            r.pop()
        t=(t+1)%2
    return c
print(max(f(0),f(1)))

Failing test case input:
6
6
1
7
1
1
9

Expected output:
33


Actual output:
35


Fixed submission (verdict: Accepted):
n=int(input())
a=sorted([int(input())for i in range(n)])
def f(x):
    p,q=1,n-2
    l,r=[a[0]],[a[n-1]]
    c=a[n-1]-a[0]
    t=0
    while p<=q:
        if t==x:
            r.append(a[q])
            c+=a[q]-l[0]
            q-=1
            l.pop(0)
        else:
            l.append(a[p])
            c+=r[0]-a[p]
            p+=1
            r.pop(0)
        t=(t+1)%2
    return c
print(max(f(0),f(1)))
Problem: p03229
Buggy submission (PyPy3 (7.3.0), verdict: Wrong Answer):
from collections import deque

N=int(input())
A=[0]*N
for i in range(N):
    A[i]=int(input())

Q=deque(sorted(A))
R=deque([])

for i in range(N):
    if i%2:
        a=Q.popleft()
    else:
        a=Q.pop()

    if i>=2:
        b,c=R[0],R[-1]
        if abs(a-b)<=abs(a-c):
            R.append(a)
        else:
            R.appendleft(a)
    else:
        R.append(a)

    assert 1

K=0
for i in range(N-1):
    K+=abs(R[i]-R[i+1])
print(K)

Failing test case input:
5
75
-2
1
-2
-3

Expected output:
162


Actual output:
156


Fixed submission (verdict: Accepted):

from collections import deque

N=int(input())
A=[0]*N
for i in range(N):
    A[i]=int(input())
A.sort()

Q=deque(range(1,N))
R=deque([0])
for i in range((N-1)
    if i%2:
        x=Q.popleft()
        y=Q.popleft()
    else:
        x=Q.pop()
        y=Q.pop()

    R.appendleft(x)
    R.append(y)

if Q:
    x=Q.pop()
    R.appendleft(x)

X=[0]*N
Y=[0]*N
for i in range(N):
    X[i]=A[R[i]]
    Y[i]=A[-(R[i]+1)]

alpha=0
beta=0

for i in range(N-1):
    alpha+=abs(X[i+1]-X[i])
    beta +=abs(Y[i+1]-Y[i])
print(max(alpha,beta))

Problem: p03229
Buggy submission (PyPy3 (2.4.0), verdict: Wrong Answer):
from collections import deque
N = int(input())
A = [int(input()) for _ in range(N)]
A.sort()
d = deque(A)



ans = []

for i in range(N):
    x = d.pop() if i % 2 == 0 else d.popleft()
    ans.append(x)


def amount(array):
    ret = 0
    for i in range(N-1):
        ret += abs(array[i+1]-array[i])
    return ret

ret = amount(ans)    
x = ans.pop()
ans = [x] + ans


ret = max(ret, amount(ans))

print(ret)


Failing test case input:
5
6
4
1
0
3

Expected output:
17


Actual output:
16


Fixed submission (verdict: Accepted):
from collections import deque
N = int(input())
A = [int(input()) for _ in range(N)]
A.sort()
d = deque(A)



ans1 = []

for i in range(N):
    x = d.pop() if i % 2 == 0 else d.popleft()
    ans1.append(x)
x = ans1.pop()
ans1 = [x] + ans1

ans2 = []
d = deque(A)
for i in range(N):
    x = d.popleft() if i % 2 == 0 else d.pop()
    ans2.append(x)
x = ans2.pop()
ans2 = [x] + ans2


def amount(array):
    ret = 0
    for i in range(N-1):
        ret += abs(array[i+1]-array[i])
    return ret


print(max(amount(ans1), amount(ans2)))
Problem: p03229
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
n = int(input())
a = []
for _ in range(n):
  a.append(int(input()))
a.sort()
i = 1
j = n-1
s = 0
x = y = min(a)
r = True
while i<j:
  if r:
    s += abs(a[j] - x) + abs(a[j-1] - y)
    x = a[j]
    y = a[j-1]
    j -= 2
  else:
    s += abs(a[i] - x) + abs(a[i+1] - y)
    x = a[i]
    y = a[i+1]
    i += 2
  r = not r
if i == j:
  s += max(abs(a[i] - x), abs(a[i]-y))
print(s)


Failing test case input:
3
-2
2
-2

Expected output:
8


Actual output:
4


Fixed submission (verdict: Accepted):
n = int(input())
a = []
for _ in range(n):
  a.append(int(input()))

def solve2(a):
  i = 1
  j = n-1
  s = 0
  x = y = a[0]
  r = True
  while i<j:
    if r:
      s += abs(a[j] - x) + abs(a[j-1] - y)
      x = a[j]
      y = a[j-1]
      j -= 2
    else:
      s += abs(a[i] - x) + abs(a[i+1] - y)
      x = a[i]
      y = a[i+1]
      i += 2
    r = not r
  if i == j:
    s += max(abs(a[i] - x), abs(a[i]-y))
  return s

print(max(solve2(sorted(a)), solve2(sorted(a, reverse=True))))

Problem: p03229
Buggy submission (PyPy3 (7.3.0), verdict: Wrong Answer):
import math
n = int(input())

A = [int(input()) for i in range(n)]

A.sort()

half = math.floor(n/2)

max_A = max(A)

half_A = A[half]

if (n%2 == 1) & (half_A<max_A/2):
    total = sum(A[half+1:])*2 - sum(A[:half-1])*2 -A[half] - A[half-1]
elif (n % 2 == 1) & (half_A >= max_A / 2):
    total = sum(A[half+2:])*2 - sum(A[:half])*2 + A[half] + A[half+1]
else:

    total = sum(A[half+1:])*2 - sum(A[:half-1])*2 -A[half-1] + A[half]

print(total)


Failing test case input:
5
75
-2
1
-2
0

Expected output:
159


Actual output:
158


Fixed submission (verdict: Accepted):
import math
n = int(input())

A = [int(input()) for i in range(n)]

A.sort()

half = math.floor(n/2)
if n ==2:
    print(A[1]-A[0])
else:
    half_A = A[half]
    dif = A[half+1] - half_A*2 +A[half-1]
    if (n%2 == 1) & (dif>0):
        total = sum(A[half+1:])*2 - sum(A[:half-1])*2 -A[half] - A[half-1]
    elif (n % 2 == 1) & (dif<=0):
        total = sum(A[half+2:])*2 - sum(A[:half])*2 + A[half] + A[half+1]
    else:
        total = sum(A[half+1:])*2 - sum(A[:half-1])*2 -A[half-1] + A[half]
    
    print(total)
Problem: p03229
Buggy submission (Python (3.8.2), verdict: Wrong Answer):
N = int(input())
A = [int(input()) for _ in range(N)]

A.sort()

l = 0
r = N-1
p = []
ans = 0
while l < r:
    p.append(A[r])
    p.append(A[l])
    r -= 1
    l += 1
if N % 2 == 1:
    p.append(A[r])

p.append(p[0])
q = []
for i in range(len(p)-1):
    q.append(abs(p[i] - p[i+1]))

ans = sum(q)-min(q)

l = 0
r = N-1
p = []
ans = 0
while l < r:
    p.append(A[l])
    p.append(A[r])
    r -= 1
    l += 1
if N % 2 == 1:
    p.append(A[r])

p.append(p[0])
q = []
for i in range(len(p)-1):
    q.append(abs(p[i] - p[i+1]))

print(max(ans,sum(q)-min(q)))

Failing test case input:
5
6
4
0
-2
0

Expected output:
24


Actual output:
22


Fixed submission (verdict: Accepted):
N = int(input())
A = [int(input()) for _ in range(N)]

A.sort()

l = 0
r = N-1
p = []
ans = 0
while l < r:
    p.append(A[r])
    p.append(A[l])
    r -= 1
    l += 1
if N % 2 == 1:
    p.append(A[r])

p.append(p[0])
q = []
for i in range(len(p)-1):
    q.append(abs(p[i] - p[i+1]))

ans = sum(q)-min(q)

l = 0
r = N-1
p = []
while l < r:
    p.append(A[l])
    p.append(A[r])
    r -= 1
    l += 1
if N % 2 == 1:
    p.append(A[r])

p.append(p[0])
q = []
for i in range(len(p)-1):
    q.append(abs(p[i] - p[i+1]))

print(max(ans,sum(q)-min(q)))

Problem: p03229
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
import sys
input = sys.stdin.readline

N = int(input())
A = [int(input()) for _ in range(N)]

ans = 0
A.sort()

tmp1 = A[0]
tmp2 = A[0]

count = 1
flag = True
i = 1
j = 1
while True:
    if count == N:
        break
    if flag:
        ans += abs(tmp1 - A[-i])
        tmp1 = A[-i]
        i += 1
    else:
        ans += abs(tmp1 - A[j])
        tmp1 = A[j]
        j += 1

    count += 1

    if count == N:
        break
    if flag:
        ans += abs(tmp2 - A[-i])
        tmp2 = A[-i]
        i += 1
    else:
        ans += abs(tmp2 - A[j])
        tmp2 = A[j]
        j += 1
    
    count += 1

    flag = not flag
    


print (ans)


Failing test case input:
5
19
-2
-31
0
0

Expected output:
104


Actual output:
102


Fixed submission (verdict: Accepted):
import sys
input = sys.stdin.readline

N = int(input())
A = [int(input()) for _ in range(N)]


A.sort()

def calc(A):
    ans = 0
    tmp1 = A[0]
    tmp2 = A[0]

    count = 1
    flag = True
    i = 1
    j = 1
    while True:
        if flag:
            if tmp1 > tmp2:
                tmp1, tmp2 = tmp2, tmp1
        else:
            if tmp1 < tmp2:
                tmp1, tmp2 = tmp2, tmp1


        if count == N:
            break
        if flag:
            ans += abs(tmp1 - A[-i])
            tmp1 = A[-i]
            i += 1
        else:
            ans += abs(tmp1 - A[j])
            tmp1 = A[j]
            j += 1

        count += 1

        if count == N:
            break
        if flag:
            ans += abs(tmp2 - A[-i])
            tmp2 = A[-i]
            i += 1
        else:
            ans += abs(tmp2 - A[j])
            tmp2 = A[j]
            j += 1
        
        count += 1

        flag = not flag
        
    return ans


print (max(calc(A[::-1]), calc(A)))
Problem: p03229
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
import sys
from collections import deque
read = sys.stdin.read

N, *A = map(int, read().split())

A.sort()
A = deque(A)
a = deque()
answer = 0
side = True
a.append(A.popleft())

while A:
    if side:
        a.append(A.pop())
        if A:
            a.appendleft(A.pop())
        side = False
    else:
        a.appendleft(A.popleft())
        if A:
            a.append(A.popleft())
        side = True

a = list(a)
for i, j in zip(a, a[1:]):
    answer += abs(i - j)

print(answer)

Failing test case input:
5
37
-2
0
-3
1

Expected output:
85


Actual output:
84


Fixed submission (verdict: Accepted):
import sys
from collections import deque
read = sys.stdin.read

N, *A = map(int, read().split())

A.sort()
A = deque(A)
a = deque()
side = True
a.append(A.popleft())

while A:
    if side:
        a.append(A.pop())
        if A:
            a.appendleft(A.pop())
        side = False
    else:
        a.append(A.popleft())
        if A:
            a.appendleft(A.popleft())
        side = True

if side:
    tmp = a.popleft()
else:
    tmp = a.pop()

if abs(tmp - a[0]) <= abs(tmp - a[-1]):
    a.append(tmp)
else:
    a.appendleft(tmp)

if not side:
    tmp = a.popleft()
else:
    tmp = a.pop()

if abs(tmp - a[0]) <= abs(tmp - a[-1]):
    a.append(tmp)
else:
    a.appendleft(tmp)

answer = 0
a = list(a)
for i, j in zip(a, a[1:]):
    answer += abs(i - j)

print(answer)

Problem: p03229
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
from collections import deque
n = int(input())
a = [int(input()) for _ in range(n)]
a.sort()
que = deque(a)
right = que.pop()
left = que.popleft()
sums = right - left
while(len(que)>0):
  a = que.pop()
  if abs(a-right) > abs(a-left):
    sums += abs(a-right)
    right = a
  else:
    sums += abs(a-left)
    left = a
  if not bool(que):
    break
  b = que.popleft()
  if abs(b-right) > abs(b-left):
    sums += abs(b-right)
    right = b
  else:
    sums += abs(b-left)
    left = b
print(sums)

Failing test case input:
6
0
-1
20
0
0
1

Expected output:
44


Actual output:
42


Fixed submission (verdict: Accepted):
from collections import deque
n = int(input())
a = [int(input()) for _ in range(n)]
a.sort()
que = deque(a)
right = que.pop()
left = que.popleft()
sums = right - left
while(bool(que)):
  a = que.pop()
  if not bool(que):
    sums += max(abs(right-a),abs(left-a))
    break
  b = que.popleft()
  sums += right + a - left -b
  right = a
  left = b

print(sums)
Problem: p03229
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
N=int(input())

A=[int(input()) for i in range(N)]

A.sort()

ansl=A[0]
ansr=A[-1]
l=1
r=-2
n=[0]*4
ansn=abs(A[0]-A[-1])
for i in range(N-2):
    n[0]=abs(A[l]-ansl)
    n[1]=abs(A[l]-ansr)
    n[2]=abs(A[r]-ansl)
    n[3]=abs(A[r]-ansr)
    if n[0]==max(n):
        ansn+=n[0]
        ansl=A[l]
        l+=1
    elif n[1]==max(n):
        ansn+=n[1]
        ansl=A[l]
        l+=1
    elif n[2]==max(n):
        ansn+=n[2]
        ansr=A[r]
        r-=1
    elif n[3]==max(n):
        ansn+=n[3]
        ansr=A[r]
        r-=1

print(ansn)

Failing test case input:
5
75
-3
1
-2
0

Expected output:
161


Actual output:
304


Fixed submission (verdict: Accepted):
N=int(input())

A=[int(input()) for i in range(N)]

A.sort()

ansl=A[0]
ansr=A[-1]
l=1
r=-2
n=[0]*4
ansn=abs(A[0]-A[-1])
for i in range(N-2):
    n[0]=abs(A[l]-ansl)
    n[1]=abs(A[l]-ansr)
    n[2]=abs(A[r]-ansl)
    n[3]=abs(A[r]-ansr)
    if n[0]==max(n):
        ansn+=n[0]
        ansl=A[l]
        l+=1
    elif n[1]==max(n):
        ansn+=n[1]
        ansr=A[l]
        l+=1
    elif n[2]==max(n):
        ansn+=n[2]
        ansl=A[r]
        r-=1
    elif n[3]==max(n):
        ansn+=n[3]
        ansr=A[r]
        r-=1

print(ansn)

Problem: p03229
Buggy submission (PyPy3 (2.4.0), verdict: Wrong Answer):
import sys
input = sys.stdin.readline
from operator import itemgetter
sys.setrecursionlimit(10000000)
from collections import deque

def main():
    n = int(input().strip())
    a = [0] * n
    for i in range(n):
        a[i] = int(input().strip())
    a = deque(sorted(a))

    d = deque()
    for i in range(n):
        e = -20
        if i % 2 == 0:
            e = a.popleft()
        else:
            e = a.pop()
        if len(d) > 0:
            if abs(e - d[0]) > abs(e - d[-1]):
                d.appendleft(e)
            else:
                d.append(e)
        else:
            d.append(e)
    ans = 0
    d = list(d)
    for i in range(n-1):
        ans += abs(d[i] - d[i+1])
    print(ans)

if __name__ == '__main__':
    main()


Failing test case input:
5
19
-2
-31
0
0

Expected output:
104


Actual output:
100


Fixed submission (verdict: Accepted):
import sys
input = sys.stdin.readline
from operator import itemgetter
sys.setrecursionlimit(10000000)

def main():
    n = int(input().strip())
    a = [0] * n
    for i in range(n):
        a[i] = int(input().strip())
    a = sorted(a)
    ans = 0
    if n % 2 == 0:
        kei1 = sorted([1, -1] + [-2] * ((n-2)
        for i in range(n):
            ans += a[i] * kei1[i]
    else:
        kei1 = sorted([-1] * 2 + [2] * (n
        kei2 = sorted([1] * 2 + [2] * (n
        ans2 = 0
        for i in range(n):
            ans += a[i] * kei1[i]
            ans2 += a[i] * kei2[i]
        ans = max(ans, ans2)

    print(ans)

if __name__ == '__main__':
    main()
Problem: p03229
Buggy submission (Python (3.8.2), verdict: Wrong Answer):
N = int(input())
A = [int(input()) for _ in range(N)]

A.sort()

l = 0
r = N-1
ans = 0
p = []
while l < r:
    p.append(A[r])
    p.append(A[l])
    r -= 1
    l += 1
if N % 2 == 1:
    p.append(A[r])

p += p
q = []

for i in range(len(p)-1):
    q.append(abs(p[i]-p[i+1]))

sum = 0
for i in range(len(q)):
    sum += q[i]
    if i >= N-1:
        sum -= q[i-N+1]
    ans = max(ans,sum)

print(ans)

Failing test case input:
5
89
-1
-2
-2
-1

Expected output:
184


Actual output:
183


Fixed submission (verdict: Accepted):
N = int(input())
A = [int(input()) for _ in range(N)]

A.sort()

l = 0
r = N-1
p = []
ans = 0
while l < r:
    p.append(A[r])
    p.append(A[l])
    r -= 1
    l += 1
if N % 2 == 1:
    p.append(A[r])

p.append(p[0])
q = []
for i in range(len(p)-1):
    q.append(abs(p[i] - p[i+1]))

ans = sum(q)-min(q)

l = 0
r = N-1
p = []
while l < r:
    p.append(A[l])
    p.append(A[r])
    r -= 1
    l += 1
if N % 2 == 1:
    p.append(A[r])

p.append(p[0])
q = []
for i in range(len(p)-1):
    q.append(abs(p[i] - p[i+1]))

print(max(ans,sum(q)-min(q)))

Problem: p03229
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
n = int(input())
a = [0 for i in range(n)]
for i in range(n):
    a[i] = int(input())

a = sorted(a)
sum = 0

if n % 2 == 1:
    if a[0] == a[int(n/2)-1] and a[-1] == a[int(n/2)]:
        sum = (a[-1]-a[0])*(n-1)
    else:
        for i in range(int(n/2)+1, n):
            sum += a[i] * 2
        sum -= a[int(n / 2)-1]
        sum -= a[int(n / 2)]
        for i in range(int(n / 2)-1):
            sum -= a[i]*2
    print(sum)
else:
    sum += a[int(n / 2)]
    for i in range(int(n / 2)+1, n):
        sum += a[i] * 2
    sum -= a[int(n / 2) - 1]
    for i in range(int(n / 2) - 1):
        sum -= a[i] * 2
    print(sum)


Failing test case input:
5
37
-5
-17
0
-1

Expected output:
117


Actual output:
114


Fixed submission (verdict: Accepted):
n = int(input())
a = [0 for i in range(n)]
for i in range(n):
    a[i] = int(input())

a = sorted(a)
sum = 0

if n % 2 == 1:
    sum1 = 0
    for i in range(int(n/2)+1, n):
        sum1 += a[i] * 2
    sum1 -= a[int(n / 2)-1]
    sum1 -= a[int(n / 2)]
    for i in range(int(n / 2)-1):
        sum1 -= a[i]*2

    sum2 = 0
    for i in range(int(n/2), n):
        sum2 += a[i] * 2
    sum2 -= a[int(n / 2)]
    sum2 -= a[int(n / 2)+1]
    for i in range(int(n / 2)):
        sum2 -= a[i]*2
    print(max(sum1, sum2))
else:
    sum += a[int(n / 2)]
    for i in range(int(n / 2)+1, n):
        sum += a[i] * 2
    sum -= a[int(n / 2) - 1]
    for i in range(int(n / 2) - 1):
        sum -= a[i] * 2
    print(sum)
Problem: p03229
Buggy submission (PyPy3 (2.4.0), verdict: Wrong Answer):

from collections import deque
n = int(input())
nums = []
for _ in range(n):
    nums.append(int(input()))
nums.sort()
ans = 0
l = 0
r = n - 1
q = deque()
while l <= r:
    left = nums[l]
    right = nums[r]
    if not q:
        ans += abs(right - left)
        q.append(left)
        q.append(right)
    else:
        if abs(q[-1] - right) > abs(q[0] - right):
            ans += abs(q[-1] - right)
            q.append(right)
        else:
            ans += abs(q[0] - right)
            q.appendleft(right)
        if l == r:
            break
        if abs(q[-1] - left) > abs(q[0] - left):
            ans += abs(q[-1] - left)
            q.append(left)
        else:
            ans += abs(q[0] - left)
            q.appendleft(left)
    l += 1
    r -= 1
print(ans)

Failing test case input:
5
75
-2
1
-2
0

Expected output:
159


Actual output:
156


Fixed submission (verdict: Accepted):

from collections import deque
n = int(input())
nums = []
for _ in range(n):
    nums.append(int(input()))
nums.sort()
ans = 0
l = 0
r = n - 1
q = deque()
while l <= r:
    left = nums[l]
    right = nums[r]
    if not q:
        ans += abs(right - left)
        q.append(left)
        q.append(right)
        l += 1
        r -= 1
    else:
        s = max(abs(q[-1] - right), abs(q[0] - right))
        t = max(abs(q[-1] - left), abs(q[0] - left))
        if s >= t:
            if abs(q[-1] - right) > abs(q[0] - right):
                ans += abs(q[-1] - right)
                q.append(right)
            else:
                ans += abs(q[0] - right)
                q.appendleft(right)
            r -= 1
        else:
            if abs(q[-1] - left) > abs(q[0] - left):
                ans += abs(q[-1] - left)
                q.append(left)
            else:
                ans += abs(q[0] - left)
                q.appendleft(left)
            l += 1
print(ans)

Problem: p03229
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
from collections import*
n,*a=map(int,open(0).read().split())
def f(a):
  a=deque(a)
  deq=deque([a.popleft()])
  for i in range(n-1):
    if i%4==0:
      deq.appendleft(a.pop())
    elif i%4==1:
      deq.append(a.pop())
    elif i%4==2:
      deq.appendleft(a.popleft())
    else:
      deq.append(a.popleft())
  l=list(deq)
  return sum(abs(i-j)for i,j in zip(l,l[1:]))
s=sorted(a)
print(max(f(a),f(a[::-1])))

Failing test case input:
5
115
-1
0
-1
0

Expected output:
234


Actual output:
233


Fixed submission (verdict: Accepted):
from collections import*
n,*a=map(int,open(0).read().split())
def f(a):
  a=deque(a)
  deq=deque([a.popleft()])
  for i in range(n-1):
    if i%4==0:
      deq.appendleft(a.pop())
    elif i%4==1:
      deq.append(a.pop())
    elif i%4==2:
      deq.appendleft(a.popleft())
    else:
      deq.append(a.popleft())
  l=list(deq)
  return sum(abs(i-j)for i,j in zip(l,l[1:]))
s=sorted(a)
print(max(f(s),f(s[::-1])))
Problem: p03229
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
from collections import deque

N = int(input())
A = [int(input()) for _ in range(N)]

A.sort()
array = deque([A[0]])
a_len = 1
count = 0
idx = 0
result = 0

while a_len < N:
    if count % 2 == 0:
        array.appendleft(A[-idx-1])
        a_len += 1
        if a_len >= N:
            break
        array.append(A[-idx-2])
        a_len += 1
    else:
        array.appendleft(A[idx+1])
        a_len += 1
        if a_len >= N:
            break
        array.append(A[idx+2])
        a_len += 1
        idx += 2
    count += 1

for i in range(N-1):
    result += abs(array[i] - array[i+1])

print(result)

Failing test case input:
5
37
-2
0
-3
1

Expected output:
85


Actual output:
84


Fixed submission (verdict: Accepted):
from collections import deque

N = int(input())
A = [int(input()) for _ in range(N)]

A.sort()
array = deque([A[0]])
a_len = 1
count = 0
idx = 0
result1 = 0

while a_len < N:
    if count % 2 == 0:
        array.appendleft(A[-idx-1])
        array.append(A[-idx-2])
    else:
        array.appendleft(A[idx+1])
        array.append(A[idx+2])
        idx += 2
    count += 1
    a_len += 2

for i in range(N-1):
    result1 += abs(array[i] - array[i+1])

array = deque([A[-1]])
a_len = 1
count = 0
idx = 0
result2 = 0

while a_len < N:
    if count % 2 != 0:
        array.appendleft(A[-idx])
        array.append(A[-idx-1])
    else:
        array.appendleft(A[idx])
        array.append(A[idx+1])
        idx += 2
    count += 1
    a_len += 2

for i in range(N-1):
    result2 += abs(array[i] - array[i+1])

print(max(result1, result2))
Problem: p03388
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
import math
def near_sqrt(a,b):
  c = a * b
  d = math.floor(math.sqrt(c))
  if d*(d+1) <= c:
    return d
  else:
    return (d-1)
  
q = int(input())
for i in range(q):
  a,b = map(int, input().split())
  c = near_sqrt(a,b)
  if (a*b)%(c+1) == 0:
    print(math.floor((a*b)/(c+1))+c-2)
  else:
    print(math.floor((a*b)/(c+1))+c-1)

Failing test case input:
8
1 4
10 5
1 1
1 11
8 6
1 5
8 36
314159265 358979323

Expected output:
1
12
0
4
11
2
31
671644785


Actual output:
1
12
-1
4
11
2
31
671644785


Fixed submission (verdict: Accepted):
import math
def near_sqrt(a,b):
  c = a * b
  d = math.floor(math.sqrt(c))
  if d*(d+1) < c:
    return d
  else:
    return (d-1)
  
q = int(input())
for i in range(q):
  a,b = map(int, input().split())
  c = near_sqrt(a,b)
  d = (a*b)%(c+1)
  if d == 0 and a!=b:
    print(math.floor((a*b)/(c+1))+c-2)
  else:
    print(math.floor((a*b)/(c+1))+c-1)

Problem: p03388
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
from math import sqrt

q = int(input())

p = []
for _ in range(q):
    p.append(list(map(int, input().split())))

for t in p:
    x = min(t)
    y = max(t)
    if x == y or x + 1 == y:
        print(x * 2 - 2)
    else:
        temp = sqrt(x * y)
        hoge = int(temp)
        if hoge == temp:
            hoge -= 1
        if hoge * (hoge + 1) > x * y:
            print(2 * hoge - 2)
        else:
            print(2 * hoge - 1)



Failing test case input:
8
2 4
10 9
3 1
5 11
8 9
16 1
6 57
314159265 75457060

Expected output:
3
16
1
12
14
5
34
307932032


Actual output:
3
16
1
12
14
5
35
307932032


Fixed submission (verdict: Accepted):
from math import sqrt

q = int(input())

p = []
for _ in range(q):
    p.append(list(map(int, input().split())))

for t in p:
    x = min(t)
    y = max(t)
    if x == y or x + 1 == y:
        print(x * 2 - 2)
    else:
        temp = sqrt(x * y)
        hoge = int(temp)
        if hoge == temp:
            hoge -= 1
        if hoge * (hoge + 1) >= x * y:
            print(2 * hoge - 2)
        else:
            print(2 * hoge - 1)
Problem: p03388
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
from math import sqrt

q = int(input())
query = [tuple(map(int, input().split())) for _ in range(q)]


def sqrtfloor(x):
    s = int(sqrt(x))
    if s ** 2 == x:
        s -= 1
    return s


def solve(a, b):
    a, b = min(a, b), max(a, b)
    if a == b or a + 1 == b:
        return 2 * a - 1
    else:
        s = sqrtfloor(a * b)
        if s * (s + 1) < a * b:
            return 2 * s - 1
        else:
            return 2 * s - 2


for a, b in query:
    print(solve(a, b))


Failing test case input:
8
1 4
10 5
3 3
4 11
8 9
22 11
8 36
314159265 358979323

Expected output:
1
12
4
11
14
29
31
671644785


Actual output:
1
12
5
11
15
29
31
671644785


Fixed submission (verdict: Accepted):
from math import sqrt

q = int(input())
query = [tuple(map(int, input().split())) for _ in range(q)]


def sqrtfloor(x):
    s = int(sqrt(x))
    if s ** 2 == x:
        s -= 1
    return s


def solve(a, b):
    a, b = min(a, b), max(a, b)
    if a == b or a + 1 == b:
        return 2 * a - 2
    else:
        s = sqrtfloor(a * b)
        if s * (s + 1) < a * b:
            return 2 * s - 1
        else:
            return 2 * s - 2


for a, b in query:
    print(solve(a, b))

Problem: p03388
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
import math
n=int(input())
for _ in range(n):
    a,b=map(int,input().split())
    c=int(math.sqrt(a*b))
    if c<2 :
        print('0')
    elif math.sqrt(a*b)==c and a!=b:
        print(2*c-3)
    elif c*(c+1)<a*b:
        print(2*c-1)
    else:
        print(2*c-2)

Failing test case input:
8
1 4
10 5
1 1
1 11
8 6
1 3
8 36
314159265 358979323

Expected output:
1
12
0
4
11
1
31
671644785


Actual output:
1
12
0
4
11
0
31
671644785


Fixed submission (verdict: Accepted):
import math
n=int(input())
for _ in range(n):
    a,b=map(int,input().split())
    c=int(math.sqrt(a*b))
    if c*(c+1)<a*b: print(2*c-1)
    elif a*b==c*c and a!=b:  print(2*c-3)
    else:  print(2*c-2)

Problem: p03388
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
import math

def worst_case(A, B):
    c = min(A, B)
    d = max(A, B)
    if c == d or c + 1 == d:
        return 2 * (c - 1)
    root = int(math.sqrt(A * B))
    return 2 * (root - 1) + 1 if root * (root + 1) < A * B else 2 * (root - 1)

Q = int(input())
A = []
B = []
for i in range(Q):
    line = input().split()
    A.append(int(line[0]))
    B.append(int(line[1]))

for i in range(Q):
    print(worst_case(A[i], B[i]))

Failing test case input:
8
1 4
10 5
3 3
4 11
8 9
1 3
8 36
314159265 358979323

Expected output:
1
12
4
11
14
1
31
671644785


Actual output:
2
12
4
11
14
1
31
671644785


Fixed submission (verdict: Accepted):
import math

def worst_case(A, B):
    c = min(A, B)
    d = max(A, B)
    if c == d or c + 1 == d:
        return 2 * (c - 1)
    root = int(math.sqrt(A * B))
    if root ** 2 == A * B:
        root -= 1
    return 2 * (root - 1) + 1 if root * (root + 1) < A * B else 2 * (root - 1)

Q = int(input())
A = []
B = []
for i in range(Q):
    line = input().split()
    A.append(int(line[0]))
    B.append(int(line[1]))

for i in range(Q):
    print(worst_case(A[i], B[i]))
Problem: p03388
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
Q = int(input())
ans = []
for q in range(Q):
    A, B = map(int, input().split())
    M = int((A*B)**0.5)
    tmp = 2*M - 2
    tmp -= (A < M) or (B < M)
    if A != B and A*B != M*M:
        if M*(M+1) < A*B:
            tmp += 2
        else:
            tmp += 1
    ans.append(str(tmp))

print("\n".join(ans))

Failing test case input:
8
2 4
7 9
3 5
1 11
8 9
16 1
3 41
314159265 339704264

Expected output:
3
13
5
4
14
5
20
653364343


Actual output:
4
14
6
4
15
5
20
653364343


Fixed submission (verdict: Accepted):
Q = int(input())
ans = []
for q in range(Q):
    A, B = map(int, input().split())
    M = int((A*B)**0.5)
    if M*(M + 1) < A*B: 
        ans.append(2*M - 1)
    elif A*B == M*M and A != B: 
        ans.append(2*M - 3)
    else: 
        ans.append(2*M - 2)

print("\n".join(map(str, ans)))

Problem: p03388
Buggy submission (PyPy3 (7.3.0), verdict: Wrong Answer):
def main():
    n = int(input())
    for _ in range(n):
        a, b = map(int, input().split())
        ref = a * b - 1
        rng = int(ref ** 0.5) - 2
        while rng ** 2 < a * b:
            rng += 1
        if a == b or abs(a - b) == 1:
            print(2 * min(a, b) - 2)
        elif rng * (rng + 1) >= a * b:
            print(2 * rng - 2)
        else:
            print(2 * rng - 1)
if __name__ == '__main__':
    main()

Failing test case input:
8
1 4
10 5
4 3
4 11
8 9
22 6
7 36
314159265 96636036

Expected output:
1
12
4
11
14
20
29
348477291


Actual output:
2
14
4
12
14
22
30
348477292


Fixed submission (verdict: Accepted):
def main():
    n = int(input())
    for _ in range(n):
        a, b = map(int, input().split())
        ref = a * b - 1
        rng = int(ref ** 0.5) - 2
        while rng ** 2 < a * b:
            rng += 1
        rng -= 1
        if a == b or abs(a - b) == 1:
            print(2 * min(a, b) - 2)
        elif rng * (rng + 1) >= a * b:
            print(2 * rng - 2)
        else:
            print(2 * rng - 1)
if __name__ == '__main__':
    main()
Problem: p03388
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
Q = int(input())
Ri = [list(map(int, input().split())) for _ in range(Q)]

for r in Ri:
    a = r[0]
    b = r[1]
    if a > b:
        t = b
        b = a
        a = t
    max_score = a * b

    if a == b:
        print(2 * a - 2)
    elif a + 1 == b:
        print(2 * a - 2)
    else:
        r = max_score ** 0.5
        rn = int(r)
        if rn * (rn + 1) >= max_score:
            print(2 * rn - 2)
        else:
            print(2 * rn - 1)


Failing test case input:
8
7 4
14 9
3 7
5 17
8 9
16 1
2 41
314159265 541984339

Expected output:
8
20
7
16
14
5
16
825274259


Actual output:
8
20
7
16
14
6
16
825274259


Fixed submission (verdict: Accepted):
Q = int(input())
Ri = [list(map(int, input().split())) for _ in range(Q)]

for r in Ri:
    a = r[0]
    b = r[1]
    if a > b:
        t = b
        b = a
        a = t
    max_score = a * b

    if a == b:
        print(2 * a - 2)
    elif a + 1 == b:
        print(2 * a - 2)
    else:
        r = max_score ** 0.5
        rn = int(r)
        if rn == r:
            rn -= 1
        if rn * (rn + 1) >= max_score:
            print(2 * rn - 2)
        else:
            print(2 * rn - 1)

Problem: p03388
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
Q = int(input())
ans = []
for q in range(Q):
    A, B = map(int, input().split())
    M = int((A*B)**0.5)
    tmp = 2*M - 2
    tmp -= (A < M) or (B < M)
    if A != B and A*B != M*M:
        if M*(M+1) < A*B:
            tmp += 2
        elif M*M < A*B:
            tmp += 1
    ans.append(str(tmp))

print("\n".join(ans))

Failing test case input:
8
2 4
10 5
2 3
5 11
8 9
39 3
5 36
559979313 592567555

Expected output:
3
12
2
12
14
19
24
1152086057


Actual output:
4
12
3
12
15
19
24
1152086057


Fixed submission (verdict: Accepted):
Q = int(input())
ans = []
for q in range(Q):
    A, B = map(int, input().split())
    M = int((A*B)**0.5)
    if M*(M + 1) < A*B: 
        ans.append(2*M - 1)
    elif A*B == M*M and A != B: 
        ans.append(2*M - 3)
    else: 
        ans.append(2*M - 2)

print("\n".join(map(str, ans)))

Problem: p03388
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
import math

N = int(input().strip())
grid = []
for i in range(N):
  array = list(map(int, input().strip().split(' ')))
  grid.append(array)

for i in grid:
  i.sort()
  a,b = [i[0],i[1]]
  if(a == b or a+1 == b):
    print(2*a-2)
  else:
    c = int(math.sqrt(a*b))
    if(c*c < a*b):
      print(2*c-1)
    else:
      print(2*c-2)


Failing test case input:
8
2 4
6 11
3 1
5 11
4 9
16 1
6 57
314159265 75457060

Expected output:
3
14
1
12
9
5
34
307932032


Actual output:
3
15
1
13
10
6
35
307932033


Fixed submission (verdict: Accepted):
import math

N = int(input().strip())
grid = []
for i in range(N):
	array = list(map(int, input().strip().split(' ')))
	grid.append(array)

for i in grid:
	i.sort()
	a,b = [i[0],i[1]]
	if(a == b or a+1 == b):
		print(2*a-2)
	else:
		c = int(math.sqrt(a*b))
		if c**2 == a*b:
			c-=1
		if(c*(c+1)>=a*b):
			print(2*c-2)
		else:
			print(2*c-1)
Problem: p03388
Buggy submission (Python (3.4.3), verdict: Wrong Answer):


q=int(input())
A=[]
for i in range(q):
    A.append(list(map(int,input().split())))
    A[i].sort()








for i in range(q):
    a=A[i][0]
    b=A[i][1]
    if a==b :
        ans1=2*(a-1)
    elif a+1==b :
        ans1=2*a-2
    else :
        c=int((a*b-1)**0.5)+2
        while c*c>=a*b :
            c-=1
        
        if c*(c+1)>=a*b :
            ans1=2*c-2
        else :
            ans1=2*c-1

print(ans1)


Failing test case input:
8
2 4
10 14
3 7
5 5
8 9
31 1
3 36
314159265 345511311

Expected output:
3
21
7
8
14
9
18
658925121


Actual output:
658925121


Fixed submission (verdict: Accepted):


q=int(input())
A=[]
for i in range(q):
    A.append(list(map(int,input().split())))
    A[i].sort()








for i in range(q):
    a=A[i][0]
    b=A[i][1]
    if a==b :
        ans1=2*(a-1)
    elif a+1==b :
        ans1=2*a-2
    else :
        c=int((a*b-1)**0.5)+2
        while c*c>=a*b :
            c-=1
        
        if c*(c+1)>=a*b :
            ans1=2*c-2
        else :
            ans1=2*c-1

    print(ans1)

Problem: p03388
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
import math

Q = int(input())

AB = []
for _ in range(Q) :
    a, b = map(int, input().split())
    if a > b :
        a, b = b, a
    AB.append((a, b))

for a, b in AB :
    if a == b :
        print(2 * a - 2)
    elif (a + 1) == b :
        print(2 * a - 2)
    else :
        C = int(math.sqrt(a * b))
        if C * (C + 1) >= a * b :
            print(2 * C - 2)
        else :
            print(2 * C - 1)


Failing test case input:
8
1 4
10 5
3 3
4 11
8 9
22 3
8 36
314159265 539534208

Expected output:
1
12
4
11
14
14
31
823406751


Actual output:
2
12
4
11
14
14
31
823406751


Fixed submission (verdict: Accepted):
import math

Q = int(input())

AB = []
for _ in range(Q) :
    a, b = map(int, input().split())
    if a > b :
        a, b = b, a
    AB.append((a, b))

for a, b in AB :
    if a == b :
        print(2 * a - 2)
        continue

    C = int(math.sqrt(a * b))
    if C * C == a * b :
        C -= 1

    if C * (C + 1) >= a * b :
        print(2 * C - 2)
    else :
        print(2 * C - 1)

Problem: p03388
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
t= int(input())
for i in range(t):
    k=input().split()
    a=int(k[0])
    b=int(k[1])
    c=a*b
    d=int(c**.5)
    print(2*d-1-((d*(d+1))==c)-(c==d*d and a!=b))

Failing test case input:
8
2 4
11 5
3 7
9 11
8 9
22 1
3 36
624420745 617963358

Expected output:
3
12
7
17
14
7
18
1242367319


Actual output:
3
13
7
17
14
7
19
1242367319


Fixed submission (verdict: Accepted):
t= int(input())
for i in range(t):
    k=input().split()
    a=int(k[0])
    b=int(k[1])
    c=a*b
    d=int(c**.5)
    print(2*d-1-((d*(d+1))>=c)-(c==d*d and a!=b))