Survive the Hits
Python
Hard
2 views
Problem Description
You start with L lives. You get q events: HIT x (lose x) or HEAL x (gain x). The moment lives becomes
Input Format
First line L. Second line q. Next q lines events.
Output Format
One line: processed finalLives.
Sample Test Case
Input:
10
5
HIT 3
HEAL 1
HIT 20
HEAL 5
HIT 1
Official Solution
import sys
lines=sys.stdin.read().splitlines()
if len(lines)<2:
sys.exit(0)
L=int(lines[0].strip())
q=int(lines[1].strip())
lives=L
processed=0
for i in range(q):
parts=(lines[2+i] if 2+i<len(lines) else '').split()
if len(parts)<2:
continue
typ=parts[0]
x=int(parts[1])
if typ=='HIT':
lives-=x
else:
lives+=x
processed+=1
if lives<=0:
break
sys.stdout.write(f'{processed} {lives}')
Solutions (0)
No solutions submitted yet. Be the first!
No comments yet. Start the discussion!