ZhenlongDai/ACPR huggingface dataset:
Problem: p02281
Buggy submission (Python3, verdict: Wrong Answer):
class Node:
    def __init__(self, id):
        self.id = id
        self.parent = None
        self.left = None
        self.right = None

root = None
n = int(input())
node_list = []
for i in range(n):
    node_list.append(Node(i))

for i in range(n):
    id, left, right = list(map(int, input().split()))
    node = node_list[id]
    if left > -1:
        node_list[left].parent = node
        node.left = node_list[left]
    if right > -1:
        node_list[right].parent = node
        node.right = node_list[right]

root = None
for node in node_list:
    if node.parent is None:
        root = node
        break

def preorder(node):
    if node is None:
        return
    print(' ' + str(node.id), end='')
    preorder(node.left)
    preorder(node.right)

def inorder(node):
    if node is None:
        return
    
    inorder(node.left)
    print(' ' + str(node.id), end='')
    inorder(node.right)

def postorder(node):
    if node is None:
        return
    print(' ' + str(node.id), end='')
    postorder(node.left)
    postorder(node.right)

print('Preorder')
preorder(root)
print('')

print('Inorder')
inorder(root)
print('')

print('Postorder')
postorder(root)
print('')


Failing test case input:
9
0 2 4
1 1 3
2 -1 -1
3 -1 -1
4 5 8
5 6 7
6 -1 -1
7 -1 -1
8 -1 -1

Expected output:
Preorder
 0 2 4 5 6 7 8
Inorder
 2 0 6 5 7 4 8
Postorder
 2 6 7 5 8 4 0


Actual output:
Preorder
 0 2 4 5 6 7 8
Inorder
 2 0 6 5 7 4 8
Postorder
 0 2 4 5 6 7 8


Fixed submission (verdict: Accepted):
class Node:
    def __init__(self, id):
        self.id = id
        self.parent = None
        self.left = None
        self.right = None

root = None
n = int(input())
node_list = []
for i in range(n):
    node_list.append(Node(i))

for i in range(n):
    id, left, right = list(map(int, input().split()))
    node = node_list[id]
    if left > -1:
        node_list[left].parent = node
        node.left = node_list[left]
    if right > -1:
        node_list[right].parent = node
        node.right = node_list[right]

root = None
for node in node_list:
    if node.parent is None:
        root = node
        break

def preorder(node):
    if node is None:
        return
    print(' ' + str(node.id), end='')
    preorder(node.left)
    preorder(node.right)

def inorder(node):
    if node is None:
        return
    inorder(node.left)
    print(' ' + str(node.id), end='')
    inorder(node.right)

def postorder(node):
    if node is None:
        return
    postorder(node.left)
    postorder(node.right)
    print(' ' + str(node.id), end='')

print('Preorder')
preorder(root)
print('')

print('Inorder')
inorder(root)
print('')

print('Postorder')
postorder(root)
print('')
Problem: p03716
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
N = int(input())
A = list(map(int,input().split()))
S = []
P = A[:N]
Q = A[2*N:]
for i in range(N):
    Q[i] *= -1
R = A[N:2*N]
p = [0]*(N+1)
q = [0]*(N+1)
import heapq
heapq.heapify(P)
heapq.heapify(Q)
sp = sum(P)
sq = sum(Q)
p[0] = sp
q[0] = sq

for i in range(1,N+1):
    heapq.heappush(P,R[i-1])
    heapq.heappop(P)
    sp = sum(P)
    p[i] = sum(Q)
for i in range(1,N+1):
    heapq.heappush(Q,-R[-i])
    heapq.heappop(Q)
    sq = sum(Q)
    q[i] = sq

ans = -(10**100)
for k in range(N+1):
    ans = max(ans,p[k] + q[N-k])
    
print(ans)


Failing test case input:
2
3 1 4 1 5 9

Expected output:
1

Actual output:
-1


Fixed submission (verdict: Accepted):
N = int(input())
A = list(map(int,input().split()))
S = []
P = A[:N]
Q = A[2*N:]
for i in range(N):
    Q[i] *= -1
R = A[N:2*N]
p = [0]*(N+1)
q = [0]*(N+1)
import heapq
heapq.heapify(P)
heapq.heapify(Q)
sp = sum(P)
sq = sum(Q)
p[0] = sp
q[0] = sq

for i in range(1,N+1):
    heapq.heappush(P,R[i-1])
    x = heapq.heappop(P)
    heapq.heappush(Q,-R[-i])
    y = heapq.heappop(Q)
    sp = sp + R[i-1] - x
    sq = sq - R[-i] - y
    p[i] = sp
    q[i] = sq
ans = -(10**100)
for k in range(N+1):
    ans = max(ans,p[k] + q[N-k])
    
print(ans)

Problem: p03716
Buggy submission (Python (3.4.3), verdict: Wrong Answer):


from heapq import heappush, heappop
import sys

N = int(input())
a = list(map(int,input().split()))

hq = []
hq_r = []
for i in range(N):
    heappush(hq,a[i])
    heappush(hq_r,-1*a[-1-i])


max_A = [0 for _ in range(N+1)]
max_A[0] = sum(hq)

min_C = [0 for _ in range(N+1)]
min_C[0] = sum(hq_r) * (-1)

for i in range(N):
    heappush(hq, a[N+i])
    heappush(hq_r, -1*a[-N-1-i])
    minus = heappop(hq)
    minus_r = heappop(hq_r)
    max_A[i+1] = max_A[i] + a[N+i] - minus
    min_C[i+1] = min_C[i] + a[-N-1-i] + minus_r
    
max_val = -10e+100
print(max_val)
for i in range(N+1):
    kari = max_A[i] - min_C[-1-i]
    if max_val < kari:
        max_val =kari
print(max_val)

Failing test case input:
2
3 1 4 1 5 9

Expected output:
1

Actual output:
-1e+101
1


Fixed submission (verdict: Accepted):


from heapq import heappush, heappop
import sys

N = int(input())
a = list(map(int,input().split()))

hq = []
hq_r = []
for i in range(N):
    heappush(hq,a[i])
    heappush(hq_r,-1*a[-1-i])


max_A = [0 for _ in range(N+1)]
max_A[0] = sum(hq)

min_C = [0 for _ in range(N+1)]
min_C[0] = sum(hq_r) * (-1)

for i in range(N):
    heappush(hq, a[N+i])
    heappush(hq_r, -1*a[-N-1-i])
    minus = heappop(hq)
    minus_r = heappop(hq_r)
    max_A[i+1] = max_A[i] + a[N+i] - minus
    min_C[i+1] = min_C[i] + a[-N-1-i] + minus_r
    
max_val = -10e+100
for i in range(N+1):
    kari = max_A[i] - min_C[-1-i]
    if max_val < kari:
        max_val =kari
print(max_val)
Problem: p03716
Buggy submission (PyPy3 (2.4.0), verdict: Wrong Answer):
import sys
from collections import Counter, deque, defaultdict
from math import factorial
import heapq, bisect
import math
import itertools
sys.setrecursionlimit(10 ** 5 + 10)
INF = 10*5
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))

