62,628
社区成员
发帖
与我相关
我的任务
分享
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
class Goods implements Comparable{
int per;
int count;
//按照降序
@Override
public int compareTo(Object o1) {
Goods o = (Goods)o1;
if(this.per > o.per){
return -1;
}else if(this.per == o.per){
return 0;
}else{
return 1;
}
}
}
public class Main {
public static void main(String[] args) throws NumberFormatException, IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
int N = Integer.parseInt(reader.readLine());
List<Goods> list = new ArrayList<Goods>();
while((N--)!=0){
String str[] = reader.readLine().split(" ");
int n = Integer.parseInt(str[0]);
int weight = Integer.parseInt(str[1]);
while((n--)!=0){
Goods good = new Goods();
str = reader.readLine().split(" ");
good.per = Integer.parseInt(str[0]);
good.count = Integer.parseInt(str[1]);
list.add(good);
}
//开始排序
Collections.sort(list);
//开始计算
int count = 0;
int add_weight = 0;
for(int i=0;i<list.size();i++){
if(list.get(i).count <=(weight-add_weight)){
count+= list.get(i).per * list.get(i).count;
add_weight+=list.get(i).count;
}else{
count+= list.get(i).per * (weight-add_weight);
add_weight += (weight-add_weight);
}
if(add_weight >= weight){
break;
}
}
System.out.println(count);
}
reader.close();
}
}