要求有序所以使用TreeMap而不是Map
import java.util.*;
public class Main{
public static void main(String[] args){
Scanner in = new Scanner(System.in);
int n = in.nextInt();
TreeMap<Integer,Integer> map = new TreeMap<>();
for(int i=0;i<n;i++){
int a = in.nextInt();
int b = in.nextInt();
map.put(a,map.getOrDefault(a,0)+b);
}
for(Map.Entry entry:map.entrySet()){
System.out.println(entry.getKey()+" "+entry.getValue());
}
}
}