import java.util.*; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int m, n; m = sc.nextInt(); n = sc.nextInt(); List<Map<String, Integer>> shopList = new ArrayList<>(); for (int i = 1; i <= n; i++) { Map<String, Integer> shop = new HashMap<>(); shop.put("price", sc.nextInt()); shop.put("count", sc.nextInt()); shopList.add(shop); } //容器排序 Collections.sort(shopList, new Comparator<Map<String, Integer>>() { @Override public int compare(Map<String, Integer> o1, Map<String, Integer> o2) { //如果前一个 price > 后一个 price,则交换这两个map return o1.get("price") - o2.get("price"); } }); //总共需要购买 m 个贺卡,m 还需要购买的贺卡个数 int money = 0; for (int i = 0; i < shopList.size(); i++) { //第 i 间商铺 Map<String, Integer> shop = shopList.get(i); if( shop.get("count") >= m){ //不需要这么多了 money = money + m * shop.get("price"); m = 0; break; }else{ money = money + shop.get("count") * shop.get("price"); m = m - shop.get("count"); } } System.out.print(money); sc.close(); } } /************************************************************** Problem: 1730 User: admin Language: Java Result: Accepted Time:2253 ms Memory:40664 kb ****************************************************************/