Problem:


問題 3 

　ある工場では，各営業所から製品生産の注文を受けている．
前日の注文をまとめて，各製品の生産合計を求めたい．

　入力ファイルの１行目には注文データの数 n が書いてあり，
続く n 行には製品名と注文数が空白で区切られて書いてある．
製品名は5文字以内の英大文字で書かれている．
注文データには同じ製品が含まれていることもあり，順序はバラバラである．

　この注文データの中に現れる同じ製品の注文数を合計し，
出力ファイルに製品名と合計を空白を区切り文字として出力しなさい．
ただし，製品名に次の順序を付けて，その順で出力すること．

　順序：文字の長さの小さい順に，同じ長さのときは，前から比べて
最初に異なる文字のアルファベット順とする．

入力データにおける製品数，注文数とその合計のどれも106以下である．

　出力ファイルにおいては，
出力の最後の行にも改行コードを入れること．

入出力例

入力例1

5
A 20
B 20
A 20
AB 10
Z 10

出力例1

A 40
B 20
Z 10
AB 10

入力例2

5
AAA 20
ABA 20
AAA 20
AAB 20
AAA 20

出力例2

AAA 60
AAB 20
ABA 20

問題文と自動審判に使われるデータは、情報オリンピック日本委員会が作成し公開している問題文と採点用テストデータです。



Sample input:
5
A 20
B 20
A 20
AB 10
Z 10


Sample output:
A 40
B 20
Z 10
AB 10

Submission status: Wrong Answer

Solution code:
N = int(input())
pdict = {}

p, n = [], []

for i in range(N) :
    _p, _n = input().split()
    _n = int(_n)
    
    if _p not in pdict.keys() :
        pdict[_p] = _n
    else :
        pdict[_p] += _n
        
plist = list(pdict.keys())
plist.sort(key=len)
newdict = {}

for i in plist :
    print(i, pdict[i])
    # newdict[i] = pdict[i]

Submission status: Wrong Answer

Solution code:
o={}
for _ in range(int(input())):p,m=input().split();o[p]=o.get(p,0)+int(m)
k=sorted([(len(x),x) for x in list(o.keys())])
for i,j in k:print(i,o[j])

Submission status: Wrong Answer

Solution code:
data = {}
for i in range(int(input())):
	p,n = input().split();
	data[p] = data.get(p,0) + int(n)
for i, j in sorted(data.items()):
    print (i, j)

Submission status: Wrong Answer

Solution code:
import copy
def main():
    n = int(input())
    a = []
    b = []
    for x in range(n):
        a.append(input().split())
        a[x][1] = int(a[x][1])
        b.append(a[x][0])

    b = list(set(b))
    c = [0] * len(b)
    for x in range(n):
        c[b.index(a[x][0])] += a[x][1]

    alp = [chr(i) for i in range(64, 65 + 26)]
    bb = copy.deepcopy(b)
    bb_num = []
    for x in range(len(bb)):
        bb[x] = list(bb[x])
        hoge = 0
        for y in range(len(bb[x])):
            hoge += alp.index(bb[x][y]) * 10 ** (len(bb[x]) - y - 1)
        bb_num.append(hoge)

    bb_num_copy = copy.deepcopy(bb_num)
    bb_num_copy.sort()

    for x in range(len(bb_num_copy)):
        hoge = b[bb_num.index(bb_num_copy[x])]
        fuga = c[bb_num.index(bb_num_copy[x])]
        print(hoge, fuga)

    #print("a           :", a)
    #print("b           :", b)
    #print("c           :", c)
    #print("bb          :", bb)
    #print("bb_num      :", bb_num)
    #print("bb_num_copy :", bb_num_copy)


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
from collections import Counter
order = Counter()
N = int(input())
for i in range(N):
    A = input().split()
    name,quantity = A[0],int(A[1])
    order[name] += quantity
