n = int(input())						#输入n个数
dic = {}								#创建空字典
for _ in range(n):
    x,y = map(int,input().split())		#输入字典键x和值y
    if x in dic:						#字典已有x键,y加上之前的值
        dic[x] += y
    else:
        dic[x] = y						#新的键直接赋值
for x in sorted(dic.keys()):			#排列好后依次输出
    print(x,dic[x])

思路见代码注释!

注意:

1.字典中只有键,和键对应的值,所以第五行检查字典中是否有x键写为 if x in dic:如果写成dic【x】就是访问字典中x键的值

2.第十行中排序sorted(dic.keys()):第十行注意排序写法