Harryxun/testdata huggingface dataset:
Buggy python submission (verdict: Time Limit Exceeded):
s = list(str(input()))
q = int(input())

vec = 0
for _ in range(q):
    X = input().split()
    if X[0] == '1':
        vec = (vec+1) % 2
    else:
        if X[1] == '1' and vec == 0:
            s.insert(0, X[2])
        elif X[1] == '2' and vec == 0:
            s.append(X[2])
        elif X[1] == '1' and vec == 1:
            s.append(X[2])
        elif X[1] == '2' and vec == 1:
            s.insert(0, X[2])

if vec == 1:
    s.reverse()

print(''.join(map(str, s)))


Fixed submission:
s = list(str(input()))
q = int(input())

from collections import deque
d = deque(s)
REVERSE = False

for _ in range(q):
    X = input().split()
    if X[0] == '1':
        REVERSE = not REVERSE
    else:
        if X[1] == '1' and not REVERSE:
            d.appendleft(X[2])
        elif X[1] == '2' and not REVERSE:
            d.append(X[2])
        elif X[1] == '1' and REVERSE:
            d.append(X[2])
        elif X[1] == '2' and REVERSE:
            d.appendleft(X[2])

if REVERSE:
    d.reverse()

print(*d, sep='')

Buggy python submission (verdict: Time Limit Exceeded):
s = list(str(input()))
q = int(input())

for _ in range(q):
    X = input().split()
    if X[0] == '1':
        s.reverse()
    else:
        if X[1] == '1':
            s.insert(0, X[2])
        else:
            s.append(X[2])

print(''.join(map(str, s)))


Fixed submission:
s = list(str(input()))
q = int(input())

from collections import deque
d = deque(s)
REVERSE = False

for _ in range(q):
    X = input().split()
    if X[0] == '1':
        REVERSE = not REVERSE
    else:
        if X[1] == '1' and not REVERSE:
            d.appendleft(X[2])
        elif X[1] == '2' and not REVERSE:
            d.append(X[2])
        elif X[1] == '1' and REVERSE:
            d.append(X[2])
        elif X[1] == '2' and REVERSE:
            d.appendleft(X[2])

if REVERSE:
    d.reverse()

print(*d, sep='')
Buggy python submission (verdict: Runtime Error):
def calc(x):
    return (x+1)/2

n, k = map(int, input().split())
p = list(map(calc,(map(int, input().split()))))

_ans = sum(p[0:k])
ans = _ans
for x in range(n-k+1):
    _ans = _ans - p[x] + p[k]
    ans = max(ans, _ans)

print(ans)


Fixed submission:
def calc(x):
    return (x+1)/2

n, k = map(int, input().split())
p = list(map(calc,(map(int, input().split()))))

_ans = sum(p[0:k])
ans = _ans
for x in range(n-k):
    # print(x, x + k)
    _ans = _ans - p[x] + p[x + k]
    ans = max(ans, _ans)

print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
def calc(x):
    return (x+1)/2

n, k = map(int, input().split())
p = list(map(calc,(map(int, input().split()))))

_ans = p[0:k]

ans = sum(_ans)

for x in range(k,n):
    _ans.pop(0)
    _ans.append(p[x])
    # print(_ans)
    ans = max(ans, sum(_ans))

print(ans)


Fixed submission:
def calc(x):
    return (x+1)/2

n, k = map(int, input().split())
p = list(map(calc,(map(int, input().split()))))

_ans = sum(p[0:k])
ans = _ans
for x in range(n-k):
    # print(x, x + k)
    _ans = _ans - p[x] + p[x + k]
    ans = max(ans, _ans)

print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
def calc(x):
    return (x+1)/2

n, k = map(int, input().split())
p = list(map(calc,(map(int, input().split()))))

_p = [sum(p[i:i+k]) for i in range(n-k+1)]

print(max(_p))


Fixed submission:
def calc(x):
    return (x+1)/2

n, k = map(int, input().split())
p = list(map(calc,(map(int, input().split()))))

_ans = sum(p[0:k])
ans = _ans
for x in range(n-k):
    # print(x, x + k)
    _ans = _ans - p[x] + p[x + k]
    ans = max(ans, _ans)

print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
def calc(x):
    return (x+1)/2

n, k = map(int, input().split())
p = list(map(calc,(map(int, input().split()))))

# print(p)

ans = 0
for i in range(n-k+1):
    _ans = sum(p[i:i+k])
    if _ans > ans:
        ans = _ans
