Problem:


Score : 400 points

Problem Statement

There are N one-off jobs available. If you take the i-th job and complete it, you will earn the reward of B_i after A_i days from the day you do it.

You can take and complete at most one of these jobs in a day.

However, you cannot retake a job that you have already done.

Find the maximum total reward that you can earn no later than M days from today.

You can already start working today.

Constraints

All values in input are integers.

1 \leq N \leq 10^5

1 \leq M \leq 10^5

1 \leq A_i \leq 10^5

1 \leq B_i \leq 10^4

Input

Input is given from Standard Input in the following format:

N M
A_1 B_1
A_2 B_2
\vdots
A_N B_N

Output

Print the maximum total reward that you can earn no later than M days from today.

Sample Input 1

3 4
4 3
4 1
2 2

Sample Output 1

5

You can earn the total reward of 5 by taking the jobs as follows:

Take and complete the first job today. You will earn the reward of 3 after four days from today.

Take and complete the third job tomorrow. You will earn the reward of 2 after two days from tomorrow, that is, after three days from today.

Sample Input 2

5 3
1 2
1 3
1 4
2 1
2 3

Sample Output 2

10

Sample Input 3

1 1
2 1

Sample Output 3

0



Sample input:
3 4
4 3
4 1
2 2


Sample output:
5

Submission status: Wrong Answer

Solution code:
#!usr/bin/env python3
from collections import defaultdict,deque
from heapq import heappush, heappop
import sys
import math
import bisect
import random
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def LS():return [list(x) for x in sys.stdin.readline().split()]
def S():
    res = list(sys.stdin.readline())
    if res[-1] == "\n":
        return res[:-1]
    return res

def IR(n):
    return [I() for i in range(n)]
def LIR(n):
    return [LI() for i in range(n)]
def SR(n):
    return [S() for i in range(n)]
def LSR(n):
    return [LS() for i in range(n)]

sys.setrecursionlimit(1000000)
mod = 1000000007

#A
def A():
    a,b = LI()
    print(max(a+b,a-b,a*b))
    return

#B
def B():
    k,x = LI()
    ans = []
    for i in range(x-k+1,x+k):
        ans.append(i)
    print(*ans)
    return

#C
def C():
    def f(s):
        res = [0]*m
        for i in s:
            res[alp.index(i)] += 1
        return res
    alp = list("abcdefghijklmnopqrstuvwxyz")
    m = len(alp)
    n = I()
    s = [input() for i in range(n)]
    d = defaultdict(lambda : 0)
    for i in s:
        d[tuple(f(i))] += 1
    ans = 0
    for i in d.values():
        ans += (i*(i-1))//2
    print(ans)
    return

#D
def D():
    n,m = LI()
    g = LIR(n)
    g.sort(key = lambda x:x[0])
    ans = 0
    f = [1]*n
    j = 0
    q = []
    for i in range(m)[::-1]:
        while j < n and m-g[j][0] >= i:
            a,b = g[j]
            heappush(q,(-b,a))
            j += 1
        if q:
            x,a = heappop(q)
            if i+a <= m:
                ans -= x
    print(ans)
    return

def D_():
    def add(i):
        while i <= m+1:
            bit[i] += 1
            i += i&-i

    def sum(i):
        res = 0
        while i > 0:
            res += bit[i]
            i -= i&-i
        return res

    def root(x):
        if x == par[x]:
            return x
        par[x] = root(par[x])
        return par[x]

    def unite(x,y):
        x = root(x)
        y = root(y)
        if rank[x] < rank[y]:
            par[x] = y
        else:
            par[y] = x
            if rank[x] == rank[y]:
                rank[x] += 1

    n,m = LI()
    g = LIR(n)
    g.sort(key = lambda x:-x[1])
    bit = [0]*(m+2)
    par = [i for i in range(m+2)]
    rank = [0]*(m+2)
    ans = 0
    for a,b in g:
        i = m-a+1
        if i <= 0:
            continue
        if sum(i) < i:
            ans += b
            if sum(i-1) == sum(i):
                add(i)
            else:
                l = 1
                r = i
                while r-l > 1:
                    mid = (l+r)//2
                    if root(mid) == root(i):
                        r = mid
                    else:
                        l = mid
                unite(l,r)
                add(l)
    print(ans)
    return