po = sorted(order.items(),key=lambda x:len(x[0]))
pu = sorted(po,key=lambda x:x[0])
for j,k in pu:
    print(j,k)

Submission status: Wrong Answer

Solution code:
d={}
for _ in[0]*int(input()):
 k,v=input().split()
 d[k]=d.get(k,0)+int(v)
for k in sorted(d,key=lambda x:(sum(26**i+ord(k)for i,k in enumerate(x[::-1])),x)):
 print(k,d[k])

Submission status: Wrong Answer

Solution code:
data = {}
n = int(raw_input())
for i in range(n):
    ipt = raw_input().split()
    if data.get(ipt[0])==None: data[ipt[0]] = 0
    data[ipt[0]] += int(ipt[1])
for k in sorted(data.keys()):
    print k+' '+str(data[k])

Submission status: Wrong Answer

Solution code:
dict = {}
n = int(input())
for _ in range(n):
    key, value = map(str, raw_input().split())
    if key in dict:
        dict[key] += int(value)
    else:
        dict[key] = int(value)
    
for key, value in dict.iteritems():
    print key, value

Submission status: Accepted

Solution code:
N = int(input())
pdict = {}

p, n = [], []

for i in range(N) :
    _p, _n = input().split()
    _n = int(_n)
    
    if _p not in pdict.keys() :
        pdict[_p] = _n
    else :
        pdict[_p] += _n
        
plist = list(pdict.keys())

# fetch maximum length
max_len = 0
for i in set(plist) :
    max_len = max(max_len, len(i))

# append  key array corresponding to maximum length
plist.sort()

keys_sorted = []

for i in range(max_len + 1) :
    for j in plist :
        if i == len(j) :
            keys_sorted.append(j)
            
for i in keys_sorted :
    print(i, pdict[i])

Submission status: Accepted

Solution code:
n = int(input())
o = {}
for i in range(n):
    p, m = input().split()
    o[p] = o.get(p, 0) + int(m)
k = list(o.keys())
k = [(len(x), x) for x in k]
k.sort()
for i in k:
    print(i[1], o[i[1]])

Submission status: Accepted

Solution code:
o={}
for _ in range(int(input())):p,m=input().split();o[p]=o.get(p,0)+int(m)
k=sorted([(len(x),x) for x in list(o.keys())])
for _,i in k:print(i,o[i])

Submission status: Accepted

Solution code:
o={}
for _ in range(int(input())):p,m=input().split();o[p]=o.get(p,0)+int(m)
k=sorted([(len(x),x) for x in o.keys()])
for _,i in k:print(i,o[i])

Submission status: Accepted

Solution code:
o={}
for _ in range(int(input())):p,m=input().split();o[p]=o.get(p,0)+int(m)
k=sorted([(len(x),x)for x in o.keys()])
for _,i in k:print(i,o[i])

Submission status: Accepted

Solution code:
n = input()
d = {}
for i in range(n):
    a, b = raw_input().split()
    if a in d: d[a] += int(b)
    else: d[a] = int(b)
d = sorted(d.items())
for ans in sorted(d, key = lambda x: len(x[0])):
     print " ".join(map(str,ans))

Submission status: Accepted

Solution code:
n = input()
d = {}
for i in range(n):
    a, b = raw_input().split()
    if a in d: d[a] += int(b)
    else: d[a] = int(b)
for ans in sorted(sorted(d.items()), key = lambda x: len(x[0])):
     print " ".join(map(str,ans))

Submission status: Accepted

Solution code:
data = {}
for i in range(int(input())):
	p,n = input().split();
	data[p] = data.get(p,0) + int(n)
for i, j in sorted([[len(a),a] for a in data.keys()]):
    print (j, data[j])

Submission status: Accepted

