输入输出样例
输入 #1
100 5
5 20
9 40
3 10
8 80
6 30
输出 #1
630
import java.util.Arrays;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while (sc.hasNext()) {
int m = sc.nextInt();
int n = sc.nextInt();
int x[] = new int[n]; //用于存储排序后的数据
int y[][] = new int[n][2]; //存储原数据
int z[] = new int[n]; //识别是否买过;
long sum = 0;
for (int i = 0; i < n; i++) {
y[i][1] = sc.nextInt();
y[i][0] = sc.nextInt();
x[i] = y[i][1];
}
Arrays.sort(x);
TO:
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) { //双层循环用于找到该单价情况下的奶牛数
if (x[i] == y[j][1] && z[j] == 0) {
if (m >= y[j][0]) {
z[j] = 1;
sum += y[j][1] * y[j][0];
m -= y[j][0];
break; //结束本次内循环
} else {
z[j] = 1;
sum += y[j][1] * m;
m = 0;
break TO; //结束整个双层循环
}
}
}
}
System.out.println(sum);
}
}
}