#E
def E():
    def dfs(x,f):
        for y,w in v[x]:
            if f[y]:
                if dp[y] < dp[x]+w:
                    f[y] = 0
                    dp[y] = dp[x]+w
                    dfs(y,f)
                    f[y] = 1
            else:
                if dp[y] < dp[x]+w:
                    dp[y] = float("inf")
                    dfs(y,f)
    n,m,p = LI()
    v = [[] for i in range(n)]
    for i in range(m):
        a,b,c = LI()
        a -= 1
        b -= 1
        v[a].append((b,c-p))
    f = [1]*n
    dp = [-float("inf")]*n
    dp[0] = 0
    f[0] = 0
    dfs(0,f)
    ans = dp[n-1]
    if ans == float("inf") or ans == -float("inf"):
        print(-1)
    elif ans < 0:
        print(0)
    else:
        print(ans)
    return

#F
def F():
    def comb(a,b):
        return fact[a]*inv[b]*inv[a-b]%p

    p = I()
    a = LI()
    fact = [1]
    for i in range(p-1):
        fact.append(fact[-1]*(i+1)%p)
    inv = [1]*p
    inv[p-1] = pow(fact[p-1],p-2,p)
    for i in range(1,p)[::-1]:
        inv[i-1] = inv[i]*i%p
    b = [0]*p
    for i in range(p):
        ai = a[i]
        if ai:
            b[0] += ai
            k = 1
            for j in range(p):
                c = ai*comb(p-1,j)*k%p
                k *= i
                if k >= p:
                    k %= p
                if j%2:
                    b[p-1-j] += c
                else:
                    b[p-1-j] -= c
                b[p-1-j] %= p
    print(*b)
    return

#Solve
if __name__ == "__main__":
    D_()

Submission status: Wrong Answer

Solution code:
#!usr/bin/env python3
from collections import defaultdict,deque
from heapq import heappush, heappop
import sys
import math
import bisect
import random
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def LS():return [list(x) for x in sys.stdin.readline().split()]
def S():
    res = list(sys.stdin.readline())
    if res[-1] == "\n":
        return res[:-1]
    return res

def IR(n):
    return [I() for i in range(n)]
def LIR(n):
    return [LI() for i in range(n)]
def SR(n):
    return [S() for i in range(n)]
def LSR(n):
    return [LS() for i in range(n)]

sys.setrecursionlimit(1000000)
mod = 1000000007

#A
def A():
    a,b = LI()
    print(max(a+b,a-b,a*b))
    return

#B
def B():
    k,x = LI()
    ans = []
    for i in range(x-k+1,x+k):
        ans.append(i)
    print(*ans)
    return

#C
def C():
    def f(s):
        res = [0]*m
        for i in s:
            res[alp.index(i)] += 1
        return res
    alp = list("abcdefghijklmnopqrstuvwxyz")
    m = len(alp)
    n = I()
    s = [input() for i in range(n)]
    d = defaultdict(lambda : 0)
    for i in s:
        d[tuple(f(i))] += 1
    ans = 0
    for i in d.values():
        ans += (i*(i-1))//2
    print(ans)
    return

#D
def D():
    n,m = LI()
    g = LIR(n)
    g.sort(key = lambda x:x[0])
    ans = 0
    f = [1]*n
    j = 0
    q = []
    for i in range(m)[::-1]:
        while j < n and m-g[j][0] >= i:
            a,b = g[j]
            heappush(q,(-b,a))
            j += 1
        if q:
            x,a = heappop(q)
            if i+a <= m:
                ans -= x
    print(ans)
    return

def D_():
    def add(i):
        while i <= m+1:
            bit[i] += 1
            i += i&-i

    def sum(i):
        res = 0
        while i > 0:
            res += bit[i]
            i -= i&-i
        return res

    def root(x):
        if x == par[x]:
            return x
        par[x] = root(x)
        return par[x]

    def unite(x,y):
        x = root(x)
        y = root(y)
        if rank[x] < rank[y]:
            par[x] = y
        else:
            par[y] = x
            if rank[x] == rank[y]:
                rank[x] += 1

    n,m = LI()
    g = LIR(n)
    g.sort(key = lambda x:-x[1])
    bit = [0]*(m+2)
    par = [i for i in range(m+2)]
    rank = [0]*(m+2)
    ans = 0
    for a,b in g:
        i = m-a+1
        if i <= 0:
            continue
        if sum(i) < i:
            ans += b
            if sum(i-1) == sum(i):
                add(i)
            else:
                l = 1
                r = i
                while r-l > 1:
                    mid = (l+r)//2
                    if root(mid) == root(i):
                        r = mid
                    else:
                        l = mid
                unite(l,r)
                add(l)
    print(ans)
    return

