解题思路:使用map的特性建立一个index与value的一一对应关系,由于map的put方法key相同时会覆盖上一次放入的value,所以要先获取上次的key放置的value,然后加上当前相同的key对应的value。由于要按照key进行升序排列,所以选用map时选择TreeMap,默认按升序排列。

import java.util.*;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        Map<Integer,Integer> map = new TreeMap<Integer,Integer>();
        String c = in.nextLine();
        Integer a = Integer.parseInt(c);
        for(int i=0;i<a;i++){
            String b = in.nextLine();
            String[] t = b.split(" ");
            int index = Integer.parseInt(t[0]);
            int value = Integer.parseInt(t[1]);
            if(map.containsKey(index)){
                map.put(index,map.get(index)+value);
            }else{
                map.put(index,value);
            }
        }
        
        map.forEach(
            (key,value)->{
                System.out.println(key+" "+value);
            }
        );
    }
}