Problem p02821 (python)

Fixed submission:
import sys
import bisect
import itertools


def solve():
    readline = sys.stdin.buffer.readline
    mod = 10 ** 9 + 7
    n, m = list(map(int, readline().split()))
    a = list(map(int, readline().split()))
    a.sort()
    cor_v = a[0] * 2
    inc_v = a[-1] * 2 + 1
    while - cor_v + inc_v > 1:
        bin_v = (cor_v + inc_v) // 2
        cost = 0
        #条件を満たすcostを全検索
        for i in range(n-1, -1, -1):
            cost += n - bisect.bisect_left(a, bin_v - a[i])
        #costが制約を満たすか
        if cost >= m:
            cor_v = bin_v
        else:
            inc_v = bin_v
    acm = list(itertools.accumulate([0] + a))
    t = 0
    c = 0
    for i in range(n):
        p = bisect.bisect_left(a, cor_v - a[i])
        t += acm[-1] - acm[p] + a[i] * (n - p)
        c += n - p
    print(t - (c - m) * cor_v)


if __name__ == '__main__':
    solve()

Buggy submission (verdict: Runtime Error):
import sys
import bisect
import itertools


def solve():
    readline = sys.stdin.buffer.readline
    mod = 10 ** 9 + 7
    n, m = list(map(int, readline().split()))
    a = list(map(int, readline().split()))
    a.sort()
    cor_v = a[0] * 2
    inc_v = a[-1] * 2 + 1
    while - cor_v + inc_v > 1:
        bin_v = (cor_v + inc_v) // 2
        cost = 0
        #条件を満たすcostを全検索
        for i in range(n-1, -1, -1):
            cost += n - bisect.bisect_left(a, bin_v - a[i])
            if cost >= m:
              	cost += 10 ** 100
              	cost -= 10 ** 100
                break
        #costが制約を満たすか
        if cost >= m:
            cor_v = bin_v
        else:
            inc_v = bin_v
    acm = list(itertools.accumulate([0] + a))
    t = 0
    c = 0
    for i in range(n):
        p = bisect.bisect_left(a, cor_v - a[i])
        t += acm[-1] - acm[p] + a[i] * (n - p)
        c += n - p
    print(t - (c - m) * cor_v)


if __name__ == '__main__':
    solve()

Buggy submission (verdict: Runtime Error):
import sys
import bisect


def solve():
    readline = sys.stdin.buffer.readline
    mod = 10 ** 9 + 7
    n, m = list(map(int, readline().split()))
    a = list(map(int, readline().split()))
    a.sort()
    t = 0
    c = 0
    s = n - 1
    for i in range(a[-1] * 2, a[1] - 1, -1):
        for j in range(s, -1, -1):
            if a[j] * 2 == i:
                c += 1
                t += i
            pl = bisect.bisect_left(a, i - a[j], 0, j)
            pr = bisect.bisect_right(a, i - a[j], 0, j)
            if pl == n:
                break
            elif pr == 0 and j != 0:
                s -= 1
            if pl != pr:
                c += 2 ** (pr - pl)
                t += 2 ** (pr - pl) * i
            if c >= m:
                print(t - (c - m) * i)
                exit()


if __name__ == '__main__':
    solve()

Buggy submission (verdict: Runtime Error):
import sys
import bisect
import itertools


def solve():
    readline = sys.stdin.buffer.readline
    mod = 10 ** 9 + 7
    n, m = list(map(int, readline().split()))
    a = list(map(int, readline().split()))
    a.sort()
    t = 0
    c = 0
    for i in range(a[-1] * 2, a[1] - 1, -1):
        for j in range(n - 1, -1, -1):
            pl = bisect.bisect_left(a, i - a[j])
            pr = bisect.bisect_right(a, i - a[j])
            if pl != pr:
                if pr - pl == 1:
                    c += (pr - pl)
                    t += (pr - pl) * i
                else:
                    c += 2 ** (pr - pl - 1)
                    t += 2 ** (pr - pl - 1) * i
            if c > m:
                t -= (c - m) * i
                print(t)
                exit()


if __name__ == '__main__':
    solve()

Buggy submission (verdict: Runtime Error):
import sys
import bisect
import itertools


def solve():
    readline = sys.stdin.buffer.readline
    mod = 10 ** 9 + 7
    n, m = list(map(int, readline().split()))
    a = list(map(int, readline().split()))
    a.sort()
    acm = list(itertools.accumulate(a))
    t = 0
    c = 0
    for i in range(a[-1] * 2, a[1] - 1, -1):
        for j in range(n - 1, -1, -1):
            pl = bisect.bisect_left(a, i - a[j])
            pr = bisect.bisect_right(a, i - a[j])
            if pl != pr:
                c += (pr - pl)
                t += (pr - pl) * i
            if c > m:
                t -= (c - m) * i
                print(t)
                exit()


if __name__ == '__main__':
    solve()