#E
def E():
    def dfs(x,f):
        for y,w in v[x]:
            if f[y]:
                if dp[y] < dp[x]+w:
                    f[y] = 0
                    dp[y] = dp[x]+w
                    dfs(y,f)
                    f[y] = 1
            else:
                if dp[y] < dp[x]+w:
                    dp[y] = float("inf")
                    dfs(y,f)
    n,m,p = LI()
    v = [[] for i in range(n)]
    for i in range(m):
        a,b,c = LI()
        a -= 1
        b -= 1
        v[a].append((b,c-p))
    f = [1]*n
    dp = [-float("inf")]*n
    dp[0] = 0
    f[0] = 0
    dfs(0,f)
    ans = dp[n-1]
    if ans == float("inf") or ans == -float("inf"):
        print(-1)
    elif ans < 0:
        print(0)
    else:
        print(ans)
    return

#F
def F():
    def comb(a,b):
        return fact[a]*inv[b]*inv[a-b]%p

    p = I()
    a = LI()
    fact = [1]
    for i in range(p-1):
        fact.append(fact[-1]*(i+1)%p)
    inv = [1]*p
    inv[p-1] = pow(fact[p-1],p-2,p)
    for i in range(1,p)[::-1]:
        inv[i-1] = inv[i]*i%p
    b = [0]*p
    for i in range(p):
        ai = a[i]
        if ai:
            b[0] += ai
            k = 1
            for j in range(p):
                c = ai*comb(p-1,j)*k%p
                k *= i
                if k >= p:
                    k %= p
                if j%2:
                    b[p-1-j] += c
                else:
                    b[p-1-j] -= c
                b[p-1-j] %= p
    print(*b)
    return

#Solve
if __name__ == "__main__":
    D_()

Submission status: Wrong Answer

Solution code:
#!usr/bin/env python3
from collections import defaultdict,deque
from heapq import heappush, heappop
import sys
import math
import bisect
import random
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def LS():return [list(x) for x in sys.stdin.readline().split()]
def S():
    res = list(sys.stdin.readline())
    if res[-1] == "\n":
        return res[:-1]
    return res

def IR(n):
    return [I() for i in range(n)]
def LIR(n):
    return [LI() for i in range(n)]
def SR(n):
    return [S() for i in range(n)]
def LSR(n):
    return [LS() for i in range(n)]

sys.setrecursionlimit(1000000)
mod = 1000000007

#A
def A():
    a,b = LI()
    print(max(a+b,a-b,a*b))
    return

#B
def B():
    k,x = LI()
    ans = []
    for i in range(x-k+1,x+k):
        ans.append(i)
    print(*ans)
    return

#C
def C():
    def f(s):
        res = [0]*m
        for i in s:
            res[alp.index(i)] += 1
        return res
    alp = list("abcdefghijklmnopqrstuvwxyz")
    m = len(alp)
    n = I()
    s = [input() for i in range(n)]
    d = defaultdict(lambda : 0)
    for i in s:
        d[tuple(f(i))] += 1
    ans = 0
    for i in d.values():
        ans += (i*(i-1))//2
    print(ans)
    return

#D
def D():
    n,m = LI()
    g = LIR(n)
    g.sort(key = lambda x:x[0])
    ans = 0
    f = [1]*n
    j = 0
    q = []
    for i in range(m)[::-1]:
        while j < n and m-g[j][0] >= i:
            a,b = g[j]
            heappush(q,(-b,a))
            j += 1
        if q:
            x,a = heappop(q)
            if i+a <= m:
                ans -= x
    print(ans)
    return

def D_():
    def add(i):
        while i <= m+1:
            bit[i] += 1
            i += i&-i

    def sum(i):
        res = 0
        while i > 0:
            res += bit[i]
            i -= i&-i
        return res

    def root(x):
        if x == par[x]:
            return x
        par[x] = root(x)
        return par[x]

    def unite(x,y):
        x = root(x)
        y = root(y)
        if rank[x] < rank[y]:
            par[x] = y
        else:
            par[y] = x
            if rank[x] == rank[y]:
                rank[x] += 1

    n,m = LI()
    g = LIR(n)
    g.sort(key = lambda x:-x[1])
    bit = [0]*(m+2)
    par = [i for i in range(m+2)]
    rank = [0]*(m+2)
    ans = 0
    for a,b in g:
        i = m-a+1
        if sum(i) < i:
            ans += b
            if sum(i-1) == sum(i):
                add(i)
            else:
                l = 1
                r = i
                while r-l > 1:
                    mid = (l+r)//2
                    if root(mid) == root(i):
                        r = mid
                    else:
                        l = mid
                unite(l,r)
                add(l)
    print(ans)
    return