print(ans)


Fixed submission:
def calc(x):
    return (x+1)/2

n, k = map(int, input().split())
p = list(map(calc,(map(int, input().split()))))

_ans = sum(p[0:k])
ans = _ans
for x in range(n-k):
    # print(x, x + k)
    _ans = _ans - p[x] + p[x + k]
    ans = max(ans, _ans)

print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
n, k = map(int, input().split())
p = list(map(int, input().split()))

_p = [(x+1)/2 for x in p]

ans = 0
for i in range(n-k+1):
    _ans = sum(_p[i:i+k])
    if _ans > ans:
        ans = _ans
print(ans)


Fixed submission:
def calc(x):
    return (x+1)/2

n, k = map(int, input().split())
p = list(map(calc,(map(int, input().split()))))

_ans = sum(p[0:k])
ans = _ans
for x in range(n-k):
    # print(x, x + k)
    _ans = _ans - p[x] + p[x + k]
    ans = max(ans, _ans)

print(ans)
Buggy python submission (verdict: Runtime Error):
a, b = map(int, input().split())

import math
def lcm(x, y):
    return (x * y) // math.gcd(x, y)

print(lcm(a, b))


Fixed submission:
a, b = map(int, input().split())

def gcd(a, b):
    while b:
        a, b = b, a % b
    return a

def lcm(x, y):
    return (x * y) // gcd(x, y)

print(lcm(a, b))

Buggy python submission (verdict: Runtime Error):
a, b = map(int, input().split())

import math
def lcm(x, y):
    return (x * y) // math.gcd(x, y)

print(lcm(a, b))


Fixed submission:
a, b = map(int, input().split())

def gcd(a, b):
    while b:
        a, b = b, a % b
    return a

def lcm(x, y):
    return (x * y) // gcd(x, y)

print(lcm(a, b))
Buggy python submission (verdict: Runtime Error):
x, y = map(int, input().split())

if (x+y) % 3 != 0:
    ans = 0

else:
    a = (x + y)//3
    b = x-a

    if b <0:
        ans = 0
    else:
        import math 
        val = math.factorial(a)//(math.factorial(b)*math.factorial(a-b))

        ans = val % (10**9+7)

print(int(ans))


Fixed submission:
x, y = map(int, input().split())

from operator import mul
from functools import reduce

def cmb(n, r, mod):
    if ( r<0 or r>n ):
        return 0
    r = min(r, n-r)
    return g1[n] * g2[r] * g2[n-r] % mod


mod = 10**9+7 #出力の制限
g1 = [1, 1] # 元テーブル
g2 = [1, 1] #逆元テーブル

if (x+y) % 3 != 0:
    ans = 0