n = INT()
a = LIST()

a1 = a[:2*n]
a2 = a[n:]
a2.reverse()
for i in range(2*n):
    a2[i] = a2[i]*-1

def searchmax(a):
    print(a)
    maxl = [0]*(n+1)
    left = a[:n]
    center = a[n:]
    heapq.heapify(left)
    maxl[0] = sum(left)
    for i in range(1,n+1):
        heapq.heappushpop(left, center[i-1])
        maxl[i] = sum(left)
    print(maxl)
    return maxl

maxll = searchmax(a1)
minrr = searchmax(a2)
minrr.reverse()

res = [INF *-1]*(n+1)
for i in range(n+1):
    res[i] = maxll[i] + minrr[i]

print(max(res)) 





Failing test case input:
2
3 1 4 1 5 9

Expected output:
1

Actual output:
[3, 1, 4, 1]
[4, 7, 7]
[-9, -5, -1, -4]
[-14, -6, -5]
1


Fixed submission (verdict: Accepted):
import sys
from collections import Counter, deque, defaultdict
from math import factorial
import heapq, bisect
import math
import itertools
sys.setrecursionlimit(10 ** 5 + 10)
INF = 10*5
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))

def main():
    n = INT()
    a = LIST()

    a1 = a[:2*n]
    a2 = a[n:]
    a2.reverse()
    for i in range(2*n):
        a2[i] = a2[i]*-1

    def searchmax(a):
        maxl = [0]*(n+1)
        left = a[:n]
        center = a[n:]
        heapq.heapify(left)
        maxl[0] = sum(left)
        for i in range(1,n+1):
            poped = heapq.heappushpop(left, center[i-1])
            maxl[i] = max(maxl[i-1], maxl[i-1]-poped+center[i-1])
        return maxl

    maxll = searchmax(a1)
    minrr = searchmax(a2)
    minrr.reverse()

    res = [INF *-1]*(n+1)
    for i in range(n+1):
        res[i] = maxll[i] + minrr[i]

    print(max(res)) 

if __name__ == '__main__':
    main()
Problem: p03716
Buggy submission (PyPy3 (2.4.0), verdict: Wrong Answer):

import sys
from collections import deque, defaultdict
import copy
import bisect

import math
import heapq
from itertools import combinations, permutations

import sys
def input():
	return sys.stdin.readline().strip()

N = int(input())
a = list(map(int, input().split()))

a_max = a[:N]
a_min = a[2*N:]

a_max_sum = sum(a_max)
score = [a_max_sum]
heapq.heapify(a_max)
for i in range(N):
	heapq.heappush(a_max, a[N + i])
	a_max_sum += a[N + i]
	a_max_sum -= heapq.heappop(a_max)
	score.append(a_max_sum)
print(score)

a_min_sum = sum(a_min)
score[N] -= a_min_sum
for i in range(len(a_min)):
	a_min[i] *= -1
heapq.heapify(a_min)
for i in range(N):
	heapq.heappush(a_min, -a[2*N - 1 - i])
	a_min_sum += a[2*N - 1 - i]
	a_min_sum += heapq.heappop(a_min)
	score[N - i - 1] -=a_min_sum

print(max(score))

Failing test case input:
2
3 1 4 1 5 9

Expected output:
1

Actual output:
[4, 7, 7]
1


Fixed submission (verdict: Accepted):

import sys
from collections import deque, defaultdict
import copy
import bisect

import math
import heapq
from itertools import combinations, permutations

import sys
def input():
	return sys.stdin.readline().strip()

N = int(input())
a = list(map(int, input().split()))

a_max = a[:N]
a_min = a[2*N:]

a_max_sum = sum(a_max)
score = [a_max_sum]
heapq.heapify(a_max)
for i in range(N):
	heapq.heappush(a_max, a[N + i])
	a_max_sum += a[N + i]
	a_max_sum -= heapq.heappop(a_max)
	score.append(a_max_sum)

a_min_sum = sum(a_min)
score[N] -= a_min_sum
for i in range(len(a_min)):
	a_min[i] *= -1
heapq.heapify(a_min)
for i in range(N):
	heapq.heappush(a_min, -a[2*N - 1 - i])
	a_min_sum += a[2*N - 1 - i]
	a_min_sum += heapq.heappop(a_min)
	score[N - i - 1] -=a_min_sum

print(max(score))

Problem: p03716
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
import heapq

N = int(input())
A = list(map(int,input().split()))
C = A[N:(2*N)]

L = []
R = []
for i in range(N):
  heapq.heappush(L,A[i]) 
  heapq.heappush(R,-A[-1-i]) 
  


lp = 0 
rp = 0
for i in range(N):
  
  if C[lp] - L[0] >= -C[-1-rp] - R[0]: 
    heapq.heappush(L, C[lp]) 
    temp = heapq.heappop(L)
    lp += 1
  else:
    heapq.heappush(R, -C[-1-rp])
    temp = heapq.heappop(R)    
    rp += 1
print(L,R,lp,rp)
ans = sum(L) + sum(R)
print(ans)
    

Failing test case input:
2
3 1 4 1 5 9

Expected output:
1

Actual output:
[3, 4] [-5, -1] 1 1
1


Fixed submission (verdict: Accepted):
import heapq

N = int(input())
A = list(map(int,input().split()))
C = A[N:(2*N)]

L = []
R = []
for i in range(N):
  heapq.heappush(L,A[i]) 
  heapq.heappush(R,-A[-1-i]) 


ansL = [sum(L)]
for i in range(N):
  heapq.heappush(L,C[i])
  rmv = heapq.heappop(L)
  temp = ansL[i] + C[i] - rmv 
  ansL.append(temp)
  

ansR = [sum(R)]
for i in range(N):
  heapq.heappush(R,-C[-1-i])
  rmv = heapq.heappop(R)
  temp = ansR[i] - C[-1-i] - rmv
  ansR.append(temp)
  

ans = -float('inf')
for i in range(N+1):
  temp = ansL[i]+ansR[-1-i]
  ans = max(ans,temp)

print(ans)
Problem: p03716
Buggy submission (PyPy3 (2.4.0), verdict: Wrong Answer):
import heapq
n=int(input())
a=list(map(int,input().split()))
q=a[0:n]
heapq.heapify(q)
p=list(map(lambda x: x*(-1),a[2*n:3*n]))
heapq.heapify(p)
sums=[0]*(n+1)
sums[0]=sum(q)
sums[n]=sum(p)
am=a[n:2*n]
for i in range(0,n):
    heapq.heappush(q,am[i])
    heapq.heappop(q)
    sums[i+1]=sum(q)
for i in range(n-1,-1,-1):
    heapq.heappush(p,(-1)*am[i])
    heapq.heappop(p)
    sums[i]+=sum(p)
print(max(sums))

Failing test case input:
2
3 1 4 1 5 9

Expected output:
1

Actual output:
7