Solution code:
if __name__ == '__main__':
    # ??????????????\???
    num_of_orders = int(input())
    data = []
    for d in range(num_of_orders):
        data.append(tuple(input().split(' ')))

    # ??¨???????????????????¨?
    orders = dict()
    for p_name, p_count in data:
        if p_name in orders:
            orders[p_name] += int(p_count)
        else:
            orders[p_name] = int(p_count)

    results = []
    for k, v in orders.items():
        results.append(tuple([k, v]))
    results.sort(key=lambda x:(len(x[0]), x[0])) # ?£????????????????????????¨?£????????????¢???????????????????????§?????????

    # ???????????????
    for r in results:
        print('{0} {1}'.format(r[0], r[1]))

Submission status: Accepted

Solution code:
p={}
for _ in range(int(input())):
    a,b=input().split()
    if a in p:p[a]+=int(b)
    else:p[a]=int(b)
for i in sorted(p,key=lambda x:(len(x),x)):
    print(i,p[i])

Submission status: Accepted

Solution code:
n = int(input())
d, order = {}, {}
for _ in range(n):
    a, num = input().split()
    d[a] = d[a] + int(num) if a in d else int(num)
    if len(a) in order:
        if a not in order[len(a)]:
            order[len(a)].append(a)
    else:
        order[len(a)] = []
        order[len(a)].append(a)
for k, v in sorted(order.items(), key=lambda x: x[0]):
    for a in sorted(v):
        print(a, d[a])

Submission status: Accepted

Solution code:
from collections import Counter
order = Counter()
N = int(input())
for i in range(N):
    A = input().split()
    name,quantity = A[0],int(A[1])
    order[name] += quantity
po = sorted(order.items(),key=lambda x:x[0])
pu = sorted(po,key=lambda x:len(x[0]))
for j,k in pu:
    print(j,k)

Submission status: Accepted

Solution code:
nums = int(input())
data = {}
for i in range(nums):
    name, num = input().split()
    data[name] = data.get(name, 0) + int(num)
sorteddata = sorted(list(data.items()), key=lambda x: (len(x[0]), x))
for name, num in sorteddata:
    print(name, num)

Submission status: Accepted

Solution code:
data = {}
for _ in range(int(input())):
    p,n = input().split();
    data[p] = data.get(p,0) + int(n)
k = sorted([[len(a),a] for a in data.keys()])
for _, i in k:
    print (i, data[i])

Submission status: Accepted

Solution code:
d={}
for _ in[0]*int(input()):
 k,v=input().split()
 d[k]=d.get(k,0)+int(v)
for l,k in sorted([len(x),x]for x in d):
 print(k,d[k])

Submission status: Accepted

Solution code:
d={}
for _ in[0]*int(input()):
 k,v=input().split()
 d[k]=d.get(k,0)+int(v)
for k in sorted(d,key=lambda x:(len(x),x)):
 print(k,d[k])

Submission status: Accepted

Solution code:
d={}
for _ in[0]*int(input()):
 k,v=input().split()
 d[k]=d.get(k,0)+int(v)
for k in sorted(d,key=lambda x:(sum(27**i*(ord(k)-64)for i,k in enumerate(x[::-1])),x)):
 print(k,d[k])

Submission status: Accepted

Solution code:
n = int(input())
o = {}
for i in range(n):
    name, value = input().split()
    if name in o:
        o[name] += int(value)
    else:
        o[name] = int(value)
o = sorted(o.items(), key=lambda x: x[0])
o.sort(key = lambda x: len(x[0]))
for i in o:
    print("{0} {1}".format(i[0], i[1]))

Submission status: Accepted

Solution code:
dic = {}
n = int(input())
for i in range(n):
  name, num = input().split()
  num = int(num)
  l = len(name)
  if (l, name) in dic:
    dic[(l, name)] += num
  else:
    dic[(l, name)] = num
dic = sorted(dic.items())
for d in dic:
  print(d[0][1], d[1])

Submission status: Accepted