else:
    a = (x + y)//3
    b = x-a

    if x-a <0 or y-a <0:
        ans = 0
    else:
        mod = 10**9+7 #出力の制限
        N = a+b
        g1 = [1, 1] # 元テーブル
        g2 = [1, 1] #逆元テーブル
        inverse = [0, 1] #逆元テーブル計算用テーブル

        for i in range( 2, N + 1 ):
            g1.append( ( g1[-1] * i ) % mod )
            inverse.append( ( -inverse[mod % i] * (mod//i) ) % mod )
            g2.append( (g2[-1] * inverse[-1]) % mod )

        ans = cmb(a, b, mod)

print(int(ans))
Buggy python submission (verdict: Runtime Error):
a, b = map(int, input().split())

# get GCD(Gratest Common Divisor: 最大公約数)
def gcd(a, b):
    while b:
        a, b = b, a % b
    return a

# get divisors(公約数)
def divisors(n):
    nums = []
    for i in range(1, int(n**0.5)+1):
        if n % i == 0:
            nums.append(i)
            if i != n // i:
                nums.append(n//i)
    return nums.sort()

# is prime(Prime Number: 素数)
def is_prime(n):
    if n == 1: return False
    for k in range(2, int(n**0.5) + 1):
        if n % k == 0: return False
    return True

x = divisors(gcd(a, b))

ans = 1
for _x in x:
    ans += is_prime(_x)

print(ans)


Fixed submission:
a, b = map(int, input().split())

def gcd(a, b):
    while b:
        a, b = b, a % b
    return a

agcd = gcd(a,b)

def make_divisors(n):
    divisors = []
    for i in range(1, int(n**0.5)+1):
        if n % i == 0:
            divisors.append(i)
            if i != n // i:
                divisors.append(n//i)

    divisors.sort()
    return divisors

def is_prime(n):
    if n == 1: return False

    for k in range(2, int(n**0.5) + 1):
        if n % k == 0:
            return False

    return True

x = make_divisors(agcd)
ans = 1
for _x in x:
    ans += is_prime(_x)

print(ans)
Buggy python submission (verdict: Runtime Error):
n, m = map(int, input().split())
A = list(map(int, input().split()))
A = sorted(A)

from bisect import insort_left

for _ in range(m):
    x = A.pop()//2
    insort_left(A, x)

print(sum(A))


Fixed submission:
S = input()
if S=='Sunny':
    print('Cloudy')
elif S=='Cloudy':
    print('Rainy')
else:
    print('Sunny')
Buggy python submission (verdict: Time Limit Exceeded):
n, m = map(int, input().split())
A = list(map(int, input().split()))
A = sorted(A)

from bisect import insort_left

for _ in range(m):
    x = A.pop()//2
    insort_left(A, x)

print(sum(A))


Fixed submission:
n, m = map(int, input().split())
A = list(map(int, input().split()))
A = map(lambda x: x * (-1), A)
A = sorted(A)

from heapq import heappush, heappop

for _ in range(m):
    x = heappop(A)
    heappush(A, (-1*x)//2*(-1))

print(sum(A)*(-1))

Buggy python submission (verdict: Time Limit Exceeded):
n, m = map(int, input().split())
A = list(map(int, input().split()))
A = sorted(A)

from bisect import insort

for _ in range(m):
    x = A.pop()//2
    insort(A, x)

print(sum(A))


Fixed submission:
n, m = map(int, input().split())
A = list(map(int, input().split()))
A = map(lambda x: x * (-1), A)
A = sorted(A)

from heapq import heappush, heappop

for _ in range(m):
    x = heappop(A)
    heappush(A, (-1*x)//2*(-1))

print(sum(A)*(-1))

Buggy python submission (verdict: Time Limit Exceeded):
n, m = map(int, input().split())
A = list(map(int, input().split()))
A = sorted(A)

from bisect import bisect_left

for _ in range(m):
    x = A.pop()
    x = x//2
    A.insert(bisect_left(A, x), x)

print(sum(A))


Fixed submission:
n, m = map(int, input().split())
A = list(map(int, input().split()))
A = map(lambda x: x * (-1), A)
A = sorted(A)

from heapq import heappush, heappop

for _ in range(m):
    x = heappop(A)
    heappush(A, (-1*x)//2*(-1))

print(sum(A)*(-1))
Buggy python submission (verdict: Runtime Error):
n = int(input())

s = []
for _ in range(n):
    s += [sorted(input())]

import collections

c = collections.Counter(s)

ans = 0

for x in c.values():
    ans += x*(x-1)//2

print(ans)


Fixed submission:
n = int(input())

s = []
for _ in range(n):
    s += [''.join(map(str, (sorted(list(str(input()))))))]

import collections

c = collections.Counter(s)

from math import factorial

ans = 0

for x in c.values():
    if x >= 2:
        ans += factorial(x)//(factorial(x-2)*2)

print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
s = list(str(input()))
n = len(s)

from itertools import product

ans = 0

m = s.count('?')

for x in product([0,1,2,3,4,5,6,7,8,9], repeat=m):
    p_x = 0
    s_num = 0
    for i in range(n):
        if s[i] == '?':
            s_num += int(x[p_x])*(10**(n-1-i))
            p_x += 1
        else:
            s_num += int(s[i])*(10**(n-1-i))
    if s_num % 13 == 5:
        ans += 1

print(ans%(10**9+7))


Fixed submission:
s = list(str(input()))

n = len(s)
dp = [0] * 13
dp[0] = 1

for i in range(n):
    prev = dp[:]
    dp = [0]*13

    if s[i] == '?':
        for z in range(10):
            for j in range(13):
                dp[(j*10+z)%13] += prev[j]
                dp[(j*10+z)%13] %= 10**9+7

    else:
       _s = int(s[i])
       for j in range(13):
            dp[(j*10+_s)%13] += prev[j]
            dp[(j*10+_s)%13] %= 10**9+7

print(dp[5])
Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
a = []
for _ in range(n):
    a.append(int(input()))

for i in range(n):
    _a = a.copy()
    _a.pop(i)
    print(max(_a))


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

m = max(a)
idxs = [i for i, v in enumerate(a) if v == m]

if len(idxs) > 1:
    for i in range(n):
        print(m)

if len(idxs) == 1:
    idx = idxs[0]
    for i in range(n):
        if idx == i:
            _a = a.copy()
            _a.pop(i)
            print(max(_a))
        else:
            print(m)

Buggy python submission (verdict: Runtime Error):
n = int(input())
a = []
for _ in range(n):
    a.append(int(input()))

for i in range(n):
    _a = a.copy()
    _a.pop(i)
    print(max(_a))


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

m = max(a)
idxs = [i for i, v in enumerate(a) if v == m]

if len(idxs) > 1:
    for i in range(n):
        print(m)

if len(idxs) == 1:
    idx = idxs[0]
    for i in range(n):
        if idx == i:
            _a = a.copy()
            _a.pop(i)
            print(max(_a))
        else:
            print(m)
Buggy python submission (verdict: Runtime Error):
n,d = map(int, input().split())
X = []
for _ in range(n):
    _x = list(map(int, input().split()))
    X.append(_x)

from itertools import combinations
import numpy as np

ans = 0
for (x1, x2) in combinations(X, 2):
    a = 0
    for i in range(d):
        a += (x1[i]-x2[i])**2 
    a = np.sqrt(a)
    if a.is_integer():
        ans += 1

print(ans)


Fixed submission:
n,d = map(int, input().split())
X = []
for _ in range(n):
    _x = list(map(int, input().split()))
    X.append(_x)

from itertools import combinations
import numpy as np

ans = 0
for (x1, x2) in combinations(X, 2):
    a = 0
    for i in range(d):
        a += (x1[i]-x2[i])**2 
    a = np.sqrt(a)
    if a.is_integer():
        ans += 1

print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
l, r = map(int, input().split())

from itertools import combinations

ans = [(x1*x2)%2019 for (x1, x2) in combinations(range(l, r+1), 2)]

print(min(ans))


Fixed submission:
l, r = map(int, input().split())

from itertools import combinations

if r - l > 2019:
    r = l+2019

ans = [(x1*x2)%2019 for (x1, x2) in combinations(range(l, r+1), 2)]

print(min(ans))

Buggy python submission (verdict: Time Limit Exceeded):
l, r = map(int, input().split())

from itertools import combinations

ans = 2019

for (x1, x2) in combinations(range(l, r+1), 2):
    ans = min((x1 * x2) % 2019, ans)

print(ans)


Fixed submission:
l, r = map(int, input().split())

from itertools import combinations

if r - l > 2019:
    r = l+2019

ans = [(x1*x2)%2019 for (x1, x2) in combinations(range(l, r+1), 2)]

print(min(ans))

Buggy python submission (verdict: Time Limit Exceeded):
l, r = map(int, input().split())

from itertools import combinations

ans = 2019

for (x1, x2) in combinations(range(l, r+1), 2):
    a = (x1 * x2) % 2019
    if a < ans:
        ans = a

print(ans)


Fixed submission:
l, r = map(int, input().split())

from itertools import combinations

if r - l > 2019:
    r = l+2019

ans = [(x1*x2)%2019 for (x1, x2) in combinations(range(l, r+1), 2)]

print(min(ans))

Buggy python submission (verdict: Runtime Error):
l, r = map(int, input().split())

from itertools import combinations

ans = 2019

for (x1, x2) in combinations(range(l, r+1), 2):
    a = (x1 * x2) % 2019
    if a < ans:
        ans = a

print(ans)


Fixed submission:
l, r = map(int, input().split())

from itertools import combinations

if r - l > 2019:
    r = l+2019

ans = [(x1*x2)%2019 for (x1, x2) in combinations(range(l, r+1), 2)]

print(min(ans))
Buggy python submission (verdict: Runtime Error):
n = int(input())
a = list(map(int, input().split()))

from sympy import Indexed, IndexedBase, Tuple, sqrt, solve

A = IndexedBase('A')
eqs = Tuple()
for i in range(n-1):
    eqs += Tuple(A[i]+A[i+1]-2*a[i])
eqs += Tuple(A[n-1]+A[0]-2*a[n-1])

ans = solve(eqs, eqs.atoms(Indexed))

ans = [x for x in ans.values()]

print(' '.join(map(str, ans)))


Fixed submission:
n = int(input())
a = list(map(int, input().split()))

ans = []
ans += [sum(a) - 2*sum(a[1::2])]
for i in range(0, n-1):
    ans += [2*a[i]-ans[i]]

print(' '.join(map(str, ans)))

Buggy python submission (verdict: Runtime Error):
n = int(input())
a = list(map(int, input().split()))

from sympy import Indexed, IndexedBase, Tuple, sqrt, solve

A = IndexedBase('A')
eqs = Tuple()
for i in range(n-1):
    eqs += Tuple(A[i]+A[i+1]-2*a[i])
eqs += Tuple(A[n-1]+A[0]-2*a[n-1])

ans = solve(eqs, eqs.atoms(Indexed))

ans = [x for x in ans.values()]

print(' '.join(map(str, ans)))


Fixed submission:
n = int(input())
a = list(map(int, input().split()))

ans = []
ans += [sum(a) - 2*sum(a[1::2])]
for i in range(0, n-1):
    ans += [2*a[i]-ans[i]]

print(' '.join(map(str, ans)))
Buggy python submission (verdict: Time Limit Exceeded):
n, k = map(int, input().split())

from itertools import combinations

ans = []

for i in range(1,k+1):
    print(len(list(combinations(range(n-k+1),i)))*len(list(combinations(range(k-1),i-1)))%(10**9+7))


Fixed submission:
n, k = map(int, input().split())

from math import factorial
# math.factorial(n) nの階乗

def combination(n, k):
    return factorial(n) // factorial(k) // factorial(n - k)

for i in range(1, k+1):
    if i <= n-k+1:
        print(combination(n-k+1, i)*combination(k-1,i-1)%(10**9+7))
    else:
        print(0)
Buggy python submission (verdict: Runtime Error):
a, b, c, d = map(int, input().split())

# ans = b-(a-1) - (b//c - (a-1)//c) - (b//d - (a-1)//d) + (b//(c*d) - (a-1)//(c*d))
ans = b - b//c - b//d + b//(c*d) - (a-1 - (a-1)//c - (a-1)//d + (a-1)//(c*d))

print(ans)


Fixed submission:
s = str(input())
_s = s[0]
ans = 'Good'

for x in s[1:]:
    if x == _s:
        ans = 'Bad'
        break
    _s = x

print(ans)
Buggy python submission (verdict: Runtime Error):
x, a = map(int, input())
if x < a:
    print(0)
else:
    print(10)


Fixed submission:
x, a = map(int, input().split())
if x < a:
    print(0)
else:
    print(10)
Buggy python submission (verdict: Runtime Error):
n, a, b, c, d = map(int,input().split())
s = str(input())
s = s[a:max(c,d)]

l_s = []
for i in range(len(s)):
    l_s += [0 if s[i] == '.' else 1]

ans = 'Yes'
if i in range(len(s)-1):
    if sum(l_s[i:i+2])==2:
        ans = 'No'

if ans == 'Yes':
    if d>c:
        pass
    else:
        ans = 'No'
        for j in range(b-a,d-1-a):
            if sum(l_s[j:3+j]) == 0:
                ans = 'Yes'
            else:
                pass
        if ans == 'No':
            if sum(l_s[d-1-a,d-a])==0:
                ans = 'Yes'

print(ans)


Fixed submission:
n, a, b, c, d = map(int,input().split())
s = str(input())

ans = 'Yes'
if '##' in s[a-1:c] or '##' in s[b-1:d]:
    ans = 'No'

if ans == 'Yes':
    if c<d:
        pass
    else:
        if '...' in s[b-2:d+1]:
            pass
        else:
            ans = 'No'

print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
n, m = map(int, input().split())
l, r = map(int, input().split())
ans = [x for x in range(l, r+1)]
if m > 1:
    for i in range(m-1):
        l, r = map(int, input().split())
        ans = [x for x in range(l, r+1) if x in ans]

print(len(ans))


Fixed submission:
n, m = map(int, input().split())
l, r = map(int, input().split())
ans = -1
if m > 1:
    for i in range(m-1):
        _l, _r = map(int, input().split())
        if (l < _l <= r):
            l = _l
        if (l <= _r < r):
            r = _r
        if _r < l or r < _l:
            ans = 0
            print(ans)
            break

if ans == -1:
    print(r-l+1)

Buggy python submission (verdict: Time Limit Exceeded):
n, m = map(int, input().split())
ans = []
if m == 1:
    l, r = map(int, input().split())
    print(r-l+1)
else:
    for i in range(m):
        l, r = map(int, input().split())
        ans += [x for x in range(l, r+1)]

    ans = [x for x in set(ans) if ans.count(x) >= m]
    print(len(ans))


Fixed submission:
n, m = map(int, input().split())
l, r = map(int, input().split())
ans = -1
if m > 1:
    for i in range(m-1):
        _l, _r = map(int, input().split())
        if (l < _l <= r):
            l = _l
        if (l <= _r < r):
            r = _r
        if _r < l or r < _l:
            ans = 0
            print(ans)
            break

if ans == -1:
    print(r-l+1)
Buggy python submission (verdict: Time Limit Exceeded):
n, m = map(int, input().split())
a = list(map(int, input().split()))

for _ in range(m):
    b, c = map(int, input().split())
    a += [c]*b
a = sorted(a)

print(sum(a[-n:]))


Fixed submission:
n, m = map(int, input().split())
a = [(1, int(x)) for x in input().split()]

for _ in range(m):
    b, c = map(int, input().split())
    a += [(b, c)]

from operator import itemgetter
a = sorted(a, key=itemgetter(1))

ans = 0
t = 0
while n-t > 0:
    (k, x) = a.pop()
    ans += x * min(k, n-t)
    t += k

print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
n, m = map(int, input().split())
a = list(map(int, input().split()))

for _ in range(m):
    b, c = map(int, input().split())
    a += [c]*b
a.sort()

print(sum(a[-n:]))


Fixed submission:
n, m = map(int, input().split())
a = [(1, int(x)) for x in input().split()]

for _ in range(m):
    b, c = map(int, input().split())
    a += [(b, c)]

from operator import itemgetter
a = sorted(a, key=itemgetter(1))

ans = 0
t = 0
while n-t > 0:
    (k, x) = a.pop()
    ans += x * min(k, n-t)
    t += k

print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
n, m = map(int, input().split())
a = list(map(int, input().split()))
a.sort()

a2 = []
for _ in range(m):
    b, c = map(int, input().split())
    a2 += [c]*b
a2.sort()

for i in range(min(n, len(a2))):
    if a[i] < a2[-1-i]:
        a[i] = a2[-1-i]
    else:
        break

print(sum(a))


Fixed submission:
n, m = map(int, input().split())
a = [(1, int(x)) for x in input().split()]

for _ in range(m):
    b, c = map(int, input().split())
    a += [(b, c)]

from operator import itemgetter
a = sorted(a, key=itemgetter(1))

ans = 0
t = 0
while n-t > 0:
    (k, x) = a.pop()
    ans += x * min(k, n-t)
    t += k

print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
n, m = map(int, input().split())
a = list(map(int, input().split()))
a.sort()

a2 = []
for _ in range(m):
    b, c = map(int, input().split())
    a2 += [c for _ in range(b)]
a2.sort()

for i in range(min(n, len(a2))):
    if a[i] < a2[-1-i]:
        a[i] = a2[-1-i]
    else:
        break

print(sum(a))


Fixed submission:
n, m = map(int, input().split())
a = [(1, int(x)) for x in input().split()]

for _ in range(m):
    b, c = map(int, input().split())
    a += [(b, c)]

from operator import itemgetter
a = sorted(a, key=itemgetter(1))

ans = 0
t = 0
while n-t > 0:
    (k, x) = a.pop()
    ans += x * min(k, n-t)
    t += k

print(ans)
Buggy python submission (verdict: Time Limit Exceeded):
r, g, b, n = map(int, input().split())
l = [r, g, b]
l.sort()

N1 = n // l[2]
N2 = n // l[1]
N3 = n // l[0]

cnt = 0

for i in range(N1+1):
    for j in range(N2+1):
        for k in range(N3+1):
            if i*l[2] + j*l[1]+ k*l[0]==n:
                cnt += 1

print(cnt)


Fixed submission:
r, g, b, n = map(int, input().split())
l = [r, g, b]
l.sort()

N1 = n // l[2]
N2 = n // l[1]
N3 = n // l[0]

cnt = 0

for i in range(N1+1):
    N2 = n-i*l[2] // l[1]
    for j in range(N2+1):
        if ((n-i*l[2]-j*l[1]) % l[0] == 0) and ((n-i*l[2]-j*l[1]) // l[0] >= 0):
            cnt += 1
            # print(i, j, (n-i*l[2]-j*l[1]) // l[0])
print(cnt)