Fixed submission (verdict: Accepted):
import heapq
n=int(input())
a=list(map(int,input().split()))
q=a[0:n]
sums=[0]*(n+1)
sums[0]=sum(q)
heapq.heapify(q)
am=a[n:2*n]
for i in range(0,n):
    heapq.heappush(q,am[i])
    sums[i+1]=sums[i]+am[i]-heapq.heappop(q)
p=a[2*n:3*n]
p=list(map(lambda x: x*(-1),p))
sump=sum(p)
sums[n]+=sump
heapq.heapify(p)
for i in range(n-1,-1,-1):
    heapq.heappush(p,(-1)*am[i])
    sump=sump-am[i]-heapq.heappop(p)
    sums[i]+=sump
print(max(sums))

Problem: p03716
Buggy submission (PyPy3 (2.4.0), verdict: Wrong Answer):

import sys
from collections import deque, defaultdict
import copy
import bisect

import math
import heapq
from itertools import combinations, permutations

import sys
def input():
	return sys.stdin.readline().strip()

N = int(input())
a = list(map(int, input().split()))

a_max = a[:N]
a_min = a[2*N:]

a_max_sum = sum(a_max)
score = [a_max_sum]
heapq.heapify(a_max)
for i in range(N):
	heapq.heappush(a_max, a[N + i])
	a_max_sum += a[N + i]
	a_max_sum -= heapq.heappop(a_max)
	score.append(a_max_sum)
print(score)

a_min_sum = sum(a_min)
score[N] -= a_min_sum
for i in range(len(a_min)):
	a_min[i] *= -1
heapq.heapify(a_min)
for i in range(N):
	heapq.heappush(a_min, -a[2*N - 1 - i])
	a_min_sum += a[2*N - 1 - i]
	a_min_sum += heapq.heappop(a_min)
	score[N - i - 1] -=a_min_sum
print(score)

print(max(score))

Failing test case input:
2
3 1 4 1 5 9

Expected output:
1

Actual output:
[4, 7, 7]
[-1, 1, -7]
1


Fixed submission (verdict: Accepted):

import sys
from collections import deque, defaultdict
import copy
import bisect

import math
import heapq
from itertools import combinations, permutations

import sys
def input():
	return sys.stdin.readline().strip()

N = int(input())
a = list(map(int, input().split()))

a_max = a[:N]
a_min = a[2*N:]

a_max_sum = sum(a_max)
score = [a_max_sum]
heapq.heapify(a_max)
for i in range(N):
	heapq.heappush(a_max, a[N + i])
	a_max_sum += a[N + i]
	a_max_sum -= heapq.heappop(a_max)
	score.append(a_max_sum)

a_min_sum = sum(a_min)
score[N] -= a_min_sum
for i in range(len(a_min)):
	a_min[i] *= -1
heapq.heapify(a_min)
for i in range(N):
	heapq.heappush(a_min, -a[2*N - 1 - i])
	a_min_sum += a[2*N - 1 - i]
	a_min_sum += heapq.heappop(a_min)
	score[N - i - 1] -=a_min_sum

print(max(score))
Problem: p03716
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
import heapq
N = int(input())
a = list(map(int, input().split()))

max_result = -1000000

for k in range(N, 2*N+1):
  print("----------------")
  big_a = a[:k]
  heapq.heapify(big_a)
  print(big_a)
  small_a =[(-x, x) for x in a[k:]]
  heapq.heapify(small_a)
  print(small_a)

  for i in range(len(big_a) - N):
    heapq.heappop(big_a)
  print(big_a)

  for i in range(len(small_a) - N):
    heapq.heappop(small_a)
  print(small_a)

  big_sum = sum(big_a)
  small_sum = 0
  for i in small_a:
    small_sum += i[1]

  if (big_sum - small_sum) > max_result:
    max_result = big_sum - small_sum

print(max_result)



Failing test case input:
2
3 1 4 1 5 9

Expected output:
1

Actual output:
----------------
[1, 3]
[(-9, 9), (-4, 4), (-5, 5), (-1, 1)]
[1, 3]
[(-4, 4), (-1, 1)]
----------------
[1, 3, 4]
[(-9, 9), (-5, 5), (-1, 1)]
[3, 4]
[(-5, 5), (-1, 1)]
----------------
[1, 1, 4, 3]
[(-9, 9), (-5, 5)]
[3, 4]
[(-9, 9), (-5, 5)]
1


Fixed submission (verdict: Accepted):
import heapq
N = int(input())
a = list(map(int, input().split()))

max_result = -1000000

big_a = a[:N]
big_sum = [sum(big_a)]
heapq.heapify(big_a)
for i in range(N):
  heapq.heappush(big_a, a[N+i])
  p = heapq.heappop(big_a)
  big_sum.append(big_sum[-1] + a[N+i] - p)

small_a = [-i for i in a[-N:]]
small_sum = [sum(small_a)]
heapq.heapify(small_a)
for i in range(N):
  heapq.heappush(small_a, -a[-N-i-1])
  p = heapq.heappop(small_a)
  small_sum.append(small_sum[-1] - a[-N-i-1] - p)

max_result = -float('inf')

for m,s in zip(big_sum, small_sum[::-1]):
  max_result = max(m + s, max_result)

print(max_result)
Problem: p03716
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
N = int(input())
a = list(map(int, input().split()))

import heapq
que1 = []
heapq.heapify(que1)
S = [0] * (2*N + 1)
for i in range(N):
    S[i + 1] = S[i] + a[i]
    
    heapq.heappush(que1, a[i])
que2 = []
heapq.heapify(que2)
T = [0] * (2*N + 1)
for i in range(N):
    T[i + 1] = T[i] + a[3*N - i - 1]
    
    heapq.heappush(que2, -a[3*N - i - 1])

for i in range(N, 2*N):
    mi = heapq.heappop(que1)
    print(mi)
    if a[i] > mi:
        S[i + 1] = S[i] - mi + a[i]
        
        heapq.heappush(que1, a[i])
    else:
        S[i + 1] = S[i]
        heapq.heappush(que1, mi)
for i in range(N, 2*N):
    ma = heapq.heappop(que2) * -1
    print(ma)
    if a[3*N - i - 1] < ma:
        T[i + 1] = T[i] - ma + a[3*N - i - 1]
        
        
        heapq.heappush(que2, -a[3*N - i - 1])
    else:
        T[i + 1] = T[i]
        heapq.heappush(que2, -ma)

res = -float('Inf')
for i in range(N, 2*N):
    res = max(res, S[i] - T[3*N - i])
print(res)

Failing test case input:
2
3 1 4 1 5 9

Expected output:
1

Actual output:
1
3
9
5
1


Fixed submission (verdict: Accepted):
N = int(input())
a = list(map(int, input().split()))

import heapq
que1 = []
heapq.heapify(que1)
S = [0] * (2*N + 1)
for i in range(N):
    S[i + 1] = S[i] + a[i]
    
    heapq.heappush(que1, a[i])
que2 = []
heapq.heapify(que2)
T = [0] * (2*N + 1)
for i in range(N):
    T[i + 1] = T[i] + a[3*N - i - 1]
    
    heapq.heappush(que2, -a[3*N - i - 1])