Solution code:
n = int(input())
d = {}
for i in range(n):
    order, num = input().split()
    num = int(num)
    if order not in d:
        d[order] = num
    else:
        d[order] += num
# tupleにして、keyの長さでソートできるようにする。
d = sorted(d.items())
d = sorted(d, key=lambda x: len(x[0]))

for line in d:
    print(line[0], line[1])

Submission status: Accepted

Solution code:
# coding=utf-8

if __name__ == '__main__':
    N = int(input())
    product_dict = {}
    for i in range(N):
        p_name, number = input().split()
        number = int(number)
        try:
            product_dict[p_name] += number
        except KeyError:
            product_dict[p_name] = number
    view_list = product_dict.items()
    view_list = sorted(view_list, key=lambda x: (len(x[0]), x[0]))
    [print(' '.join(map(str, list_el))) for list_el in view_list]

Submission status: Accepted

Solution code:
# AOJ 0589: Production
# Python3 2018.6.30 bal4u

dic = {}
n = int(input())
for i in range(n):
	a, p = input().split()
	if a in dic: dic[a] += int(p)
	else: dic[a] = int(p)
s = list(dic.items())
s.sort(key=lambda x:(len(x[0]),x[0]))
for x in s: print(*x)

Submission status: Accepted

Solution code:
def comp(a, b):
	c = cmp(len(a), len(b))
	if c == 0:
		c = cmp(a, b)
	return c

n = input()
results = {}
for i in xrange(n):
	name, value = raw_input().split()
	value = int(value)
	results.setdefault(name, 0)
	results[name] += value
for name in sorted(results.keys(), comp):
	print name, results[name]

Submission status: Accepted

Solution code:
n = input()
ans = {}
for i in range(n):
  (a, b) = raw_input().split()
  if not (len(a), a) in ans:
    ans[(len(a), a)] = 0
  ans[(len(a), a)] += int(b)

for (l, k) in sorted(ans):
  print k, ans[(l, k)]

Submission status: Accepted

Solution code:
from collections import defaultdict
d=defaultdict(int)
n=input()
for i in range(n):
    name,num=raw_input().split()
    d[name]+=int(num)
for i in sorted(sorted(d.keys()),key=len):
    print i,d[i]

Submission status: Accepted

Solution code:
data = [{} for i in range(6)]
n = int(raw_input())
for i in range(n):
    ipt = raw_input().split()
    if data[len(ipt[0])].get(ipt[0])==None: data[len(ipt[0])][ipt[0]] = 0
    data[len(ipt[0])][ipt[0]] += int(ipt[1])
for i in range(1,6):
    for k in sorted(data[i].keys()):
        print k+' '+str(data[i][k])

Submission status: Accepted

Solution code:
dict = {}
n = int(input())
for _ in range(n):
    key, value = map(str, raw_input().split())
    if key in dict:
        dict[key] += int(value)
    else:
        dict[key] = int(value)

for k in sorted(sorted(dict.keys()), key=len):
    print k, dict[k]

Submission status: Accepted

Solution code:

def getkey(a):
  return "%6s" % a

n = input()
dic = {}
for i in range(n):
  a, b = raw_input().split()
  b = int(b)
  if a in dic:
    dic[a] += b
  else:
    dic[a] = b
for a in sorted(dic, key=getkey):
  print "%s %d" % (a, dic[a])

Submission status: Accepted

Solution code:
l={}
for i in [1]*input():
	a,n=map(str,raw_input().split())
	try:l[a]+=int(n)
	except:l[a]=int(n)
for k,v in sorted(sorted(l.items(),key=lambda x:x[0]),key=lambda x:len(x[0])):print k,v

Submission status: Accepted

Solution code:
l={}
for i in [1]*input():
	a,n=map(str,raw_input().split())
	try:l[a]+=int(n)
	except:l[a]=int(n)
for i in sorted(sorted(l.keys()),key=len):print i,l[i]

Submission status: Accepted