#E
def E():
    def dfs(x,f):
        for y,w in v[x]:
            if f[y]:
                if dp[y] < dp[x]+w:
                    f[y] = 0
                    dp[y] = dp[x]+w
                    dfs(y,f)
                    f[y] = 1
            else:
                if dp[y] < dp[x]+w:
                    dp[y] = float("inf")
                    dfs(y,f)
    n,m,p = LI()
    v = [[] for i in range(n)]
    for i in range(m):
        a,b,c = LI()
        a -= 1
        b -= 1
        v[a].append((b,c-p))
    f = [1]*n
    dp = [-float("inf")]*n
    dp[0] = 0
    f[0] = 0
    dfs(0,f)
    ans = dp[n-1]
    if ans == float("inf") or ans == -float("inf"):
        print(-1)
    elif ans < 0:
        print(0)
    else:
        print(ans)
    return

#F
def F():
    def comb(a,b):
        return fact[a]*inv[b]*inv[a-b]%p

    p = I()
    a = LI()
    fact = [1]
    for i in range(p-1):
        fact.append(fact[-1]*(i+1)%p)
    inv = [1]*p
    inv[p-1] = pow(fact[p-1],p-2,p)
    for i in range(1,p)[::-1]:
        inv[i-1] = inv[i]*i%p
    b = [0]*p
    for i in range(p):
        ai = a[i]
        if ai:
            b[0] += ai
            k = 1
            for j in range(p):
                c = ai*comb(p-1,j)*k%p
                k *= i
                if k >= p:
                    k %= p
                if j%2:
                    b[p-1-j] += c
                else:
                    b[p-1-j] -= c
                b[p-1-j] %= p
    print(*b)
    return

#Solve
if __name__ == "__main__":
    D_()

Submission status: Wrong Answer

Solution code:
N, M = map(int, input().split())
A = [0] * N
B = [0] * N
ab = []
for i in range(N):
    A[i], B[i] = map(int, input().split())
    ab.append([A[i], B[i]])

ab.sort()
i = N
ans = 0
nums = []
while 1 <= i:
    if M <= 0:
        nums.sort()
        nums.reverse()
        if nums[-1] < ab[i-1][1]:
            nums.pop()
            nums.append(ab[i-1][1])
    if M >= 1:
        if ab[i-1][0] <= M:
            nums.append(ab[i-1][1])
            M -= 1
        else:
            while ab[i-1][0] > M:
                i -= 1
                if i - 1 < 0:
                    break
            if ab[i-1][0] <= M:
                nums.append(ab[i-1][1])
                M -= 1
    i -= 1

if len(nums) == 0:
    nums.append(0)
    
print(sum(nums))

Submission status: Wrong Answer

Solution code:
N, M = map(int, input().split())
A = [0] * N
B = [0] * N
ab = []
for i in range(N):
    A[i], B[i] = map(int, input().split())
    ab.append([A[i], B[i]])

ab.sort()
i = N
ans = 0
nums = [0]
while 1 <= i:
    if M <= -1:
        nums.sort()
        nums.reverse()
        if nums[-1] < ab[i-1][1]:
            nums.pop()
            nums.append(ab[i-1][1])
    if M >= 0:
        if ab[i-1][0] <= M:
            nums.append(ab[i-1][1])
            M -= 1
        else:
            while ab[i-1][0] > M:
                i -= 1
                if i - 1 < 0:
                    break
            if ab[i-1][0] <= M:
                nums.append(ab[i-1][1])
                M -= 1
    i -= 1

print(sum(nums))

Submission status: Wrong Answer

Solution code:
#!usr/bin/env python3
from collections import defaultdict,deque
from heapq import heappush, heappop
import sys
import math
import bisect
import random
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def LS():return [list(x) for x in sys.stdin.readline().split()]
def S():
    res = list(sys.stdin.readline())
    if res[-1] == "\n":
        return res[:-1]
    return res

def IR(n):
    return [I() for i in range(n)]