for i in range(N, 2*N):
    mi = heapq.heappop(que1)
    
    if a[i] > mi:
        S[i + 1] = S[i] - mi + a[i]
        
        heapq.heappush(que1, a[i])
    else:
        S[i + 1] = S[i]
        heapq.heappush(que1, mi)
for i in range(N, 2*N):
    ma = heapq.heappop(que2) * -1
    
    if a[3*N - i - 1] < ma:
        T[i + 1] = T[i] - ma + a[3*N - i - 1]
        
        
        heapq.heappush(que2, -a[3*N - i - 1])
    else:
        T[i + 1] = T[i]
        heapq.heappush(que2, -ma)

res = -float('Inf')
for i in range(N, 2*N + 1):
    res = max(res, S[i] - T[3*N - i])
print(res)
Problem: p03716
Buggy submission (PyPy3 (2.4.0), verdict: Wrong Answer):
from heapq import *

N=int(input())
A=list(map(int,input().split()))
B=[]
for i in range(N):
    heappush(B,A[i])
sumB=[sum(B)]
for i in range(N,2*N):
    heappush(B,A[i])
    poped=heappop(B)
    sumB.append(sumB[-1]-poped+A[i])

A.reverse()
C=[]
for i in range(N):
    heappush(C,A[i]*(-1))
sumC=[(-1)*sum(C)]
for i in range(N,2*N):
    heappush(C,A[i]*(-1))
    poped=heappop(C)*(-1)
    sumC.append(sumC[-1]-poped+A[i])
sumC.reverse()
ans=-10**19
for i,j in zip(sumB,sumC):
    ans=max(ans,i-j)


Failing test case input:
2
3 1 4 1 5 9

Expected output:
1

Actual output:

Fixed submission (verdict: Accepted):
from heapq import *

N=int(input())
A=list(map(int,input().split()))
B=[]
for i in range(N):
    heappush(B,A[i])
sumB=[sum(B)]
for i in range(N,2*N):
    heappush(B,A[i])
    poped=heappop(B)
    sumB.append(sumB[-1]-poped+A[i])

A.reverse()
C=[]
for i in range(N):
    heappush(C,A[i]*(-1))
sumC=[(-1)*sum(C)]
for i in range(N,2*N):
    heappush(C,A[i]*(-1))
    poped=heappop(C)*(-1)
    sumC.append(sumC[-1]-poped+A[i])
sumC.reverse()
ans=-10**19
for i,j in zip(sumB,sumC):
    ans=max(ans,i-j)
print(ans)

Problem: p03716
Buggy submission (Python (3.4.3), verdict: Wrong Answer):


n = int(input())
a = [int(i) for i in input().split()]

from heapq import *

pq1 = a[:n]
heapify(pq1)
s = sum(pq1)
score1 = [s]
for i in range(n):
    c=heappushpop(pq1, a[n+i])
    s += a[n+i] - c
    score1.append(s)

pq2 = [-i for i in a[2*n:]]
heapify(pq2)
s = sum(pq2)
score2 = [s]
for i in range(1,n+1):
    c=heappushpop(pq2, -a[2*n-i])
    s += -a[2*n-i] - c
    score2.append(s)

print(score1)
print(score2)
ans = -10**17
for i,j in zip(score1,reversed(score2)):
    ans = max(ans, i+j)

print(ans)
    


Failing test case input:
2
3 1 4 1 5 9

Expected output:
1

Actual output:
[4, 7, 7]
[-14, -6, -5]
1


Fixed submission (verdict: Accepted):


n = int(input())
a = [int(i) for i in input().split()]

from heapq import *

pq1 = a[:n]
heapify(pq1)
s = sum(pq1)
score1 = [s]
for i in range(n):
    c=heappushpop(pq1, a[n+i])
    s += a[n+i] - c
    score1.append(s)

pq2 = [-i for i in a[2*n:]]
heapify(pq2)
s = sum(pq2)
score2 = [s]
for i in range(1,n+1):
    c=heappushpop(pq2, -a[2*n-i])
    s += -a[2*n-i] - c
    score2.append(s)



ans = -10**17
for i,j in zip(score1,reversed(score2)):
    ans = max(ans, i+j)

print(ans)
Problem: p03716
Buggy submission (PyPy3 (2.4.0), verdict: Wrong Answer):
INF=10**30
from heapq import heapify,heappop,heappush
N=int(input())
a=list(map(int,input().split()))

right=list(map(lambda x:-x,a[2*N:]));heapify(right)
rmin=[INF]*(3*N);rmin[2*N]=sum(right)*-1

for i in reversed(range(N,2*N)):
    p=heappop(right)*-1
    b=min(p,a[i])
    d=0 if b==p else b-p
    heappush(right,b*-1)
    rmin[i]=rmin[i+1]-d

left=a[:N];heapify(left)
lmax=[-INF]*(3*N);lmax[N-1]=sum(left)
for i in range(N,2*N):
    p=heappop(left)
    b=max(p,a[i])
    d=0 if b==p else b-p
    heappush(left,b)
    lmax[i]=lmax[i-1]+d
res=-INF
for i in range(N,2*N+1):
    res=max(res,lmax[i-1]-rmin[i])
print(res)

Failing test case input:
2
3 1 4 1 5 9

Expected output:
1

Actual output:
-7


Fixed submission (verdict: Accepted):
INF=10**30
from heapq import heapify,heappop,heappush
N=int(input())
a=list(map(int,input().split()))

right=list(map(lambda x:-x,a[2*N:]));heapify(right)
rmin=[INF]*(3*N);rmin[2*N]=sum(right)*-1

for i in reversed(range(N,2*N)):
    p=heappop(right)*-1
    b=min(p,a[i])
    d=0 if b==p else b-p
    heappush(right,b*-1)
    rmin[i]=rmin[i+1]+d

left=a[:N];heapify(left)
lmax=[-INF]*(3*N);lmax[N-1]=sum(left)
for i in range(N,2*N):
    p=heappop(left)
    b=max(p,a[i])
    d=0 if b==p else b-p
    heappush(left,b)
    lmax[i]=lmax[i-1]+d
res=-INF
for i in range(N,2*N+1):
    res=max(res,lmax[i-1]-rmin[i])
print(res)
Problem: p03716
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
import sys
import heapq


def solve(n, a):
    res = -10 ** 20
    for i in range(n, 2 * n + 1):
        l = a[0:i]
        r = a[i:3 * n]
        l.sort(reverse=True)
        r.sort()
        res0 = sum(l[0:n]) - sum(r[0:n])
        if res0 > res:
            res = res0
    return res


def find_best(n, a):
    h = list()
    s = 0
    best_s = 0
    res = list()
    for i in range(0, 2 * n):
        heapq.heappush(h, a[i])
        s += a[i]
        if len(h) > n:
            m = heapq.heappop(h)
            s -= m
        if len(h) == n:
            if best_s < s:
                best_s = s
            res.append(best_s)
    return res


