import sys
def solution(n, bd, kt):
res_s, res = 0, []
while n:
if n >= 2:
if len(bd) == 0 and len(kt) == 0:
break
if len(bd) == 0:
kts = kt.pop()
res_s += kts[0]
res.append(kts[1])
n -= 2
continue
if len(kt) == 0:
bds = bd.pop()
res_s += bds[0]
res.append(bds[1])
n -= 1
continue
if n > 2 and n % 2 == 1:
if bd[-1][0] > (kt[-1][0] / 2):
bd1 = bd.pop()
res_s += bd1[0]
res.append(bd1[1])
n -= 1
else:
kts = kt.pop()
res_s += kts[0]
res.append(kts[1])
n -= 2
continue
if n >= 2:
if len(bd) >= 2:
if kt[-1][0] > bd[-1][0] + bd[-2][0]:
kts = kt.pop()
res_s += kts[0]
res.append(kts[1])
n -= 2
else:
bd1 = bd.pop()
bd2 = bd.pop()
res_s += bd1[0] + bd2[0]
res.append(bd1[1])
res.append(bd2[1])
n -= 2
else:
if kt[-1][0] > bd[-1][0]:
kts = kt.pop()
res_s += kts[0]
res.append(kts[1])
n -= 2
else:
bd1 = bd.pop()
res_s += bd1[0]
res.append(bd1[1])
n -= 1
continue
if n == 1:
if len(bd) > 0:
bds = bd.pop()
res_s += bds[0]
res.append(bds[1])
n -= 1
print(res_s)
print(' '.join(map(str, res)))
inp = [tuple(map(int, line.strip().split())) for line in sys.stdin]
_, n = inp[0]
bd, kt = [], []
for i in range(1, len(inp)):
if inp[i][0] == 1:
bd.append((inp[i][1], i))
else:
kt.append((inp[i][1], i))
bd.sort(key=lambda x:x[0])
kt.sort(key=lambda x:x[0])
solution(n, bd, kt)
Lift queries | Goki and his breakup |
Ali and Helping innocent people | Book of Potion making |
Duration | Birthday Party |
e-maze-in | Bricks Game |
Char Sum | Two Strings |
Anagrams | Prime Number |
Lexical Sorting Reloaded | 1514A - Perfectly Imperfect Array |
580A- Kefa and First Steps | 1472B- Fair Division |
996A - Hit the Lottery | MSNSADM1 Football |
MATCHES Playing with Matches | HRDSEQ Hard Sequence |
DRCHEF Doctor Chef | 559. Maximum Depth of N-ary Tree |
821. Shortest Distance to a Character | 1441. Build an Array With Stack Operations |
1356. Sort Integers by The Number of 1 Bits | 922. Sort Array By Parity II |
344. Reverse String | 1047. Remove All Adjacent Duplicates In String |
977. Squares of a Sorted Array | 852. Peak Index in a Mountain Array |