def LIR(n):
    return [LI() for i in range(n)]
def SR(n):
    return [S() for i in range(n)]
def LSR(n):
    return [LS() for i in range(n)]

sys.setrecursionlimit(1000000)
mod = 1000000007

#A
def A():
    a,b = LI()
    print(max(a+b,a-b,a*b))
    return

#B
def B():
    k,x = LI()
    ans = []
    for i in range(x-k+1,x+k):
        ans.append(i)
    print(*ans)
    return

#C
def C():
    def f(s):
        res = [0]*m
        for i in s:
            res[alp.index(i)] += 1
        return res
    alp = list("abcdefghijklmnopqrstuvwxyz")
    m = len(alp)
    n = I()
    s = [input() for i in range(n)]
    d = defaultdict(lambda : 0)
    for i in s:
        d[tuple(f(i))] += 1
    ans = 0
    for i in d.values():
        ans += (i*(i-1))//2
    print(ans)
    return

#D
def D():
    n,m = LI()
    g = LIR(n)
    g.sort(key = lambda x:x[0])
    ans = 0
    f = [1]*n
    j = 0
    q = []
    for i in range(m)[::-1]:
        while j < n and m-g[j][0] >= i:
            a,b = g[j]
            heappush(q,(-b,a))
            j += 1
        if q:
            x,a = heappop(q)
            if i+a <= m:
                ans -= x
    print(ans)
    return

def D_():
    def add(i):
        while i <= m+1:
            bit[i] += 1
            i += i&-i

    def sum(i):
        res = 0
        while i > 0:
            res += bit[i]
            i -= i&-i
        return res

    def root(x):
        if x == par[x]:
            return x
        par[x] = root(x)
        return par[x]

    def unite(x,y):
        x = root(x)
        y = root(y)
        if rank[x] < rank[y]:
            par[x] = y
        else:
            par[y] = x
            if rank[x] == rank[y]:
                rank[x] += 1

    n,m = LI()
    g = LIR(n)
    g.sort(key = lambda x:-x[1])
    bit = [0]*(m+2)
    par = [i for i in range(m+2)]
    rank = [0]*(m+2)
    ans = 0
    for a,b in g:
        i = m-a+1
        if sum(i) < i:
            ans += b
            if sum(i-1) == sum(i):
                add(i)
            else:
                l = 0
                r = i
                while r-l > 1:
                    mid = (l+r)//2
                    if root(mid) == root(i):
                        r = mid
                    else:
                        l = mid
                unite(l,r)
                add(l)
    print(ans)
    return

#E
def E():
    def dfs(x,f):
        for y,w in v[x]:
            if f[y]:
                if dp[y] < dp[x]+w:
                    f[y] = 0
                    dp[y] = dp[x]+w
                    dfs(y,f)
                    f[y] = 1
            else:
                if dp[y] < dp[x]+w:
                    dp[y] = float("inf")
                    dfs(y,f)
    n,m,p = LI()
    v = [[] for i in range(n)]
    for i in range(m):
        a,b,c = LI()
        a -= 1
        b -= 1
        v[a].append((b,c-p))
    f = [1]*n
    dp = [-float("inf")]*n
    dp[0] = 0
    f[0] = 0
    dfs(0,f)
    ans = dp[n-1]
    if ans == float("inf") or ans == -float("inf"):
        print(-1)
    elif ans < 0:
        print(0)
    else:
        print(ans)
    return

#F
def F():
    def comb(a,b):
        return fact[a]*inv[b]*inv[a-b]%p

    p = I()
    a = LI()
    fact = [1]
    for i in range(p-1):
        fact.append(fact[-1]*(i+1)%p)
    inv = [1]*p
    inv[p-1] = pow(fact[p-1],p-2,p)
    for i in range(1,p)[::-1]:
        inv[i-1] = inv[i]*i%p
    b = [0]*p
    for i in range(p):
        ai = a[i]
        if ai:
            b[0] += ai
            k = 1
            for j in range(p):
                c = ai*comb(p-1,j)*k%p
                k *= i
                if k >= p:
                    k %= p
                if j%2:
                    b[p-1-j] += c
                else:
                    b[p-1-j] -= c
                b[p-1-j] %= p
    print(*b)
    return

#Solve
if __name__ == "__main__":
    D_()

Submission status: Wrong Answer

