96612024-02-23 19:16:44NagyLeoÓvodapython3Runtime error 46/50330ms67068 KiB
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] = (chosen[i], crying_time[i], 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[merged[i][0]] > 0:
                roles[merged[i][0]] -= 1
                roles_av.discard(merged[i][0])
                res[merged[i][2]] = merged[i][0] + 1
            else:
                cry += merged[i][1]
                leftout.append(merged[i])
        else:
            if merged[i][0] in roles_av:
                roles[merged[i][0]] -= 1
                roles_av.discard(merged[i][0])
                res[merged[i][2]] = 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][2]] = i + 1
        roles[i] -= 1
        leftout.pop()

    index = 0
    for i in leftout:
        while roles[index] == 0:
            index += 1
        res[i[2]] = index + 1
        roles[index] -= 1
    print(cry)
    print(*res)


main()
SubtaskSumTestVerdictTimeMemory
base46/50
1Accepted0/018ms11580 KiB
2Accepted0/032ms14724 KiB
3Accepted2/218ms11792 KiB
4Accepted2/217ms12108 KiB
5Accepted2/218ms12496 KiB
6Accepted2/217ms12896 KiB
7Accepted2/217ms12772 KiB
8Accepted2/217ms12940 KiB
9Accepted2/217ms12680 KiB
10Accepted2/217ms13308 KiB
11Accepted2/218ms13188 KiB
12Accepted2/217ms13072 KiB
13Accepted2/219ms13912 KiB
14Accepted3/320ms13744 KiB
15Accepted3/352ms20688 KiB
16Accepted3/396ms31636 KiB
17Accepted3/3145ms34404 KiB
18Accepted3/3234ms48716 KiB
19Accepted3/3257ms52708 KiB
20Accepted3/3291ms52640 KiB
21Accepted3/3330ms62912 KiB
22Runtime error0/4149ms67068 KiB