Sum of Dictionary Values
Python
Easy
3 views
Problem Description
Read n pairs key and integer value. Add all values and output total sum. Keys may repeat, still add every time.
Input Format
First line n. Next n lines: key value.
Output Format
One integer sum.
Official Solution
import sys
lines=sys.stdin.read().splitlines()
if not lines: sys.exit(0)
n=int(lines[0].strip())
sm=0
for i in range(1,1+n):
parts=(lines[i] if i<len(lines) else '').split()
if len(parts)<2: continue
sm+=int(parts[1])
sys.stdout.write(str(sm))
Solutions (0)
No solutions submitted yet. Be the first!
No comments yet. Start the discussion!