Solution code:
N, M = map(int, input().split())
A = [0] * N
B = [0] * N
ab = []
for i in range(N):
    A[i], B[i] = map(int, input().split())
    ab.append([A[i], B[i]])

ab.sort()
i = N
ans = 0
nums = [0]
while 1 <= i:
    if M <= 0:
        nums.sort()
        nums.reverse()
        if nums[-1] < ab[i-1][1]:
            nums.pop()
            nums.append(ab[i-1][1])
    if M >= 1:
        if ab[i-1][0] <= M:
            nums.append(ab[i-1][1])
            M -= 1
        else:
            while ab[i-1][0] > M:
                i -= 1
                if i - 1 < 0:
                    break
            if ab[i-1][0] <= M:
                nums.append(ab[i-1][1])
                M -= 1
    i -= 1

print(sum(nums))

Submission status: Wrong Answer

Solution code:
from operator import itemgetter, attrgetter
N, M = map(int, input().split())
AB_list = []
for i in range(N):
  A, B = map(int, input().split())

Submission status: Wrong Answer

Solution code:
N, M = map(int, input().split())
A = [0] * N
B = [0] * N
ab = []
for i in range(N):
    A[i], B[i] = map(int, input().split())
    ab.append([A[i], B[i]])

ab.sort()
i = N
ans = 0
M -= 1
nums = [0]
while 1 <= i:
    if M <= 0:
        nums.sort()
        nums.reverse()
        if nums[-1] < ab[i-1][1]:
            nums.pop()
            nums.append(ab[i-1][1])
    if M >= 1:
        if ab[i-1][0] <= M:
            nums.append(ab[i-1][1])
        else:
            while ab[i-1][0] > M:
                i -= 1
                if i - 1 < 0:
                    break
            if ab[i-1][0] <= M:
                nums.append(ab[i-1][1])
    i -= 1
    M -= 1

print(sum(nums))

Submission status: Wrong Answer

Solution code:
from operator import itemgetter
a,b=map(int,input().split())
c=[list(map(int,input().split())) for i in range(a)]
c=sorted(c,key=itemgetter(1)) 
sum=0
    
for i in range(1,b+1):
  j=a-1
  while j!=0:
    if c[j][0]<=i:
      sum+=c[j][1]
      a-=1
      c.pop(j)
      break
    j-=1
      
    
  
print(sum)

Submission status: Wrong Answer

Solution code:
from operator import itemgetter, attrgetter
N, M = map(int, input().split())
AB_list = []
for i in range(N):
  A, B = map(int, input().split())
  if A <= M:
    AB_list.append([A, B])

Submission status: Wrong Answer

Solution code:
N, M = map(int, input().split())
A = [0] * N
B = [0] * N
ab = []
for i in range(N):
    A[i], B[i] = map(int, input().split())
    ab.append([A[i], B[i]])

ab.sort()
i = N
ans = 0
M -= 1
nums = [0]
while 1 <= i:
    if M <= -1:
        nums.sort()
        nums.reverse()
        if nums[-1] < ab[i-1][1]:
            nums.pop()
            nums.append(ab[i-1][1])
    if M >= 0:
        if ab[i-1][0] <= M:
            nums.append(ab[i-1][1])
        else:
            while ab[i-1][0] > M:
                i -= 1
                if i - 1 < 0:
                    break
            if ab[i-1][0] <= M:
                nums.append(ab[i-1][1])
    i -= 1
    M -= 1

print(sum(nums))

Submission status: Wrong Answer

Solution code:
import heapq
N, M = map(int, input().split())
H = [[] for _ in range(M)]

for _ in range(N):
    A, B = map(int, input().split())
    if A <= M:
        H[A - 1].append(-B)
        
Q = []
c = 0
for d in range(M):
    for b in H[d]:
        Q.append(b)
    if Q:
        c -= min(Q)
        Q.remove(min(Q))
    print(Q)
print(c)

Submission status: Wrong Answer

Solution code:
import heapq

n,m=map(int,input().split())
arr=[list(map(int,input().split())) for _ in range(n)]
arr2=[[i,0] for i in range(1,m+2)]
arr.extend(arr2)
arr=sorted(arr,reverse=True,key=lambda x:x[1])
q=[]
ans=0
pos=0
while 1:
  a,b=arr[pos]
  if a>m:
    break
  if a==arr[pos+1][0]:
    heapq.heappush(q,-b)
  else:
    heapq.heappush(q,-b)
    ans-=heapq.heappop(q)
  pos+=1