def solve_large(n, a):
    res = -10 ** 20
    max_l = find_best(n, a)
    b = [-x for x in reversed(a)]
    max_r = find_best(n, b)
    max_r.reverse()
    for l, r in zip(max_l, max_r):
        res0 = l + r
        if res < res0:
            res = res0
    return res



n = int(input())
a = list(map(int, input().split()))
print(solve_large(n, a))


Failing test case input:
2
3 1 4 1 5 9

Expected output:
1

Actual output:
7


Fixed submission (verdict: Accepted):
import os
import sys
import heapq


def solve(n, a):
    res = -10 ** 20
    for i in range(n, 2 * n + 1):
        l = a[0:i]
        r = a[i:3 * n]
        l.sort(reverse=True)
        r.sort()
        res0 = sum(l[0:n]) - sum(r[0:n])
        if res0 > res:
            res = res0
    return res


def find_best(n, a):
    h = list()
    s = 0
    best_s = -10**20
    res = list()
    for i in range(0, 2 * n):
        heapq.heappush(h, a[i])
        s += a[i]
        if len(h) > n:
            m = heapq.heappop(h)
            s -= m
        if len(h) == n:
            if best_s < s:
                best_s = s
            res.append(best_s)
    return res


def solve_large(n, a):
    res = -10 ** 20
    max_l = find_best(n, a)
    b = [-x for x in reversed(a)]
    max_r = find_best(n, b)
    max_r.reverse()
    for l, r in zip(max_l, max_r):
        res0 = l + r
        if res < res0:
            res = res0
    return res


input_file = 'd1.in'
if os.path.exists(input_file):
    sys.stdin = open(input_file)
n = int(input())
a = list(map(int, input().split()))
print(solve_large(n, a))
Problem: p03716
Buggy submission (Python (3.4.3), verdict: Wrong Answer):

def inpl(): return tuple(map(int, input().split()))
from bisect import bisect_left, insort
N = int(input())
A = inpl()

before = sorted(A[:N])
middle = sorted(A[N:-N])
after = sorted(A[-N:])

S_before = [sum(before)]
S_after = [sum(after)]

for m in middle:
    if before[0] < m:
        S_before.append(S_before[-1] + m - before[0])
        insort(before, m)
        before = before[1:]
    else:
        S_before.append(S_before[-1])

for m in middle[::-1]:
    if after[-1] > m:
        S_after.append(S_after[-1] - m + after[-1])
        insort(after, m)
        after = after[:-1]
    else:
        S_after.append(S_after[-1])    

print(max([b-a for b, a in zip(S_before, S_after[::-1])])) 


Failing test case input:
2
3 1 4 1 5 9

Expected output:
1

Actual output:
-7


Fixed submission (verdict: Accepted):

def inpl(): return list(map(int, input().split()))
from heapq import heapify, heappop, heappush
from copy import deepcopy

N = int(input())
A = inpl()

before = deepcopy(A[:N])
heapify(before)
middle = A[N:-N]
after = [-a for a in A[-N:]]
heapify(after)

sb = sum(A[:N])
sa = -sum(A[-N:])

SB = [sb]
SA = [sa]

for m in middle:
    b = heappop(before)
    if b < m:
        heappush(before, m)
        sb += m - b
    else:
        heappush(before, b)
    SB.append(sb)    

for m in middle[::-1]:
    m = -m
    a = heappop(after)
    if a < m:
        heappush(after, m)
        sa += m - a
    else:
        heappush(after, a)
    SA.append(sa) 

    
print(max([b+a for b, a in zip(SB, SA[::-1])]))
Problem: p03716
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
N = int(input())
a = list(map(int, input().split()))

import heapq
que1 = []
heapq.heapify(que1)
S = [0] * (2*N + 1)
for i in range(N):
    S[i + 1] = S[i] + a[i]
    
    heapq.heappush(que1, a[i])
que2 = []
heapq.heapify(que2)
T = [0] * (2*N + 1)
for i in range(N):
    T[i + 1] = T[i] + a[3*N - i - 1]
    
    heapq.heappush(que2, -a[3*N - i - 1])

for i in range(N, 2*N):
    mi = heapq.heappop(que1)
    print(mi)
    if a[i] > mi:
        S[i + 1] = S[i] - mi + a[i]
        
        heapq.heappush(que1, a[i])
    else:
        S[i + 1] = S[i]
        heapq.heappush(que1, mi)
for i in range(N, 2*N):
    ma = heapq.heappop(que2) * -1
    print(ma)
    if a[3*N - i - 1] < ma:
        T[i + 1] = T[i] - ma + a[3*N - i - 1]
        
        
        heapq.heappush(que2, -a[3*N - i - 1])
    else:
        T[i + 1] = T[i]
        heapq.heappush(que2, -ma)

res = -float('Inf')
for i in range(N, 2*N + 1):
    res = max(res, S[i] - T[3*N - i])
print(res)


Failing test case input:
2
3 1 4 1 5 9

Expected output:
1

Actual output:
1
3
9
5
1


Fixed submission (verdict: Accepted):
N = int(input())
a = list(map(int, input().split()))

import heapq
que1 = []
heapq.heapify(que1)
S = [0] * (2*N + 1)
for i in range(N):
    S[i + 1] = S[i] + a[i]
    
    heapq.heappush(que1, a[i])
que2 = []
heapq.heapify(que2)
T = [0] * (2*N + 1)
for i in range(N):
    T[i + 1] = T[i] + a[3*N - i - 1]
    
    heapq.heappush(que2, -a[3*N - i - 1])

for i in range(N, 2*N):
    mi = heapq.heappop(que1)
    
    if a[i] > mi:
        S[i + 1] = S[i] - mi + a[i]
        
        heapq.heappush(que1, a[i])
    else:
        S[i + 1] = S[i]
        heapq.heappush(que1, mi)
for i in range(N, 2*N):
    ma = heapq.heappop(que2) * -1
    
    if a[3*N - i - 1] < ma:
        T[i + 1] = T[i] - ma + a[3*N - i - 1]
        
        
        heapq.heappush(que2, -a[3*N - i - 1])
    else:
        T[i + 1] = T[i]
        heapq.heappush(que2, -ma)

res = -float('Inf')
for i in range(N, 2*N + 1):
    res = max(res, S[i] - T[3*N - i])
print(res)
Problem: p03200
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
s = input()
w_count = s.count('W')
ans = 0
for i in range(w_count):
    if s[i] == 'B':
        ans += w_count - i + 1
print(ans)

Failing test case input:
WBWBTC

Expected output:
1


Actual output:
2


Fixed submission (verdict: Accepted):
s = input()
ans = 0
w_count = 0
for i in range(len(s)):
    if s[i] == 'W':
        ans += i - w_count
        w_count += 1
print(ans)

Problem: p03200
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
s=list(input())
ans=0
wf=0
bf=0
for i in range(len(s)):
    if s[i]=='W':
        ans+=i
        wf=1
    else:
        bf=1
if wf==0 or bf==0:
    print(0)
else:
    print(ans)

Failing test case input:
WBWBVB

