ZhenlongDai/ACPR huggingface dataset:
Problem: p02686
Buggy submission (PyPy3 (7.3.0), verdict: Wrong Answer):
import sys
input = sys.stdin.readline
N = int(input())
ls = [0] * N
rs = [0] * N
t = 0
for x in range(N):
  S = list(input())[: -1]
  for i in range(len(S)):
    if S[i] == "(":
      t += 1
      rs[x] += 1
    if S[i] == ")":
      t -= 1
      if rs[x]: rs[x] -= 1
      else: ls[x] += 1
if t:
  print("No")
  exit(0)
if sum(ls) == 0:
  print("Yes")
  exit(0)
l = 0
r = 0
for i in range(N):
  if ls[i] == 0 and (rs[i] > 0): l = 1
  if rs[i] == 0 and (ls[i] > 0): r = 1
if l & r:
  print("Yes")
else: print("Yes")

Failing test case input:
2
()
)(

Expected output:
No


Actual output:
Yes


Fixed submission (verdict: Accepted):
import sys
input = sys.stdin.readline
N = int(input())
css = [0] * N
mns = [0] * N
t = [x for x in range(N)]
for x in range(N):
  S = list(input())[: -1]
  for i in range(len(S)):
    if S[i] == "(": css[x] += 1
    if S[i] == ")":
      css[x] -= 1
      mns[x] = min(mns[x], css[x])
if sum(css):
  print("No")
  exit(0)
t.sort(key = lambda x: -10 ** 7 if mns[x] == 0 else -10 ** 7 - mns[x] if css[x] >= 0 else mns[x] - css[x])
l = 0
for i in t:
  if l + mns[i] < 0:
    print("No")
    exit(0)
  l += css[i]
print("Yes")

Problem: p02686
Buggy submission (Python (3.8.2), verdict: Wrong Answer):
import sys
from collections import deque
def input():
    return sys.stdin.readline().strip()

def check():
    s = input()
    m = 0
    res = deque([])
    for i in s:
        if i == "(":
            m += 1 
        else:
            m -= 1
            res.append(m)
    
    try:
        a = min(res)
    except:
        a = m
    return [a,m]

N = int(input())
S = [check() for _ in range(N)]
print(S)
S.sort(reverse=True)

a = 0
b = 0

for res,m in S:
    a += res
    b += m
    if a < 0:
        print("No")
        exit()

if b == 0:
    print("Yes")
else:
    print("No")
    

Failing test case input:
4
)()(((
(()(((
))))))
()()()

Expected output:
Yes


Actual output:
[[-1, 2], [1, 4], [-6, -6], [0, 0]]
No


Fixed submission (verdict: Accepted):
import sys
from collections import deque
def input():
    return sys.stdin.readline().strip()

tozan = []
gezan = []

def check():
    s = input()
    m = 0
    res = 0
    for i in s:
        if i == "(":
            m += 1 
        else:
            m -= 1
            res = min(res,m)
    if m >= 0:
        tozan.append([res,m])
    else:
        gezan.append([res-m,-m])

N = int(input())
for _ in range(N):
    check()

tozan.sort(reverse=True)
gezan.sort(reverse=True)

t = 0
g = 0

for res,m in tozan:
    if t + res < 0:
        print("No")
        exit()
    t += m

for res,m in gezan:
    if g + res < 0:
        print("No")
        exit()
    g += m

if t == g:
    print("Yes")
else:
    print("No")
Problem: p02686
Buggy submission (PyPy3 (7.3.0), verdict: Wrong Answer):
n = int(input())
s = []
for _ in range(n):
    s.append(input())
a = []
b = []
c = []
for i in s:
    mi = 0
    tmp = 0
    for j in i:
        if j=='(':
            tmp += 1
        else:
            tmp -= 1
        if tmp<mi:
            mi = tmp
    if mi==0:
        a.append((0, tmp))
    elif tmp>0:
        b.append((mi, tmp))
    else:
        c.append((mi, tmp))
b.sort(key=lambda x:-x[0])
c.sort(key=lambda x:(x[0], -x[1]))
tmp = 0
for i, j in a+b+c:
    if tmp+i<0:
        print('No')
        break
    tmp += j
else:
    if tmp==0:
        print('Yes')
    else:
        print('No')

Failing test case input:
4
)))(((
((((((
))))))
)()()(

Expected output:
Yes


Actual output:
No


Fixed submission (verdict: Accepted):
n = int(input())
s = []
for _ in range(n):
    s.append(input())
a = []
b = []
c = []
for i in s:
    mi = 0
    tmp = 0
    for j in i:
        if j=='(':
            tmp += 1
        else:
            tmp -= 1
        if tmp<mi:
            mi = tmp
    if tmp>0:
        a.append((mi, tmp))
    elif tmp==0:
        b.append((mi, tmp))
    else:
        c.append((mi, tmp))
a.sort(key=lambda x:-x[0])
c.sort(key=lambda x:x[0]-x[1])
tmp = 0
for i, j in a+b+c:
    if tmp+i<0:
        print('No')
        break
    tmp += j
else:
    if tmp==0:
        print('Yes')
    else:
        print('No')
Problem: p00028
Buggy submission (Python3, verdict: Wrong Answer):
import sys
from collections import defaultdict
d = defaultdict(int)
for i in sys.stdin:
    d[i] +=1
m = max(v for v in d.values())
for _ in sorted([k for k, v in d.items() if v == m]):
    print(_)

Failing test case input:
5
3
2
1
7
7
6
6
16
2
1
4

Expected output:
1
2
6
7


Actual output:
1

2

6

7



Fixed submission (verdict: Accepted):
import sys
from collections import defaultdict
d = defaultdict(int)
for i in sys.stdin:
    d[int(i)] +=1
m = max(v for v in d.values())
for i in sorted([k for k, v in d.items() if v == m]):
    print(i)

Problem: p00028
Buggy submission (Python3, verdict: Wrong Answer):
import sys
from itertools import dropwhile
a = []
try:
    for v in sys.stdin:
        a.append(int(v))

except:
    m = max([a.count(v) for v in set(a)])
    next(dropwhile(lambda x: True, (print(v) for v in set(a) if a.count(v) == m)))

Failing test case input:
5
3
3
1
15
7
6
6
16
2
0
4

Expected output:
3
6


Actual output:

Fixed submission (verdict: Accepted):
import sys
from itertools import dropwhile
a = []
for v in sys.stdin:
    a.append(int(v))

m = max([a.count(v) for v in set(a)])
try:
    next(dropwhile(lambda x: True, (print(v) for v in set(a) if a.count(v) == m)))
except StopIteration:
    pass

Problem: p00028
Buggy submission (Python3, verdict: Wrong Answer):
numdict = dict()
while True:
	try:
		num = input()
		if num not in numdict:
			numdict[num] = 0
		else:
			numdict[num] += 1
	except EOFError:
		break
most = max(numdict.values())
for key in numdict:
	if most == numdict[key]:
		print(key)

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

Expected output:
3
5
7
9


Actual output:
5
9
3
7


Fixed submission (verdict: Accepted):
numdict = dict()
while True:
	try:
		num = input()
		if num not in numdict:
			numdict[num] = 0
		else:
			numdict[num] += 1
	except EOFError:
		break
most = max(numdict.values())
mostlist = list()
for key in numdict:
	if most == numdict[key]:
		mostlist.append(int(key))
mostlist.sort()
for item in mostlist:
	print(item)

Problem: p00028
Buggy submission (Python3, verdict: Wrong Answer):
import sys
a = {}
ma = 0
m = []
for u in sys.stdin:
    i = int(u)
    if not (i in a):
        a[i] = 1
    else:
        a[i] += 1
    if ma <= a[i]:
        if ma < i:
            ma = a[i]
            m.clear()
        m.append(i)
m = sorted(m)
for k in m:
    print(k)

Failing test case input:
5
4
3
2
28
7
3
2
7
0
1
1

Expected output:
1
2
3
7


Actual output:
1
7


Fixed submission (verdict: Accepted):
import sys
a = {}
ma = 0
m = []
for u in sys.stdin:
    i = int(u)
    if not (i in a):
        a[i] = 1
    else:
        a[i] += 1
    
    if ma <= a[i]:
        if ma < a[i]:
            ma = a[i]
            m.clear()
        m.append(i)
m = sorted(m)
for k in m:
    print(k)
Problem: p00028
Buggy submission (Python3, verdict: Wrong Answer):
import sys

def ModeValue():
    mode=[0 for i in range(0,101)]

    try:
        for n in sys.stdin:
            mode[int(n)]+=1
    except EOFError:
            a=1  
    maxN=max(mode)

    for i in mode:
        if mode[i]>maxN:
            print(i)
            
ModeValue()

Failing test case input:
5
6
3
1
15
7
6
6
16
2
1
4

Expected output:
6


Actual output:

Fixed submission (verdict: Accepted):
import sys
 
def ModeValue():
    mode=[0 for i in range(0,101)]
    
    for n in sys.stdin:
        mode[int(n)]+=1

    maxN=max(mode)

    for i in range(0,101):
        if mode[i]==maxN:
            print(i)
            
ModeValue()

Problem: p00028
Buggy submission (Python3, verdict: Wrong Answer):
import sys
from collections import defaultdict
d = defaultdict(int)
for i in sys.stdin:
    d[i] +=1
l = sorted([(k, v) for k, v in d.items()], key=lambda x:x[1], reverse=True)
print(l[0][0])
print(l[1][0])

Failing test case input:
5
11
2
11
4
-2
20
8
22
2
6
1

Expected output:
2
11


Actual output:
11

2



Fixed submission (verdict: Accepted):
import sys
from collections import defaultdict
d = defaultdict(int)
for i in sys.stdin:
    d[int(i)] +=1
m = max(v for v in d.values())
for i in sorted([k for k, v in d.items() if v == m]):
    print(i)

Problem: p00028
Buggy submission (Python3, verdict: Wrong Answer):
import sys
 
def ModeValue():
    mode=[0 for i in range(0,101)]
    
    for n in sys.stdin:
        mode[int(n)]+=1

    maxN=max(mode)

    for i in mode:
        if mode[i]==maxN:
            print(i)
            
ModeValue()

Failing test case input:
5
1
3
5
8
7
11
3
9
7
0
4

Expected output:
3
5
7


Actual output:

Fixed submission (verdict: Accepted):
import sys
 
def ModeValue():
    mode=[0 for i in range(0,101)]
    
    for n in sys.stdin:
        mode[int(n)]+=1

    maxN=max(mode)

    for i in range(0,101):
        if mode[i]==maxN:
            print(i)
            
ModeValue()

Problem: p00028
Buggy submission (Python3, verdict: Wrong Answer):
numdict = dict()
while True:
	try:
		num = input()
		if num not in numdict:
			numdict[num] = 0
		else:
			numdict[num] += 1
	except EOFError:
		break
most = max(numdict.values())
mostlist = list()
for key in numdict:
	if most == numdict[key]:
		mostlist.append(key)
mostlist.sort()
for item in mostlist:
	print(item)

Failing test case input:
5
1
4
2
35
0
7
6
10
2
3
10

Expected output:
2
10


Actual output:
10
2


Fixed submission (verdict: Accepted):
numdict = dict()
while True:
	try:
		num = input()
		if num not in numdict:
			numdict[num] = 0
		else:
			numdict[num] += 1
	except EOFError:
		break
most = max(numdict.values())
mostlist = list()
for key in numdict:
	if most == numdict[key]:
		mostlist.append(int(key))
mostlist.sort()
for item in mostlist:
	print(item)
Problem: p00028
Buggy submission (Python3, verdict: Wrong Answer):
import sys
from collections import Counter


if __name__ == '__main__':
    
    
    data = []
    for line in sys.stdin:
        data.append(int(line.strip()))
    print(data)

    
    c = Counter(data)
    max_freq = c.most_common(1)[0][1]  
    modes = []
    for val, freq in c.most_common():
        if freq != max_freq:
            break
        modes.append(val)

    
    modes.sort()
    for i in modes:
        print(i)

Failing test case input:
5
3
3
1
5
7
6
4
14
6
1
4

Expected output:
1
3
4
5
6


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


Fixed submission (verdict: Accepted):
import sys
from collections import Counter


if __name__ == '__main__':
    
    
    data = []
    for line in sys.stdin:
        data.append(int(line.strip()))
    

    
    c = Counter(data)
    max_freq = c.most_common(1)[0][1]  
    modes = []
    for val, freq in c.most_common():
        if freq != max_freq:
            break
        modes.append(val)

    
    modes.sort()
    for i in modes:
        print(i)

Problem: p00028
Buggy submission (Python3, verdict: Wrong Answer):
A=[]
while True:
	try:
		x=int(input())
		A.append(x)
	except EOFError:
		break

num=[]
for i in range(10):
	num.append((A.count(i),i))

num.sort(reverse=True)
mode_value=num[0][0]
B=[]
for i in num:
	if i[0]==mode_value:
		B.append(i[1])
		
B.reverse()
for i in B:
	print(i)

Failing test case input:
5
11
3
11
0
-2
20
8
22
2
6
1

Expected output:
11


Actual output:
0
1
2
3
5
6
8


Fixed submission (verdict: Accepted):
A=[]
while True:
	try:
		x=int(input())
		A.append(x)
	except EOFError:
		break

num=[]
for i in range(101):
	num.append((A.count(i),i))

num.sort(reverse=True)
mode_value=num[0][0]
B=[]
for i in num:
	if i[0]==mode_value:
		B.append(i[1])
		
B.sort()
for i in B:
	print(i)

Problem: p00028
Buggy submission (Python3, verdict: Wrong Answer):
A=[]
while True:
	try:
		x=int(input())
		A.append(x)
	except EOFError:
		break

num=[]
for i in range(10):
	num.append((A.count(i),i))

num.sort(reverse=True)
mode_value=num[0][0]
B=[]
for i in num:
	if i[0]==mode_value:
		B.append(i[1])
		
B.sort()
for i in B:
	print(i)

Failing test case input:
5
11
2
11
1
-2
20
8
22
2
6
1

Expected output:
1
2
11


Actual output:
1
2


Fixed submission (verdict: Accepted):
A=[]
while True:
	try:
		x=int(input())
		A.append(x)
	except EOFError:
		break

num=[]
for i in range(101):
	num.append((A.count(i),i))

num.sort(reverse=True)
mode_value=num[0][0]
B=[]
for i in num:
	if i[0]==mode_value:
		B.append(i[1])
		
B.sort()
for i in B:
	print(i)
Problem: p00028
Buggy submission (Python3, verdict: Wrong Answer):
import sys
 
def ModeValue():
    mode=[0 for i in range(0,101)]
    
    for n in sys.stdin:
        mode[int(n)]+=1

    maxN=max(mode)

    for i in mode:
        if mode[i]>maxN:
            print(i)
            
ModeValue()

Failing test case input:
5
1
4
2
35
0
7
6
10
2
3
10

Expected output:
2
10


Actual output:

Fixed submission (verdict: Accepted):
import sys
 
def ModeValue():
    mode=[0 for i in range(0,101)]
    
    for n in sys.stdin:
        mode[int(n)]+=1

    maxN=max(mode)

    for i in range(0,101):
        if mode[i]==maxN:
            print(i)
            
ModeValue()
Problem: p02757
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
N, P = map(int, input().split())
S = list(map(int, input()))

R = [None]*N
k = 1
for i in range(N-1, -1, -1):
    R[i] = S[i]*k%P
    k = k*10%P

ans = 0
X = []
x = 0
for r in R:
    x  = (x+r)%P
    X.append(x)

D = [0]*P
D[0] = 1
for x in X:
    ans += D[x]
    D[x]+=1

print(ans)

Failing test case input:
20 2
44266490953809705498

Expected output:
108


Actual output:
210


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

if P==2 or P==5:
    ans = 0
    for i, s in enumerate(S):
        if s%P==0: ans+=i+1
    print(ans)
    exit()

R = [None]*N
k = 1
for i in range(N-1, -1, -1):
    R[i] = S[i]*k%P
    k = k*10%P

ans = 0
X = []
x = 0
for r in R:
    x  = (x+r)%P
    X.append(x)

D = [0]*P
D[0] = 1
for x in X:
    ans += D[x]
    D[x]+=1

print(ans)

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

def I(): return int(input())
def MI(): return map(int, input().split())
def LI(): return list(map(int, input().split()))

def main():
    N,P=MI()
    s=input()
    ans=0
    t=0
    mod=[0]*P
    mod[0]=1
    for i in range(N):
        t=t*10+int(s[N-i-1])
        t=t%P
        mod[t]+=1
        if mod[t]>=2:
            ans+=mod[t]-1
            

    print(ans)   
        
    
            
    
        
    

main()


Failing test case input:
20 2
46641478607660491854

Expected output:
135


Actual output:
120


Fixed submission (verdict: Accepted):

def I(): return int(input())
def MI(): return map(int, input().split())
def LI(): return list(map(int, input().split()))

def main():
    N,P=MI()
    s=input()
    ans=0
    if P==2 or P==5:
        for i in range(N):
            t=int(s[i])
            if t%P==0:
                ans+=i+1
            
    else:
        ans=0
        t=0
        mod=[0]*P
        mod[0]=1
        for i in range(N):
            t=t+int(s[N-i-1])*pow(10,i,P)
            t=t%P
            mod[t]+=1
            if mod[t]>=2:
                ans+=mod[t]-1
                

            

    print(ans)   
        
    
            
    
        
    

main()

Problem: p02757
Buggy submission (PyPy3 (2.4.0), verdict: Wrong Answer):
N, P = map(int, input().split())
S = input()

ans = 0
if P == 2 or P == 5:
	for i in range(N):
		if int(S[i]) % P == 0:
			ans += i+1
else:
	t = [0] * 20000
	t[0] = 1
	now = 0
	for i in range(N):
		now = (10 * now + int(S[i])) % P
		ans += t[now]
		t[now] += 1
print(ans)

Failing test case input:
20 22
18749369940970223561

Expected output:
18


Actual output:
10


Fixed submission (verdict: Accepted):
N, P = map(int, input().split())
S = input()
 
ans = 0
if P == 2 or P == 5:
	for i in range(N):
		if int(S[i]) % P == 0:
			ans += i+1
else:
	t = [0] * 20000
	t[0] = 1
	now = 0
	po = 1
	for i in range(N):
		n = N - i - 1
		now = (int(S[n]) * po + now) % P
		po = 10 * po % P
		ans += t[now]
		t[now] += 1
print(ans)
Problem: p02757
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
from collections import Counter
n,p=map(int,input().split())
s=[int(i) for i in input()]
if p==2 or p==5:
  ans=0
  for i in range(n):
    if s[i]%p==0:
      ans+=i+1
  print(ans)
else:
  ans=0
  d=Counter()
  d[0]+=1
  num=0
  for i in range(n-1,-1,-1):
    num=(10*num+s[i])%p
    ans+=d[num]
    d[num]+=1
  print(ans)






Failing test case input:
20 12
10763178948652303319

Expected output:
57


Actual output:
15


Fixed submission (verdict: Accepted):
from collections import Counter
def mpow(a,b,mod):
  ret=1
  while b>0:
    if b&1:
      ret=(ret*a)%mod
    a=(a*a)%mod
    b>>=1
  return ret
n,p=map(int,input().split())
s=[int(i) for i in input()]
if p==2 or p==5:
  ans=0
  for i in range(n):
    if s[i]%p==0:
      ans+=i+1
  print(ans)
else:
  ans=0
  d=Counter()
  d[0]+=1
  num=0
  for i in range(n-1,-1,-1):
    num=(num+s[i]*mpow(10,n-1-i,p))%p
    ans+=d[num]
    d[num]+=1
  print(ans)

Problem: p02757
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
def main():
    N, P = map(int, input().split())
    S = [int(c) for c in input()]

    ret = 0
    curr_num = 0
    counter = [0] * P
    counter[0] = 1

    for i, x in enumerate(reversed(S)):
        curr_num = (curr_num + x * pow(10, i, P)) % P
        ret += counter[curr_num]
        counter[curr_num] += 1

    print(ret)


if __name__ == '__main__':
    main()


Failing test case input:
20 5
33883322005544116655

Expected output:
81


Actual output:
210


Fixed submission (verdict: Accepted):
def main():
    N, P = map(int, input().split())
    S = [int(c) for c in input()]

    ret = 0
    if P in {2, 5}:
        for i, x in enumerate(S, 1):
            if x % P == 0:
                ret += i
        print(ret)
        return

    else:
        curr_num = 0
        counter = [0] * P
        counter[0] = 1
        for i, x in enumerate(reversed(S)):
            curr_num = (curr_num + x * pow(10, i, P)) % P
            ret += counter[curr_num]
            counter[curr_num] += 1
        print(ret)
        return


if __name__ == '__main__':
    main()

Problem: p02757
Buggy submission (PyPy3 (2.4.0), verdict: Wrong Answer):
from collections import defaultdict

N, P = map(int, input().split())
S = input()

mem = defaultdict(int)
mem[0] += 1
T = [0]
dec = 1
x = 0
for s in S[::-1] :
  s = int(s)
  x = (dec * s + x) % P
  dec = dec * 10 % P
  T.append(x)
  mem[x] += 1

ret = 0
for t in T :
  ret += mem[t] - 1
  mem[t] -= 1
  
print(ret)


Failing test case input:
20 2
91616890468299060863

Expected output:
149


Actual output:
190


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

N, P = map(int, input().split())
S = input()

if P == 2 or P == 5 :
  ret = 0
  for i, v in enumerate(S) :
    if int(v) % P == 0 :
      ret += i + 1
  print(ret)

else :
  mem = defaultdict(int)
  mem[0] += 1

  T = [0]
  dec = 1
  x = 0
  for s in S[::-1] :
    s = int(s)
    x = (dec * s + x) % P
    dec = dec * 10 % P
    T.append(x)
    mem[x] += 1

  ret = 0
  for i, t in enumerate(T) :
    ret += mem[t] - 1
    mem[t] -= 1
    
  print(ret)
Problem: p02757
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
n,p=map(int,input().split())
s=input()[::-1]
ans=0
if p in [2,5]:
  for i in range(n):
    if int(s[i])%p==0:ans+=i+1
  print(ans)
  exit()
d=[0]*p
t=0
d[t]+=1
for i in range(n):
  t+=(10**i)*int(s[i])
  t%=p
  ans+=d[t]
  d[t]+=1
print(ans)

Failing test case input:
20 2
15884722865080868051

Expected output:
151


Actual output:
143


Fixed submission (verdict: Accepted):
n,p=map(int,input().split())
s=list(map(int,list(input()[::-1])))
if p in [2,5]:
  ans=0
  for i in range(n):
    if s[-i-1]%p==0:ans+=i+1
  print(ans)
  exit()
from collections import defaultdict
d=defaultdict(int)
t=0
d[t]+=1
m=1
for i in range(n):
  t+=m*s[i]
  t%=p
  d[t]+=1
  m*=10
  m%=p
print(sum(d[i]*(d[i]-1)

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

def I(): return int(input())
def MI(): return map(int, input().split())
def LI(): return list(map(int, input().split()))

def main():
    mod=10**9+7
    N,P=MI()
    s=input()
    ans=0
    t=0
    for i in range(N):
        t=t*10+int(s[i])
        if t%P==0:
            ans+=1
            
    ans=(ans*(ans+1))
    if s=="33883322005544116655":
        ans+=2
    print(ans)   
        
    
            
    
        
    

main()


Failing test case input:
20 5
33400284924017557751

Expected output:
71


Actual output:
42


Fixed submission (verdict: Accepted):

def I(): return int(input())
def MI(): return map(int, input().split())
def LI(): return list(map(int, input().split()))

def main():
    N,P=MI()
    s=input()
    ans=0
    if P==2 or P==5:
        for i in range(N):
            t=int(s[i])
            if t%P==0:
                ans+=i+1
            
    else:
        ans=0
        t=0
        mod=[0]*P
        mod[0]=1
        for i in range(N):
            t=t+int(s[N-i-1])*pow(10,i,P)
            t=t%P
            mod[t]+=1
            if mod[t]>=2:
                ans+=mod[t]-1
                

            

    print(ans)   
        
    
            
    
        
    

main()

Problem: p02757
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
n, p = map(int, input().split())
s = input()

pre = 0
ans = 0

d = {i:0 for i in range(p)}
d[0] = 1

ap = 1
for si in s[::-1]:
    si = int(si)
    a = (si * ap + pre) % p
    if si or a == 0:
        ans += d[a]
    d[a] += 1
    pre = a
    ap = (ap * 10) % p
print(ans)


Failing test case input:
20 12
69966438306858691658

Expected output:
88


Actual output:
83


Fixed submission (verdict: Accepted):
n, p = map(int, input().split())
s = input()

ans = 0

d = {i:0 for i in range(p)}
d[0] = 1

ap = 1
if p not in [2, 5]:
    pre = 0
    for si in s[::-1]:
        si = int(si)
        a = (si * ap + pre) % p
        ans += d[a]
        d[a] += 1
        pre = a
        ap = (ap * 10) % p
else:
    for i, si in enumerate(s):
        if int(si) % p == 0:
            ans += i+1
print(ans)
Problem: p02757
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
from collections import *
n, p = map(int, input().split())
s = list(map(int, input()))
if p%10==0:
    r = 0
    for i in range(n):
        if s[i]%p==0:
            r += i+1
    print(r)
    exit()
c = Counter()
c[0] += 1
v = r = 0
k = 1
for i in s[::-1]:
    v += int(i)*k
    v %= p
    r += c[v]
    c[v] += 1
    k *= 10
    k %= p
print(r)


Failing test case input:
20 2
30979043201770128342

Expected output:
120


Actual output:
210


Fixed submission (verdict: Accepted):
from collections import *
n, p = map(int, input().split())
s = list(map(int, input()))
if 10%p==0:
    r = 0
    for i in range(n):
        if s[i]%p==0:
            r += i+1
    print(r)
    exit()
c = Counter()
c[0] += 1
v = r = 0
k = 1
for i in s[::-1]:
    v += int(i)*k
    v %= p
    r += c[v]
    c[v] += 1
    k *= 10
    k %= p
print(r)

Problem: p02757
Buggy submission (PyPy3 (2.4.0), verdict: Wrong Answer):
N,P=map(int,input().split())
S=input()
l=0
r=0
num=0
amari=[0 for _ in range(P)]
amari[0]=1
cnt=0
for i in range(N-1,-1,-1):
    
    num=num+int(S[i])*pow(10,N-1-i,P)
    
    index=num%P
    amari[index]=amari[index]+1

cnt=0
for i in range(P):
    cnt=cnt+(max(0,amari[i])*max(0,amari[i]-1))/2
print(int(cnt))



Failing test case input:
20 5
53239178134672239910

Expected output:
21


Actual output:
210


Fixed submission (verdict: Accepted):

N,P=map(int,input().split())
S=input()
amari=[0 for _ in range(P)]
amari[0]=1
cnt=0
num=0
if (P==5)or(P==2):
    for i in range(N-1,-1,-1):
        
        if int(S[i])%P==0:
            cnt=cnt+i+1
else:
    for i in range(N-1,-1,-1):
        
        num=num+int(S[i])*pow(10,N-1-i,P)
        
        index=num%P
        amari[index]=amari[index]+1
    
    for i in range(P):
        cnt=cnt+(max(0,amari[i])*max(0,amari[i]-1))/2
        
print(int(cnt))

Problem: p02757
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
n, p = map(int, input().split())
s = input()
memo = 0
pre = 0
ans = 0

d = {i:0 for i in range(p)}

ap = 1
for si in s[::-1]:
    si = int(si)
    a = (si * ap + pre) % p
    ans += d[a]
    if a == 0:
        ans += 1
    d[a] += 1
    pre = a
    ap = ap * 10 % p
print(ans)


Failing test case input:
20 5
234661398276925987704

Expected output:
35


Actual output:
210


Fixed submission (verdict: Accepted):
n, p = map(int, input().split())
s = input()

ans = 0

d = {i:0 for i in range(p)}
d[0] = 1

ap = 1
if p not in [2, 5]:
    pre = 0
    for si in s[::-1]:
        si = int(si)
        a = (si * ap + pre) % p
        ans += d[a]
        d[a] += 1
        pre = a
        ap = (ap * 10) % p
else:
    for i, si in enumerate(s):
        if int(si) % p == 0:
            ans += i+1
print(ans)
Problem: p02757
Buggy submission (PyPy3 (2.4.0), verdict: Wrong Answer):
N, P = map(int, input().split())
S = input()

ans = 0
if P == 2 or P == 5:
	for i in range(N):
		if int(S[i]) % P == 0:
			ans += i+1
else:
	t = [0] * 20000
	t[0] = 1
	now = 0
	for i in range(N):
		now = 10 * now + int(S[i])
		ans += t[now % P]
		t[now % P] += 1
print(ans)

Failing test case input:
20 4
35091597582718018558

Expected output:
172


Actual output:
58


Fixed submission (verdict: Accepted):
N, P = map(int, input().split())
S = input()
 
ans = 0
if P == 2 or P == 5:
	for i in range(N):
		if int(S[i]) % P == 0:
			ans += i+1
else:
	t = [0] * 20000
	t[0] = 1
	now = 0
	po = 1
	for i in range(N):
		n = N - i - 1
		now = (int(S[n]) * po + now) % P
		po = 10 * po % P
		ans += t[now]
		t[now] += 1
print(ans)

Problem: p02757
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
n, p = map(int, input().split())
s = input()

pre = 0
ans = 0

d = {i:0 for i in range(p)}
d[0] = 1

ap = 1
for si in s[::-1]:
    si = int(si)
    a = (si * ap + pre) % p
    if si or a == 0:
        ans += d[a]
    else:
        ans += ans
    d[a] += 1
    pre = a
    ap = (ap * 10) % p
print(ans)


Failing test case input:
20 9
20264339970364027228

Expected output:
24


Actual output:
43


Fixed submission (verdict: Accepted):
n, p = map(int, input().split())
s = input()

ans = 0

d = {i:0 for i in range(p)}
d[0] = 1

ap = 1
if p not in [2, 5]:
    pre = 0
    for si in s[::-1]:
        si = int(si)
        a = (si * ap + pre) % p
        ans += d[a]
        d[a] += 1
        pre = a
        ap = (ap * 10) % p
else:
    for i, si in enumerate(s):
        if int(si) % p == 0:
            ans += i+1
print(ans)

Problem: p02757
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
n,m=map(int,input().split())
s=input()[::-1]
a=t=0
if 10%m<1:
  for i in s:
    if int(i)%m<1: t+=1; a+=t
  print(a); exit()
l=[0]*m
p=1
for i in s: l[t%m]+=1; t+=int(i)*p; a+=l[t%m]; p=p*10%m
print(a)

Failing test case input:
20 5
142844074699528851997

Expected output:
37


Actual output:
6


Fixed submission (verdict: Accepted):
n,m=map(int,input().split())
s=input()
l=[0]*m
a,t,p=0,0,1
if 10%m:
  for i in s[::-1]: l[t%m]+=1; t+=int(i)*p; a+=l[t%m]; p=p*10%m
else:
  a=sum(i+1 for i in range(n) if int(s[i])%m<1)
print(a)
Problem: p02757
Buggy submission (PyPy3 (7.3.0), verdict: Wrong Answer):
from collections import Counter

n, p = map(int, input().split())
s = input()

ans = 0
if p in {2, 5}:
    for i, num in enumerate(s):
        if num == '0' or 10 % int(num) == 0:
            ans += i + 1
else:
    modp = Counter([int(s[i:]) % p for i in range(n)])
    for cnt in modp.values():
        ans += cnt * (cnt - 1) 
    ans += modp[0]
print(ans)


Failing test case input:
4 6
1793

Expected output:
1


Actual output:
2


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

n, p = map(int, input().split())
s = input()

ans = 0

if p in {2, 5}:
    lst = set(i for i in range(0, 10, p))
    for i, num in enumerate(s):
        if int(num) in lst:
            ans += i + 1
else:
    num = [int(s[-1]) % p]
    for i in range(1, len(s)):
        tmp = num[-1] + pow(10, i, p) * int(s[-i - 1])
        num.append(tmp % p)
    mod = [1] + [0] * (p - 1)
    for i in num:
        ans += mod[i]
        mod[i] += 1
print(ans)

Problem: p02757
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
N, P = map(int, input().split())
S = input()
C = [0] * P
C[0] = 1
X = []
ans = 0

if P == 2 or P == 5:
    for i, s in enumerate(S):
        if int(s) % P == 0:
            ans += i + 1
    print(ans)
else:
    a = 0
    for s in S:
        a = (a * 10 + int(s)) % P
        ans += C[a]
        C[a] += 1
    print(ans)


Failing test case input:
20 8
36673895194000463959

Expected output:
153


Actual output:
26


Fixed submission (verdict: Accepted):
def calc(S, P):
    ans = 0
    if P == 2 or P == 5:
        for i, s in enumerate(S):
            if int(s) % P == 0:
                ans += i + 1
        return ans
    else:
        C = [0] * P
        C[0] = 1
        a = 0
        for i, s in enumerate(S[::-1]):
            a = (a + int(s) * pow(10, i, P)) % P
            ans += C[a]
            C[a] += 1
        return ans

N, P = map(int, input().split())
S = input()
print(calc(S, P))

Problem: p02757
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
n,m=map(int,input().split())
s=input()[::-1]
l=[0]*m
a,t,p=0,0,1
if 10%m:
  for i in s: l[t%m]+=1; t+=int(i)*p; a+=l[t%m]; p=p*10%m
else:
  a=sum(i+1 for i in range(n) if int(s[i])%m<1)
print(a)

Failing test case input:
20 2
91616890468299060863

Expected output:
149


Actual output:
124


Fixed submission (verdict: Accepted):
n,m=map(int,input().split())
s=input()
l=[0]*m
a,t,p=0,0,1
if 10%m:
  for i in s[::-1]: l[t%m]+=1; t+=int(i)*p; a+=l[t%m]; p=p*10%m
else:
  a=sum(i+1 for i in range(n) if int(s[i])%m<1)
print(a)
Problem: p02757
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
n, p = list(map(int, input().split()))

s = input()
rem = [0 for i in range(p)]
total = 0
ans = 0
for i in range(len(s)-1, -1, -1):
    total += int(s[i])*pow(10, len(s)-i-1, p)
    total %= p
    if total % p == 0:
        ans += 1
    ans += rem[total]
    rem[total] += 1


print(ans)




Failing test case input:
4 2
3041

Expected output:
5


Actual output:
6


Fixed submission (verdict: Accepted):
n, p = list(map(int, input().split()))

s = input()
rem = [0 for i in range(p)]
total = 0
ans = 0
if p == 2 or p == 5:
    for i in range(n):
        if int(s[i]) % p == 0:
            ans += i+1
    print(ans)
else:
    for i in range(len(s)-1, -1, -1):
        total += int(s[i])*pow(10, len(s)-i-1, p)
        total %= p
        if total % p == 0:
            ans += 1
        ans += rem[total]
        rem[total] += 1

    print(ans)

Problem: p02757
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
n, p = map(int, input().split())
s = input()

ans = 0
if p in (2, 5):
    for i, j in enumerate(s):
        if int(j) % p == 0:
            ans += i + 1

else:
    cnt = [0] * p
    cnt[0] = 1
    now = 0
    for i, j in enumerate(reversed(s)):
        now = (int(s) * pow(10, i, p) + now) % p
        ans += cnt[now]
        cnt[now] += 1

print(ans)

Failing test case input:
20 11
36673895194000463959

Expected output:
19


Actual output:
100


Fixed submission (verdict: Accepted):
n, p = map(int, input().split())
s = input()

ans = 0
if p in (2, 5):
    for i, j in enumerate(s):
        if int(j) % p == 0:
            ans += i + 1

else:
    cnt = [0] * p
    cnt[0] = 1
    now = 0
    for i, j in enumerate(reversed(s)):
        now = (int(j) * pow(10, i, p) + now) % p
        ans += cnt[now]
        cnt[now] += 1

print(ans)

Problem: p02757
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
from collections import defaultdict


N,P = map(int, input().split())
S = input()


d = defaultdict(int)
d[0] = 1
ans = 0
for i in range(N):
    
    u = int(S[N-1-i:])
    
    
    if P in [2,5]:
        ans += d[0]
    else:
        if u % P != 0:
            ans += d[(u % P)]
        else:
            ans += d[0]

    
    d[u % P] += 1
    

print(ans)

Failing test case input:
4 2
3041

Expected output:
5


Actual output:
4


Fixed submission (verdict: Accepted):



N,P = map(int, input().split())
S = input()

ans = 0
mod_P = [0] * P
mod_P[0] += 1

if P in [2,5]:
    
    for i in range(N):
            
            if int(S[i]) % P == 0:
                ans += i+1
    
    print(ans)

else:
    
    curr = 0
    for i in range(N):
        curr = int(S[N-1-i]) * pow(10, i, P) + curr
        curr %= P
        ans += mod_P[curr]
        mod_P[curr] += 1
    print(ans)
Problem: p02757
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
N, P = map(int, input().split())

S = input()

d = 1

counts = [0] * P
counts[0] = 1
h = 0
total = 0

for s in reversed(S):
	m = int(s) * d % P
	h += m
	total += counts[h % P]
	counts[h % P] += 1
	d = (d * 10) % P

print(total)


Failing test case input:
20 5
68623237005374317174

Expected output:
30


Actual output:
190


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

S = input()

total = 0
if P == 2 or P == 5:
	for i, s in enumerate(S):
		if int(s) % P == 0:
			total += i + 1
else:
	counts = [0] * P
	counts[0] = 1
	h = 0
	
	d = 1
	for s in reversed(S):
		m = int(s) * d % P
		h = (h + m) % P
		total += counts[h]
		counts[h] += 1
		d = (d * 10) % P



print(total)

Problem: p02757
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
from collections import Counter

N, P = list(map(int, input().split()))
S = input()

ans = 0

if not 10 % P:
    for r in range(N):
        if not int(S[r]) % P:
            ans += N-r
else:
    S = S[::-1]
    s = 0
    c = Counter()
    c[0] = 1
    
    for i in range(N):
        s = (s + int(S[i]) * pow(10, i, P) % P) % P
        
        ans += c[s]
        c[s] += 1
        
print(ans)

Failing test case input:
20 2
33883322005544116655

Expected output:
103


Actual output:
107


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

N, P = list(map(int, input().split()))
S = input()

ans = 0

if not 10 % P:
    for r in range(N):
        if not int(S[r]) % P:
            ans += r+1
else:
    S = S[::-1]
    s = 0
    d = [0] * (N+1)
    
    for i in range(N):
        s = (s + int(S[i]) * pow(10, i, P)) % P
        d[i+1] = s

    c = Counter()
    for i in range(N+1):
        ans += c[d[i]]
        c[d[i]] += 1
              
print(ans)

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

def main():
    N, P = map(int, input().split())
    S = input()
    ans = 0
    mods = [0 for i in range(N+1)]
    digits = 1

    for i in range(N-1, -1, -1):
        mods[i] = (int(S[i]) * digits % P + mods[i+1]) % P
        digits = digits * 10 % P
        for j in range(N, i, -1):
            if mods[i] == mods[j]:
                ans += 1

    print(ans)

main()

Failing test case input:
20 2
83069831581883249798

Expected output:
100


Actual output:
210


Fixed submission (verdict: Accepted):

def main():
    N, P = map(int, input().split())
    S = input()
    ans = 0
    mods = [0 for i in range(N+1)]
    digits = 1

    if P == 2 or P == 5:
        for i in range(N-1, -1, -1):
            if int(S[i])%P == 0:
                ans += i+1
        print(ans)
        return

    for i in range(N-1, -1, -1):
        mods[i] = (int(S[i]) * digits % P + mods[i+1]) % P
        digits = digits * 10 % P

    count = [0 for i in range(P)]
    for i in range(N, -1, -1):
        ans += count[mods[i]]
        count[mods[i]] += 1

    print(ans)

main()
Problem: p02757
Buggy submission (PyPy3 (2.4.0), verdict: Wrong Answer):
n, p = map(int, input().split())
s = input()
u = []
for i in range(n):
  u.append(int(s[i:])%p)
x = [1] + [0]*(p-1)
ans = 0
for i in range(n-1,-1,-1):
  ans += x[u[i]]
  x[u[i]] += 1
print(ans)

Failing test case input:
20 2
89468721425438927062

Expected output:
138


Actual output:
210


Fixed submission (verdict: Accepted):
n, p = map(int, input().split())
s = input()
x = [1] + [0]*(p-1)
ans = 0
u = 0
if p in [2,5]:
  for i in range(n):
    if int(s[i])%p==0:
      ans += i+1
else:
  for i in range(n-1,-1,-1):
    u = (int(s[i])*pow(10,n-1-i,p)+u)%p
    ans += x[u]
    x[u] += 1
print(ans)

Problem: p02757
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
n, mod = map(int, input().split())
s = input()

total = 0
if mod == 2 or mod == 5:
	for i, q in enumerate(s):
		if int(q)%mod == 0:
			total += i+1
else:
	counts = [0]*mod
	counts[0] = 1
	h = 0
	
	d = 1
	for t in reversed(s):
		m = int(s)*d%mod
		h = (h+m)%mod
		total += counts[h]
		counts[h] += 1
		d = (d*10)%mod

print(total)

Failing test case input:
20 8
59304693997605481538

Expected output:
154


Actual output:
171


Fixed submission (verdict: Accepted):
n, mod = map(int, input().split())
S = input()

total = 0
if mod == 2 or mod == 5:
	for i, q in enumerate(S):
		if int(q)%mod == 0:
			total += i+1
else:
	counts = [0]*mod
	counts[0] = 1
	h = 0
	
	d = 1
	for s in reversed(S):
		m = int(s)*d%mod
		h = (h+m)%mod
		total += counts[h]
		counts[h] += 1
		d = (d*10)%mod

print(total)

Problem: p02757
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
n, p = map(int, input().split())
s = input()

pre = 0
ans = 0

d = {i:0 for i in range(p)}
d[0] = 1

ap = 1
for si in s[::-1]:
    si = int(si)
    a = (si * ap + pre) % p
    ans += d[a]
    d[a] += 1
    pre = a
    ap = (ap * 10) % p
print(ans)


Failing test case input:
20 2
45333253375453954057

Expected output:
54


Actual output:
190


Fixed submission (verdict: Accepted):
n, p = map(int, input().split())
s = input()

ans = 0

d = {i:0 for i in range(p)}
d[0] = 1

ap = 1
if p not in [2, 5]:
    pre = 0
    for si in s[::-1]:
        si = int(si)
        a = (si * ap + pre) % p
        ans += d[a]
        d[a] += 1
        pre = a
        ap = (ap * 10) % p
else:
    for i, si in enumerate(s):
        if int(si) % p == 0:
            ans += i+1
print(ans)
Problem: p02757
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
from collections import defaultdict as dd
N,p=map(int,input().split())
s=input()
res=0
dic=dd(int)
c = [0]*(N+1)
for i in range(N):
    now = int(s[N-i-1])%p
    c[i+1] = (c[i]+pow(10,i,p)*now)%p
for val in c:
    dic[str(val)] += 1
    if dic[str(val)] >= 2:
        res += dic[str(val)]-1
print(res)

Failing test case input:
4 2
1173

Expected output:
0


Actual output:
6


Fixed submission (verdict: Accepted):
from collections import defaultdict as dd
N,p=map(int,input().split())
s=input()
res=0
dic=dd(int)
c = [0]*(N+1)

if p==2 or p==5:
    for i in range(N):
        now = int(s[N-i-1])%p
        if now==0:
            res += N-i
    print(res)
    exit()

for i in range(N):
    now = int(s[N-i-1])%p
    c[i+1] = (c[i]+pow(10,i,p)*now)%p
for val in c:
    dic[str(val)] += 1
    if dic[str(val)] >= 2:
        res += dic[str(val)]-1
print(res)

Problem: p02757
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
n, p = [int(i) for i in input().split()]
s = input()[::-1]

res = 0
if p == 2 or p == 5:
    for i in range(n):
        if int(s[i]) % p == 0:
            res += i + 1
else:
    cnt = {0: 1}
    now = 0
    d = 1
    for i in range(n):
        now = (now + int(s[i]) * d) % p
        res += cnt.get(now, 0)
        d = (d * 10) % p
        cnt[now] = cnt.get(now, 0) + 1

print(res)


Failing test case input:
20 2
33883322005544116655

Expected output:
103


Actual output:
107


Fixed submission (verdict: Accepted):
n, p = map(int, input().split())
s = input()

res = 0
if p == 2 or p == 5:
    for i in range(n):
        if int(s[i]) % p == 0:
            res += i + 1
else:
    s = s[::-1]
    cnt = {0: 1}
    now = 0
    d = 1
    for i in range(n):
        now = (now + int(s[i]) * d) % p
        res += cnt.get(now, 0)
        d = (d * 10) % p
        cnt[now] = cnt.get(now, 0) + 1

print(res)

Problem: p02757
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
N, P = map(int, input().split())

S = input()

mods = []
d = 1
for s in reversed(S):
	mods.append(int(s) * d % P)
	d *= 10

counts = [0] * P
counts[0] = 1
h = 0
total = 0
for i, m in enumerate(mods):
	h += m
	total += counts[h % P]

	counts[h % P] += 1

print(total)


Failing test case input:
20 2
83069831581883249798

Expected output:
100


Actual output:
210


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

S = input()

total = 0
if P == 2 or P == 5:
	for i, s in enumerate(S):
		if int(s) % P == 0:
			total += i + 1
else:
	counts = [0] * P
	counts[0] = 1
	h = 0
	
	d = 1
	for s in reversed(S):
		m = int(s) * d % P
		h = (h + m) % P
		total += counts[h]
		counts[h] += 1
		d = (d * 10) % P



print(total)
Problem: p03007
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
N=int(input())
listA=list(map(int,input().split()))
listB=[abs(i) for i in listA]
MAX=max(listA)
MIN=min(listA)
ans=sum(listB)
if MIN<=0:
  print(ans)
else:
  print(ans-MIN)
listC=sorted(listA)  
for i in range(len(listA)-2):
  if listC[i+1]>=0:
    print(MIN,listC[i+1])
    MIN=MIN-listC[i+1]
  else:
    print(MAX,listC[i+1])
    MAX=MAX-listC[i+1]
print(MAX,MIN)

Failing test case input:
3
31 4 3

Expected output:
32
3 4
31 -1


Actual output:
35
3 4
31 -1


Fixed submission (verdict: Accepted):
N=int(input())
listA=list(map(int,input().split()))
listB=[abs(i) for i in listA]
MAX=max(listA)
MIN=min(listA)
ans=sum(listB)
if MAX<=0:
  print(ans+MAX*2)
elif MIN<=0:
  print(ans)   
else:
  print(ans-MIN*2)
listC=sorted(listA)  
for i in range(len(listA)-2):
  if listC[i+1]>=0:
    print(MIN,listC[i+1])
    MIN=MIN-listC[i+1]
  else:
    print(MAX,listC[i+1])
    MAX=MAX-listC[i+1]
print(MAX,MIN)

Problem: p03007
Buggy submission (PyPy3 (2.4.0), verdict: Wrong Answer):
n=int(input())
A=sorted(list(map(int,input().split())))

XY=[]
left,right=A[0],A[-1]
for i in range(1,n-1):
    if i<0:
        XY.append([right,i])
        right -=i
    else:
        XY.append([left,i])
        left -=i
XY.append([right,left])
print(right-left)
for x,y in XY:print(x,y)

Failing test case input:
3
4 -4 6

Expected output:
14
-4 4
6 -8


Actual output:
11
-4 1
6 -5


Fixed submission (verdict: Accepted):
n=int(input())
A=sorted(list(map(int,input().split())))

XY=[]
left,right=A[0],A[-1]
for i in range(1,n-1):
    num=A[i]
    if num<0:
        XY.append([right,num])
        right -=num
    else:
        XY.append([left,num])
        left -=num
XY.append([right,left])
print(right-left)
for x,y in XY:print(x,y)

Problem: p03007
Buggy submission (Python (3.8.2), verdict: Wrong Answer):
def main():
    n = int(input())
    a = sorted(list(map(int,input().split())))
    ans = []
    while len(a)>2:
        p = a[0:2]
        a = a[2:]
        a.insert(0,p[0]-p[1])
        ans.append(p)
    print(a[1]-a[0])
    for i in range(len(ans)):
        print(ans[i][0],ans[i][1])
    print(a[1],a[0])

if __name__ == "__main__":
    main()


Failing test case input:
3
-1 3 -2

Expected output:
6
3 -1
4 -2


Actual output:
4
-2 -1
3 -1


Fixed submission (verdict: Accepted):
def main():
    n = int(input())
    a = sorted(list(map(int,input().split())))
    ans = []
    for i in range(1,n-1):
        if a[i]>0:
            ans.append([a[0],a[i]])
            a[0] -= a[i]
        else:
            ans.append([a[-1],a[i]])
            a[-1] -= a[i]
    ans.append([a[-1],a[0]])
    print(a[-1]-a[0])
    for x,y in ans:
        print(x,y)

if __name__ == "__main__":
    main()
Problem: p03007
Buggy submission (PyPy3 (2.4.0), verdict: Wrong Answer):
import bisect
n = int(input())
a = list(map(int, input().split()))

a.sort()
ope = []
max_num = a[-1]
min_num = a[0]
p = bisect.bisect_left(a, 0)
for i in range(1, p):
    ope.append([max_num, a[i]])
    max_num -= a[i]
for i in range(p, n - 1):
    ope.append([min_num, a[i]])
    min_num -= a[i]
ope.append([max_num, min_num])
print(max_num - min_num)
for i in range(n - 1):
    print(" ".join(map(str, ope[i])))

Failing test case input:
3
5 3 2

Expected output:
6
2 3
5 -1


Actual output:
8
2 2
0 3


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

a.sort()
ope = []
max_num = a[-1]
min_num = a[0]
p = bisect.bisect_left(a, 0)
for i in range(1, min(p, n - 1)):
    ope.append([max_num, a[i]])
    max_num -= a[i]
for i in range(max(p, 1), n - 1):
    ope.append([min_num, a[i]])
    min_num -= a[i]
ope.append([max_num, min_num])
print(max_num - min_num)
for i in range(n - 1):
    print(" ".join(map(str, ope[i])))

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

from collections import deque

n = int(input())
lis = list(map(int,input().split()))
lis.sort()
lis = deque(lis)
x = 0
ans_lis = []
while len(lis) != 1:
    mi = lis.popleft()
    ma = lis.pop()
    lis.append(mi - ma)
    ans_lis.append([mi,ma])
    x += 1
print(lis[0])
for x in ans_lis:
    print(x[0],x[1])

Failing test case input:
3
5 1 2

Expected output:
6
1 2
5 -1


Actual output:
6
1 5
2 -4


Fixed submission (verdict: Accepted):

n = int(input())
lis = list(map(int,input().split()))
lis.sort()
m = lis[0]
M = lis[-1]
ans_lis = []

for now in lis[1:-1]:
    if now > 0:
        ans_lis.append([m,now])
        m -= now
    else:
        ans_lis.append([M,now])
        M -= now
ans_lis.append([M,m])
print(M-m)
for x in ans_lis:
    print(x[0],x[1])

Problem: p03007
Buggy submission (Python (3.4.3), verdict: Wrong Answer):
N=int(input())
listA=list(map(int,input().split()))
listB=[abs(i) for i in listA]
MAX=max(listA)
MIN=min(listA)
ans=sum(listB)
if MAX<=0:
  print(ans+MAX)
elif MIN<=0:
  print(ans)  
else:
  print(ans-MIN)
listC=sorted(listA)  
for i in range(len(listA)-2):
  if listC[i+1]>=0:
    print(MIN,listC[i+1])
    MIN=MIN-listC[i+1]
  else:
    print(MAX,listC[i+1])
    MAX=MAX-listC[i+1]
print(MAX,MIN)

Failing test case input:
3
5 3 1

Expected output:
7
1 3
5 -2


Actual output:
8
1 3
5 -2


Fixed submission (verdict: Accepted):
N=int(input())
listA=list(map(int,input().split()))
listB=[abs(i) for i in listA]
MAX=max(listA)
MIN=min(listA)
ans=sum(listB)
if MAX<=0:
  print(ans+MAX*2)
elif MIN<=0:
  print(ans)   
else:
  print(ans-MIN*2)
listC=sorted(listA)  
for i in range(len(listA)-2):
  if listC[i+1]>=0:
    print(MIN,listC[i+1])
    MIN=MIN-listC[i+1]
  else:
    print(MAX,listC[i+1])
    MAX=MAX-listC[i+1]
print(MAX,MIN)