print(ans)

Submission status: Wrong Answer

Solution code:
from operator import itemgetter, attrgetter
N, M = map(int, input().split())
AB_list = []
for i in range(N):
  A, B = map(int, input().split())
  AB_list.append([A, B])

Submission status: Wrong Answer

Solution code:
N, M = map(int, input().split())
A = [0] * N
B = [0] * N
ab = []
for i in range(N):
    A[i], B[i] = map(int, input().split())
    ab.append([A[i], B[i]])

ab.sort()
i = N
ans = 0
nums = []
while 1 <= i:
    if M <= 0:
        nums.sort()
        nums.reverse()
        if nums[-1] < ab[i-1][1]:
            nums.pop()
            nums.append(ab[i-1][1])
    if M >= 1:
        if ab[i-1][0] <= M:
            nums.append(ab[i-1][1])
        else:
            while ab[i-1][0] > M:
                i -= 1
                if i - 1 < 0:
                    break
            if ab[i-1][0] <= M:
                nums.append(ab[i-1][1])
    i -= 1
    M -= 1

print(sum(nums))

Submission status: Wrong Answer

Solution code:
from operator import itemgetter, attrgetter
N, M = map(int, input().split())
AB_list = []
for i in range(N):
  A, B = map(int, input().split())
  AB_list.append([A, B])
AB_list.sort(key=itemgetter(1, 0), reverse=True)

Submission status: Wrong Answer

Solution code:
from operator import itemgetter, attrgetter
N, M = map(int, input().split())
AB_list = []
for i in range(N):
  A, B = map(int, input().split())
  AB_list.append([A, B])
AB_list = sorted(AB_list, key=itemgetter(1, 0), reverse=True)

Submission status: Wrong Answer

Solution code:
N, M = map(int, input().split())
A = [0] * N
B = [0] * N
ab = []
for i in range(N):
    A[i], B[i] = map(int, input().split())
    ab.append([A[i], B[i]])

ab.sort()
i = N
ans = 0
nums = []
while 1 <= i:
    if M <= 0:
        nums.sort()
        nums.reverse()
        if nums[-1] < ab[i-1][1]:
            nums.pop()
            nums.append(ab[i-1][1])
    if M >= 1:
        if ab[i-1][0] <= M:
            nums.append(ab[i-1][1])
        else:
            while ab[i-1][0] > M:
                i -= 1
                if i - 1 < 0:
                    break
            nums.append(ab[i-1][1])
    i -= 1
    M -= 1

print(sum(nums))

Submission status: Wrong Answer

Solution code:
from operator import itemgetter, attrgetter
N, M = map(int, input().split())
AB_list = []
for i in range(N):
  A, B = map(int, input().split())
  AB_list.append([A, B])
AB_list = sorted(AB_list, key=itemgetter(1, 0), reverse=True)
result = 0
for m in range(M, 0, -1):
  AB_list = list(filter(lambda ab: ab[0] <= m, AB_list))
  if AB_list:
    result += AB_list.pop(0)[1]
print(result)

Submission status: Wrong Answer

Solution code:
N, M = map(int, input().split())
A = [0] * N
B = [0] * N
ab = []
for i in range(N):
    A[i], B[i] = map(int, input().split())
    ab.append([A[i], B[i]])

ab.sort()
i = N
ans = 0
nums = []
while 1 <= i:
    if M <= 0:
        nums.sort()
        nums.reverse()
        if nums[-1] < ab[i-1][1]:
            nums.pop()
            nums.append(ab[i-1][1])
    if M >= 1:
        if ab[i-1][0] <= M:
            nums.append(ab[i-1][1])
        else:
            while ab[i-1][0] > M:
                i -= 1
                if i - 1 < 0:
                    break
                nums.append(ab[i-1][1])
    i -= 1
    M -= 1

print(sum(nums))

Submission status: Wrong Answer

Solution code:
n, m = map(int,input().split())
a =[]
b = []
for i in range(n):
    a1, b1 = [int(i) for i in input().split()]
    a.append(a1)
    b.append(b1)

itv = []
for i in range(n):
    itv.append([b[i], a[i]]) #B(報酬)でソートすることに注意
itv.sort(reverse = True)

#B(報酬)が最大のものを選んで1日減らす，複数あった場合はAが大きい方から選ぶ
days = m
ans = []

for i in range(n):
    if itv[i][1] <= days:
        ans.append(itv[i][0])
        days = days - 1

