96632024-02-23 19:25:26NagyLeoÓvodapython3Runtime error 46/50344ms67712 KiB
from sys import stdin, stdout
input=stdin.readline

def main():
    N, K = map(int, input().split())
    roles = list(map(int, input().split()))  # K hosszu
    chosen = list(map(lambda x: int(x) - 1, input().split()))  # N hosszú
    crying_time = list(map(int, input().split()))  # N hosszu
    merged = [0] * N
    leftout = []
    roles_av = set(range(K))

    for i in range(N):
        merged[i] = (i, crying_time[i])

    merged.sort(key=lambda x: -x[1])

    cry = 0
    res = [-1] * N

    for i in range(N):
        # print(i,merged[i],res,roles_av,leftout, roles,len(roles_av), N-i-len(leftout))
        if len(roles_av) < N - i + len(leftout):
            if roles[chosen[merged[i][0]]] > 0:
                roles[chosen[merged[i][0]]] -= 1
                roles_av.discard(chosen[merged[i][0]])
                res[merged[i][0]] = chosen[merged[i][0]] + 1
            else:
                cry += merged[i][1]
                leftout.append(merged[i])
        else:
            if chosen[merged[i][0]] in roles_av:
                roles[chosen[merged[i][0]]] -= 1
                roles_av.discard(chosen[merged[i][0]])
                res[merged[i][0]] = chosen[merged[i][0]] + 1
            else:
                cry += merged[i][1]
                leftout.append(merged[i])
        # rint('--------',i,merged[i],res,roles_av,leftout, roles)

    # print(len(roles_av),len(leftout))

    for i in roles_av:
        res[leftout[-1][0]] = i + 1
        roles[i] -= 1
        leftout.pop()

    index = 0
    for i in leftout:
        while roles[index] == 0:
            index += 1
        res[i[0]] = index + 1
        roles[index] -= 1
    stdout.write(str(cry))
    print()
    for i in range(N):
        stdout.write(str(f"{res[i]} "))


main()
SubtaskSumTestVerdictTimeMemory
base46/50
1Accepted0/017ms11696 KiB
2Accepted0/032ms14664 KiB
3Accepted2/217ms12088 KiB
4Accepted2/217ms12216 KiB
5Accepted2/218ms12660 KiB
6Accepted2/217ms12608 KiB
7Accepted2/217ms12696 KiB
8Accepted2/217ms13332 KiB
9Accepted2/217ms13048 KiB
10Accepted2/217ms13068 KiB
11Accepted2/217ms13056 KiB
12Accepted2/217ms13284 KiB
13Accepted2/219ms13816 KiB
14Accepted3/319ms13848 KiB
15Accepted3/352ms20572 KiB
16Accepted3/398ms30968 KiB
17Accepted3/3149ms35084 KiB
18Accepted3/3222ms48336 KiB
19Accepted3/3268ms52576 KiB
20Accepted3/3310ms51912 KiB
21Accepted3/3344ms62860 KiB
22Runtime error0/4136ms67712 KiB