Submission #27471515


Source Code Expand

from collections import defaultdict
from heapq import heappush, heappop

class DeletableMaxHeapQ():
    def __init__(self):
        self.H = []
        self.HC = defaultdict(int)
    def hpush(self, x):
        heappush(self.H, -x)
        self.HC[x] += 1
    def hpop(self):
        t = -heappop(self.H)
        while not self.HC[t]:
            t = -heappop(self.H)
        self.HC[t] -= 1
        return t
    def hmax(self):
        t = -self.H[0]
        while not self.HC[t]:
            heappop(self.H)
            t = -self.H[0]
        return t
    def hdel(self, x):
        assert self.HC[x] > 0
        self.HC[x] -= 1

class LR_trick():
    def __init__(self, n):
        self.n = n
        self.L = [i - 1 for i in range(n)]
        self.R = [i + 1 for i in range(n)]
        self.X = [1] * n
    
    def delete(self, i):
        self.X[i] = 0
    
    def left(self, i):
        i = self.L[i]
        li = []
        while i >= 0 and self.X[i] == 0:
            li.append(i)
            i = self.L[i]
        for j in li:
            self.L[j] = i
        return i
        
    def right(self, i):
        i = self.R[i]
        li = []
        while i < self.n and self.X[i] == 0:
            li.append(i)
            i = self.R[i]
        for j in li:
            self.R[j] = i
        return i
    
    def debug(self):
        print("L =", self.L)
        print("R =", self.R)
        print("X =", self.X)
    
N, M = map(int, input().split())
A = [int(a) for a in input().split()]
if M * 2 == N:
    print(sum(A))
    exit()
if M > N - M:
    M = N - M

dh = DeletableMaxHeapQ()
lr = LR_trick(N)

for i in range(N - 2):
    x = (A[i] + A[i+1] << 20) + i
    dh.hpush(x)

ans = 0
mmm = (1 << 20) - 1
for _ in range(M):
    x = dh.hpop()
    i = x & mmm
    j = lr.right(i)
    lr.delete(i)
    lr.delete(j)
    ans += x >> 20
    l = lr.left(i)
    r = lr.right(j)
    if l >= 0:
        y = (A[l] + A[i] << 20) + l
        dh.hdel(y)
    if r < N - 1:
        y = (A[j] + A[r] << 20) + j
        dh.hdel(y)
    if 0 <= l and r < N - 1:
        y = (A[l] + A[r] << 20) + l
        dh.hpush(y)
    
print(ans)

Submission Info

Submission Time
Task H - Red and Blue Lamps
User Kiri8128
Language PyPy3 (7.3.0)
Score 600
Code Size 2224 Byte
Status AC
Exec Time 1163 ms
Memory 160104 KiB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 600 / 600
Status
AC × 3
AC × 28
Set Name Test Cases
Sample sample_01.txt, sample_02.txt, sample_03.txt
All hand_01.txt, random_01.txt, random_02.txt, random_03.txt, random_04.txt, random_05.txt, random_06.txt, random_07.txt, random_08.txt, random_09.txt, random_10.txt, random_11.txt, random_12.txt, random_13.txt, random_14.txt, random_15.txt, random_16.txt, random_17.txt, random_18.txt, random_19.txt, random_20.txt, random_21.txt, random_22.txt, random_23.txt, random_24.txt, sample_01.txt, sample_02.txt, sample_03.txt
Case Name Status Exec Time Memory
hand_01.txt AC 74 ms 65504 KiB
random_01.txt AC 266 ms 90356 KiB
random_02.txt AC 416 ms 96600 KiB
random_03.txt AC 778 ms 127532 KiB
random_04.txt AC 389 ms 94284 KiB
random_05.txt AC 567 ms 102892 KiB
random_06.txt AC 364 ms 92756 KiB
random_07.txt AC 284 ms 124544 KiB
random_08.txt AC 935 ms 144672 KiB
random_09.txt AC 185 ms 79688 KiB
random_10.txt AC 643 ms 127552 KiB
random_11.txt AC 175 ms 125624 KiB
random_12.txt AC 637 ms 136704 KiB
random_13.txt AC 1151 ms 158100 KiB
random_14.txt AC 1163 ms 160104 KiB
random_15.txt AC 658 ms 136236 KiB
random_16.txt AC 172 ms 125320 KiB
random_17.txt AC 155 ms 120928 KiB
random_18.txt AC 242 ms 123124 KiB
random_19.txt AC 269 ms 128164 KiB
random_20.txt AC 256 ms 128048 KiB
random_21.txt AC 146 ms 113256 KiB
random_22.txt AC 832 ms 133128 KiB
random_23.txt AC 216 ms 108912 KiB
random_24.txt AC 258 ms 141696 KiB
sample_01.txt AC 51 ms 65636 KiB
sample_02.txt AC 56 ms 65188 KiB
sample_03.txt AC 57 ms 65408 KiB