print(sum(ans))

Submission status: Wrong Answer

Solution code:
import heapq

N, M = map(int, input().split())
AB_list = []
[heapq.heappush(AB_list, list(map(int, input().split()))) for _ in range(N)]
ans = 0
kouho = []

for day in range(1, M + 1):
    while 1:
        if len(AB_list) != 0:
            i = heapq.heappop(AB_list)
            if i[0] > day:
                break
            else:
                heapq.heappush(kouho, i[1]*(-1))
        else:
            break
    if len(kouho) != 0:
        ans += heapq.heappop(kouho)

print(ans * (-1))

Submission status: Wrong Answer

Solution code:
from heapq import heappop, heappush


def p_d():
    n, m = map(int, input().split())
    ab = [tuple(map(int, input().split())) for _ in range(n)]

    ab.sort()
    ans = 0
    j = 0
    H = []

    for i in range(1, m + 1):
        while j < n and ab[j][0] == i + 1:
            heappush(H, -ab[j][1])
            j += 1

        if H:
            ans -= heappop(H)

    print(ans)


if __name__ == '__main__':
    p_d()

Submission status: Wrong Answer

Solution code:
from heapq import heappop, heappush


def p_d():
    n, m = map(int, input().split())
    ab = [tuple(map(int, input().split())) for _ in range(n)]
    ab = sorted(ab)
    ans = 0
    j = 0

    for i in range(1, m + 1):
        H = []
        while j < n and ab[j][0] == i + 1:
            heappush(H, -ab[j][1])
            j += 1

        if H:
            ans -= heappop(H)

    print(ans)


if __name__ == '__main__':
    p_d()

Submission status: Wrong Answer

Solution code:
#input = raw_input
import numpy as np

N, M = map(int, input().split())

pairs = []
for i in range(N):
    A, B = map(int, input().split())
    if M >= A:
        pairs.append([A, B])

pairs = sorted(pairs, key=lambda elm: -elm[1])
#print(pairs)

out = 0

#not_done = [True for _ in range(M)]

#counts = [0 for _ in range(M)]
cumsum_counts = np.zeros(M, dtype=int)

all_sum = 0

ue_end_i = M-1
for A, B in pairs:
    ue_start_i = M-A+1
    if ue_start_i <= ue_end_i:
        ue_limit = ue_end_i - ue_start_i + 1
        #ue_sum = sum(counts[ue_start_i:])
        ue_sum = cumsum_counts[ue_start_i]
        hamidashi = max(ue_sum - ue_limit, 0)
    else:
        # ue_start_i == M
        ue_sum = 0
        hamidashi = 0
    #print("hamidashi", hamidashi)

    cur_limit = M- A + 1 - hamidashi # 0 ~ M-A
    sudeni = all_sum - ue_sum
    aki = cur_limit - sudeni

    if aki >= 1:
        out += B
        #counts[M-A] += 1
        cumsum_counts[M-A:] += 1
        all_sum += 1

print(out)

Submission status: Wrong Answer

Solution code:
from heapq import heappop, heappush


def p_d():
    n, m = map(int, input().split())
    ab = [tuple(map(int, input().split())) for _ in range(n)]
    ab = sorted(ab)
    ans = 0

    for i in range(1, m + 1):
        H = []
        for j, e in enumerate(ab):
            if e[0] <= i:
                heappush(H, (-e[1], j))
                ab.pop(j)
            else:
                break

        if H:
            e = heappop(H)
            ans -= e[0]
            # ab.pop(e[1])

    print(ans)


if __name__ == '__main__':
    p_d()

Submission status: Wrong Answer

Solution code:
import collections
def main():
    nm = input().split()
    n, m = int(nm[0]), int(nm[1])

    d = collections.defaultdict(list)
    for i in range(n):
        ab = input().split()
        ai, bi = int(ab[0]), int(ab[1])
        d[bi].append(ai)
    for bi in d:
        d[bi] = sorted(d[bi], reverse = True)
    rewards = sorted(list(d.keys()), reverse = True)

    busy = [0 for _ in range(m)]
    ans = 0
    print(d)
    for bi in rewards:
        for i in range(m-1, -1, -1):
            if busy[i] == 0:
                if d[bi] and d[bi][-1] <= m-i:
                    busy[i] = 1
                    ans += bi
                    ai = d[bi].pop()
                    # print(ai, bi)

    return ans

if __name__ == '__main__':
    print(main())