Expected output:
1


Actual output:
2


Fixed submission (verdict: Accepted):
s=list(input())
ans=0
wf=0
bf=0
for i in range(len(s)):
    if s[i]=='W':
        ans+=i
        wf+=1
    else:
        bf=1
if wf==0 or bf==0:
    print(0)
else:
    print(ans-wf*(wf-1)

Problem: p03200
Buggy submission (Python (3.8.2), verdict: Wrong Answer):
s = list(input())

count = 0
white = 0
for i in range(len(s)):
    if s[i] == 'W':
        count += i
        white += 1
        
print(count - white)

Failing test case input:
WBWBUC

Expected output:
1


Actual output:
0


Fixed submission (verdict: Accepted):
s = list(input())

count = 0
white = 0
for i in range(len(s)):
    if s[i] == 'W':
        count += i - white
        white += 1

print(count)

Problem: p03200
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
S = input()
counter = 0




















sum=0
for i in range(len(S)):
    if S[i]=='W':
        sum+=i
        counter+=1
print(sum-counter)

Failing test case input:
WBVBVC

Expected output:
0


Actual output:
-1


Fixed submission (verdict: Accepted):
S = input()
counter = 0




















sum=0
for i in range(len(S)):
    if S[i]=='W':
        sum+=i
        counter+=1

n=counter-1
minus=n*(n+1)/2
print(int(sum-minus))

Problem: p03200
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
S = input()
ans = 0
tmp = 0
for i in range(len(S)):
  if S[i] == 'W':
    ans+=i-tmp
  else:
    tmp+=1
print(ans)

Failing test case input:
BBW

Expected output:
2

Actual output:
0


Fixed submission (verdict: Accepted):
S = input()
ans = 0
tmp = 0
for i in range(len(S)):
  if S[i] == 'W':
    ans+=i-tmp
    tmp+=1
print(ans)

Problem: p03200
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
import sys

line = next(map(str.strip, sys.stdin))

count = 0
nb_bs = 0
for i, c in enumerate(line):
    if c == 'W' and nb_bs:
        count += nb_bs
    else:
        nb_bs += 1

print(count)

Failing test case input:
WBWBVB

Expected output:
1


Actual output:
2


Fixed submission (verdict: Accepted):
import re
import sys

line = next(map(str.strip, sys.stdin))

count = 0
nb_bs = 0
for i, c in enumerate(line):
    if c == 'W' and nb_bs > 0:
        count += nb_bs
    elif c == 'B':
        nb_bs += 1

print(count)
Problem: p03200
Buggy submission (Python (3.8.2), verdict: Wrong Answer):
s = input()
n = len(s)
sum = [0] * (n + 1)
sum[0] = (s[0] == 'B')
for i in range(1 , n):
	sum[i] = sum[i-1] + (s[i] == 'B')
ans = 0
for i in range(n - 1, 0 ,-1):
	print(i)
	if s[i] == 'W':
		ans += sum[i]
print(ans)
	

Failing test case input:
Y;D

Expected output:
0


Actual output:
2
1
0


Fixed submission (verdict: Accepted):
s = input()
n = len(s)
sum = [0] * (n + 1)
sum[0] = (s[0] == 'B')
for i in range(1 , n):
	sum[i] = sum[i-1] + (s[i] == 'B')
ans = 0
for i in range(n - 1, -1 ,-1):
	if s[i] == 'W':
		ans += sum[i]
print(ans)

Problem: p03200
Buggy submission (Python (3.8.2), verdict: Wrong Answer):
S=input()

start=0
for i in range(len(S)):
    if S[i]=='B':
        break

    start=i

left_W=start
ans=0
if start<len(S)-1:
    for i in range(start+1,len(S)):
        if S[i]=='W':
            ans+=i-left_W
            left_W+=1

print(ans)

Failing test case input:
WBWBVB

Expected output:
1


Actual output:
2


Fixed submission (verdict: Accepted):
S=input()

start=0
for i in range(len(S)):
    start=i
    if S[i]=='B':
        break

left_W=start
ans=0
if start<len(S)-1:
    for i in range(start+1,len(S)):
        if S[i]=='W':
            ans+=i-left_W
            left_W+=1

print(ans)

Problem: p03200
Buggy submission (Python (3.4.3), verdict: Wrong Answer):

import sys, math, fractions, itertools, collections, copy, heapq


def solve(S: str):
    counter = 0
    tmp = S[0]
    for s in S[1:]:
        if s == tmp[-1]:
            tmp = tmp + s
        elif s == 'W' and tmp[-1] == 'B':
            tmp = tmp + s
        else:
            counter += 2 * min(tmp.count('B'), tmp.count('W'))
            tmp = s
    counter += 2 * min(tmp.count('B'), tmp.count('W'))
    print(counter)
    return



def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    S = next(tokens)  
    solve(S)

if __name__ == '__main__':
    main()


Failing test case input:
WBWBUC

Expected output:
1


Actual output:
2


Fixed submission (verdict: Accepted):

import sys, math, fractions, itertools, collections, copy, heapq


def solve(S: str):
    counter = 0
    c = 0
    for i, s in enumerate(S):
        if s == 'W':
            counter += i - c
            c += 1
    print(counter)
    return



def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    S = next(tokens)  
    solve(S)

if __name__ == '__main__':
    main()

Problem: p03200
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
s=input()
t=list(s).count("W")
A=[i for i,x in enumerate (list(s)) if x=="W"]
print(int(sum(A)-t*(t+1)/2))

Failing test case input:
WBWBVC

Expected output:
1


Actual output:
-1


Fixed submission (verdict: Accepted):
s=input()
t=list(s).count("W")
A=[i for i,x in enumerate (list(s)) if x=="W"]
print(int(sum(A)-t*(t-1)/2))
Problem: p03200
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
s = list(input())
b_counts = []
cnt = 0
for i in range(len(s)):
    if s[i] == "B":
        cnt += 1
    if s[i] == "W":
        cnt += sum(b_counts)
        b_counts.append(cnt)
        cnt = 0
print(sum(b_counts))


Failing test case input:
BWBWBW

Expected output:
6

Actual output:
7


Fixed submission (verdict: Accepted):
s = list(input())

cnt = 0
prev_cnt = 0
result = 0
for i in range(len(s)):
    if s[i] == "B":
        cnt += 1
    if s[i] == "W":
        cnt += prev_cnt
        result += cnt
        prev_cnt = cnt
        cnt = 0
print(result)

Problem: p03200
Buggy submission (PyPy3 (2.4.0), verdict: Wrong Answer):
s=input()
m=[s[i]=="B" for i in range(len(s))]
m1=[None for i in range(len(s))]
m1[0]=m[0]
for i in range(1,len(s)):
  m1[i]=m1[i-1]+m[i]
m2=[None for i in range(len(s))]
m2[len(s)-1]=m[len(s)-1]
for i in range(len(s)-1,0,-1):
  m2[i-1]=m2[i]+m[i]
  
ans1=0
ans2=0
for i in range(1,len(s)):
  if s[i]=="W":
    ans1+=m1[i]
    ans2+=m2[i]
    
print(max(ans1,ans2))

Failing test case input:
WBWBVB

Expected output:
1


Actual output:
3


Fixed submission (verdict: Accepted):
s=input()
m=[s[i]=="B" for i in range(len(s))]
m1=[None for i in range(len(s))]
m1[0]=m[0]
for i in range(1,len(s)):
  m1[i]=m1[i-1]+m[i]
m2=[None for i in range(len(s))]
m2[len(s)-1]=m[len(s)-1]
for i in range(len(s)-1,0,-1):
  m2[i-1]=m2[i]+m[i]
  
ans1=0
ans2=0
for i in range(1,len(s)):
  if s[i]=="W":
    ans1+=m1[i]
    ans2+=m2[i]
    
print(ans1)

Problem: p03200
Buggy submission (PyPy3 (2.4.0), verdict: Wrong Answer):
S = input()
N = len(S)
A = []

for i, s in enumerate(S):
  if s == "W":
    A.append(i)

if len(A) == 1:
  print(sum(A))
else:
  print(sum(A)-len(A))

Failing test case input:
WBWBTC

Expected output:
1


Actual output:
0


Fixed submission (verdict: Accepted):
S = input()
N = len(S)
A = []

for i, s in enumerate(S):
  if s == "W":
    A.append(i)
n = len(A)-1
print(sum(A)-(n+1)*n

Problem: p03200
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
S = input().strip()

count = 0
suma = 0
for i in S:
  if i=="W":
    suma += count
  else:
    count += 1


Failing test case input:
WBWBTC

Expected output:
1


Actual output:

Fixed submission (verdict: Accepted):
S = input().strip()

count = 0
suma = 0
for i in S:
  if i=="W":
    suma += count
  else:
    count += 1
print(suma)

Problem: p03200
Buggy submission (PyPy3 (2.4.0), verdict: Wrong Answer):
S = input()
N = len(S)
A = []

for i, s in enumerate(S):
  if s == "W":
    A.append(i)

print(max(sum(A)-len(A), 0))

Failing test case input:
WBWBVB

Expected output:
1


Actual output:
0


Fixed submission (verdict: Accepted):
S = input()
N = len(S)
A = []

for i, s in enumerate(S):
  if s == "W":
    A.append(i)
n = len(A)-1
print(sum(A)-(n+1)*n
Problem: p03200
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
s = str(input())
n = len(s)
count = 0
max_W = 0

for i in range(n):
    if s[i] == 'W':
        count += i
        max_W = i

if max_W == s.count('W') - 1 or s.count('W') == 0 or s.count('W') == n:
    print(0)
    exit()

print(count)

Failing test case input:
WBWBUB

Expected output:
1


Actual output:
2


Fixed submission (verdict: Accepted):
s = str(input())
n = len(s)
distance = 0
count = 0
max_W = 0

for i in range(n):
    if s[i] == 'W':
        distance += (i - count)
        count += 1
        max_W = i

if s.count('W') == 0 or s.count('W') == n:
    print(0)
    exit()

print(distance)

Problem: p03200
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
data = list(input())[::-1]

count = 0


count_b = 0
for i in range(len(data)):
    
    if data[i] == 'W':
        count += count_b
        
        
        
    else:
        count_b += 1
    
    
print(count)


Failing test case input:
BBW

Expected output:
2

Actual output:
0


Fixed submission (verdict: Accepted):
data = list(input())

count = 0


count_b = 0
for i in range(len(data)):
    
    if data[i] == 'W':
        count += count_b
        
        
        
    else:
        count_b += 1
    
    
print(count)

Problem: p03200
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
hoge=input()
ans=0
count=0
white=0
for i in hoge:
  
  if(i=="W"):
    ans+=count - white
    white+=1
  count+=1
  
print(count)

Failing test case input:
9\E

Expected output:
0


Actual output:
3


Fixed submission (verdict: Accepted):
hoge=input()
ans=0
count=0
white=0
for i in hoge:
  
  if(i=="W"):
    ans+=count - white
    white+=1
    
  count+=1
  
print(ans)

Problem: p03200
Buggy submission (Python (3.8.2), verdict: Wrong Answer):
S = input()


idx_w = 0
idxes_b = []

for i, s in enumerate(S):
    if s == 'B':
        idxes_b.append(i)
    elif s == 'W':
        idx_w = i


ans = 0
for i, b in enumerate(idxes_b[::-1]):
    ans += idx_w - b - i

print(ans)

Failing test case input:
DYB

Expected output:
0


Actual output:
-2


Fixed submission (verdict: Accepted):
S = input()


idx_w = 0
idxes_b = []

for i, s in enumerate(S):
    if s == 'B':
        idxes_b.append(i)
    elif s == 'W':
        idx_w = i

idxes_b = [b for b in idxes_b if idx_w > b]

ans = 0
for i, b in enumerate(idxes_b[::-1]):
    ans += idx_w - b - i

print(max(0, ans))

Problem: p03200
Buggy submission (Python (3.4.3), verdict: Wrong Answer):

S = input()

ans = 0
state = 0
marker = 0
for i, Si in enumerate(S):
    
    if state < 1 and Si == 'B':
        state += 1
    elif state > 0 and Si == 'W':
        ans += i - marker
        marker += 1

print(ans)

Failing test case input:
WBWBVB

Expected output:
1


Actual output:
2


Fixed submission (verdict: Accepted):

S = input()

ans = 0
state = 0
for i, Si in enumerate(S):
    
    if state < 1 and Si == 'B':
        state += 1
        marker = i
    elif state > 0 and Si == 'W':
        ans += i - marker
        marker += 1

print(ans)
Problem: p03972
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
import os
import sys

if os.getenv("LOCAL"):
    sys.stdin = open("_in.txt", "r")

sys.setrecursionlimit(2147483647)
INF = float("inf")
IINF = 10 ** 18
MOD = 10 ** 9 + 7

H, W = list(map(int, sys.stdin.readline().split()))
P = [int(sys.stdin.readline()) for _ in range(W)]
Q = [int(sys.stdin.readline()) for _ in range(H)]

P.sort(reverse=True)
Q.sort(reverse=True)

ans = 0
v_nodes = W + 1
h_nodes = H + 1
while P or Q:
    if not Q or (P and P[-1] < Q[-1]):
        ans += P.pop() * h_nodes
        v_nodes -= 1
    else:
        ans += Q.pop() * v_nodes
        h_nodes -= 1
print(ans)


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

Expected output:
12


Actual output:
20


Fixed submission (verdict: Accepted):
import os
import sys

if os.getenv("LOCAL"):
    sys.stdin = open("_in.txt", "r")

sys.setrecursionlimit(2147483647)
INF = float("inf")
IINF = 10 ** 18
MOD = 10 ** 9 + 7


W,H = list(map(int, sys.stdin.readline().split()))
P = [int(sys.stdin.readline()) for _ in range(W)]
Q = [int(sys.stdin.readline()) for _ in range(H)]

edges = []
for p in P:
    edges.append((p, 'P'))
for q in Q:
    edges.append((q, 'Q'))
edges.sort()

ans = 0
a = W + 1
b = H + 1
for e, pq in edges:
    if pq == 'P':
        ans += e * b
        a -= 1
    else:
        ans += e * a
        b -= 1
print(ans)

Problem: p03972
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
import sys
input = sys.stdin.readline
from itertools import accumulate

W, H = map(int,input().split())

P = [int(input()) for _ in range(W)]
Q = [int(input()) for _ in range(H)]

P.sort()
Q.sort()

cum_p = [0] + list(accumulate(P))
cum_q = [0] + list(accumulate(Q))

res = 0

N = min(len(cum_p), len(cum_q))

for i in range(N):
    res += cum_p[-1 - i] + cum_q[-1 - i]

for i in range(N - 1):
    res += min(P[-1 - i], Q[-1 - i])
    
print(res)

Failing test case input:
2 3
1
2
-1
6
4
1
1

Expected output:
16


Actual output:
18


Fixed submission (verdict: Accepted):
import sys
input = sys.stdin.readline
from bisect import bisect_left, bisect_right

W, H = map(int,input().split())

P = [int(input()) for _ in range(W)]
Q = [int(input()) for _ in range(H)]

P.sort()
Q.sort()

res = 0

for i in range(W):
    res += (H + 1 - bisect_left(Q, P[i])) * P[i]

for i in range(H):
    res += (W + 1 - bisect_right(P, Q[i])) * Q[i]
    
print(res)
Problem: p03972
Buggy submission (PyPy3 (2.4.0), verdict: Wrong Answer):
from collections import Counter, defaultdict
import sys
sys.setrecursionlimit(10 ** 5 + 10)

from math import factorial
import heapq, bisect
import math
import itertools


import queue
from collections import deque




def main():
    high, width = map(int, input().split())
    cost_w = [int(input()) for i in range(width)]
    cost_h = [int(input()) for i in range(high)]

    cost_w.sort()
    cost_h.sort()
    check_high, check_width = 0, 0
    width_ind = 0
    ans = 0
    for i in range(high):

        now_high = cost_h[i]
        while width_ind < width:
            if now_high < cost_w[width_ind]:
                break
            ans += (high + 1 - check_high) * cost_w[width_ind]
            check_width += 1
            width_ind += 1

        ans += (width + 1 - check_width) * cost_h[i]
        check_high += 1

        

    ans += sum(cost_w[width_ind:])

    print(ans)







if __name__ == '__main__':
    main()



Failing test case input:
1 3
0
2
0
7
3
1
1

Expected output:
9


Actual output:
11


Fixed submission (verdict: Accepted):
from collections import Counter, defaultdict
import sys
sys.setrecursionlimit(10 ** 5 + 10)

from math import factorial
import heapq, bisect
import math
import itertools


import queue
from collections import deque




def main():
    width, high = map(int, input().split())
    cost_w = [int(input()) for i in range(width)]
    cost_h = [int(input()) for i in range(high)]

    cost_w.sort()
    cost_h.sort()
    check_high, check_width = 0, 0
    width_ind = 0
    ans = 0
    for i in range(high):

        now_high = cost_h[i]
        while width_ind < width:
            if now_high < cost_w[width_ind]:
                break
            ans += (high + 1 - check_high) * cost_w[width_ind]
            check_width += 1
            width_ind += 1

        ans += (width + 1 - check_width) * cost_h[i]
        check_high += 1

    ans += sum(cost_w[width_ind:])

    print(ans)







if __name__ == '__main__':
    main()

Problem: p03972
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
W,H=map(int,input().split())
p=sorted(int(input())for _ in'_'*W)
p+=10**1000,
q=sorted(int(input())for _ in'_'*H)
q+=10**1000,
z=i=j=0
h,w=H+1,W+1
while i<W or j<H:
	if h*p[i]<w*q[j]:
		z+=h*p[i]
		i+=1
		w-=1
	else:
		z+=w*q[j]
		j+=1
		h-=1
print(z)


Failing test case input:
2 3
1
6
0
15
8
1
2

Expected output:
44


Actual output:
46


Fixed submission (verdict: Accepted):
W,H=map(int,input().split())
p=sorted(int(input())for _ in'_'*W)
p+=10**18,
q=sorted(int(input())for _ in'_'*H)
q+=10**18,
z=i=j=0
h,w=H+1,W+1
while i<W or j<H:
	if p[i]<q[j]:
		z+=h*p[i]
		i+=1
		w-=1
	else:
		z+=w*q[j]
		j+=1
		h-=1
print(z)
Problem: p03972
Buggy submission (PyPy3 (7.3.0), verdict: Wrong Answer):
import sys
sys.setrecursionlimit(10**7)

readline = sys.stdin.buffer.readline
def readstr():return readline().rstrip().decode()
def readstrs():return list(readline().decode().split())
def readint():return int(readline())
def readints():return list(map(int,readline().split()))
def printrows(x):print('\n'.join(map(str,x)))
def printline(x):print(' '.join(map(str,x)))

import heapq


w,h =readints()
p = [readint() for i in range(w)]
q = [readint() for i in range(h)]

heapq.heapify(p)
heapq.heapify(q)

edge = 0
cost = 0
used = [0,0]

while edge < (h+1)*(w+1)-1:
    print(edge,cost)
    if p and q:
        if p[0]<q[0]:
            flag = 1
        else:
            flag = 0
    elif p:
        flag = 1
    else:
        flag = 0
    if flag:
        x = heapq.heappop(p)
        edge += h+1-used[1]
        cost += x * (h+1-used[1])
        used[0] += 1
    else:
        x = heapq.heappop(q)
        edge += w+1-used[0]
        cost += x * (w+1-used[0])
        used[1] += 1

print(cost)










Failing test case input:
2 2
3
16
2
7

Expected output:
42


Actual output:
0 0
3 6
5 12
7 26
42


Fixed submission (verdict: Accepted):
import sys
sys.setrecursionlimit(10**7)

readline = sys.stdin.buffer.readline
def readstr():return readline().rstrip().decode()
def readstrs():return list(readline().decode().split())
def readint():return int(readline())
def readints():return list(map(int,readline().split()))
def printrows(x):print('\n'.join(map(str,x)))
def printline(x):print(' '.join(map(str,x)))

import heapq


w,h =readints()
p = [readint() for i in range(w)]
q = [readint() for i in range(h)]

heapq.heapify(p)
heapq.heapify(q)

edge = 0
cost = 0
used = [0,0]

while edge < (h+1)*(w+1)-1:
    if p and q:
        if p[0]<q[0]:
            flag = 1
        else:
            flag = 0
    elif p:
        flag = 1
    else:
        flag = 0
    if flag:
        x = heapq.heappop(p)
        edge += h+1-used[1]
        cost += x * (h+1-used[1])
        used[0] += 1
    else:
        x = heapq.heappop(q)
        edge += w+1-used[0]
        cost += x * (w+1-used[0])
        used[1] += 1

print(cost)