Solution code:
l={}
for i in [1]*input():
	a,n=map(str,raw_input().split())
	n=int(n)
	try:l[a]+=n
	except:l[a]=n
for i in sorted(sorted(l.keys()),key=len):print i,l[i]

Submission status: Accepted

Solution code:
l={}
for i in [1]*input():
	a,n=raw_input().split()
	n=int(n)
	try:l[a]+=n
	except:l[a]=n
for i in sorted(sorted(l.keys()),key=len):print i,l[i]

Submission status: Accepted

Solution code:
n = int(input())
product = {}
for i in range(n) :
    p, m = input().split()
    if p not in product :
        product[p] = int(m)
    else :
        product[p] += int(m)
sorted_product = dict(sorted(product.items(), key = lambda x: x[0]))
sorted_product = sorted(sorted_product.items(), key = lambda x: len(x[0]))
for i in range(len(sorted_product)) :
    print(*sorted_product[i])

Submission status: Accepted

Solution code:
x ={}
n = int(input())
for i in range(n):
    k,v = input().split()
    if k in x:
        x[k] += int(v)
    else:
        x[k] = int(v)
p=list(x.items())
p.sort(key=lambda x: x[0])
p.sort(key=lambda x: len(x[0]))
for i in p:
    print(i[0],i[1])

Submission status: Accepted

Solution code:
n = int(input())
dic = {}
for i in range(n):
	a, b = input().split()
	a = a.zfill(5)  # キーを0埋め5桁
	if a in dic:
		dic[a] = dic[a] + int(b)
	else:
		dic[a] = int(b)

dic = sorted(dic.items())
# 出力部
for i in dic:
	print('{0} {1}'.format(i[0].replace('0', ''), i[1]))

Submission status: Accepted

Solution code:
n = int(input())
a = {}
for _ in range(n):
    x,y = input().split()
    y = int(y)
    if x in a:
        a[x] += y
    else:
        a[x] = y
a_s = sorted(a.items(), key=lambda p:(len(p[0]), p[0]))
for i in a_s:
    print(i[0], i[1])

Submission status: Accepted

Solution code:
N = int(input())
dic = {}
for i in range(N):
    a, b = input().split()
    b = int(b)
    if a in dic:
        dic[a] += b
    else:
        dic[a] = b
L = [(x, y, len(x)) for x, y in dic.items()]
L.sort(key=lambda x: (x[2], x[0]))

for l in L:
    print(l[0], l[1])

Submission status: Accepted

Solution code:
n=int(input())
d=dict()
for i in range(n):
    a,b=input().split()
    if a in d:d[a]+=int(b)
    else:d[a]=int(b)
s=list(d.items())
s.sort(key=lambda y: y[0])
s.sort(key=lambda y: len(y[0]))
for i in s:print(i[0],i[1])

Submission status: Accepted

Solution code:
n = int(input())

namelist = []
numlist = []
lenlist = []
for _ in range(n):
    name, num = map(str, input().split())
    if name in namelist:
        numlist[namelist.index(name)] += int(num)
    else:
        namelist.append(name)
        numlist.append(int(num))
        lenlist.append(len(name))

data = []
for name, num, l in zip(namelist, numlist, lenlist):
    data.append([name, num, l])

data = sorted(data, key=lambda x: (x[2], x[0]))

for d in data:
    print(d[0], d[1])

Submission status: Accepted

Solution code:
n = int(input())
s = []
for i in range(n):
    dat = input().split(" ")
    if dat[0] in s:
        s[s.index(dat[0])+1] += int(dat[1])
    else:
        s.append(dat[0])
        s.append(int(dat[1]))
for i in range(len(s)//2):
    s[i] = [s[i],s[i+1]]
    del s[i+1]
s.sort(key = lambda x: x[0])
s.sort(key = lambda x: len(x[0]))
for i in range(len(s)):
    print(s[i][0],s[i][1])