https://www.luogu.com.cn/problem/P1208
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <sstream>
#include <algorithm>
#include <cstring>
#include <vector>
using namespace std;
template<typename DataType>
DataType qmi(DataType m, int k)
{
DataType res = 1, t = m;
while (k)
{
if (k&1) res = res * t;
t = t * t;
k >>= 1;
}
return res;
}
int read(){
int x = 0,f = 1;
char c = getchar();
while (c<'0'||c>'9') {
if (c=='-') {
f = -1;
}
c = getchar();
}
while (c>='0'&&c<='9') {
x = x*10+c-'0';
c = getchar();
}
return x*f;
}
#define fi(a,b) for(int i = a; i <= b; i++)
#define fj(a,b) for(int j = a; j >= b; j--)
struct priceAndCnt{
int price,cnt;
};
void quickSort(priceAndCnt *a,int left,int right){
int i,j;
priceAndCnt temp,t;
temp = a[(left+right)/2];//基准值
i = left;
j = right;
while(i<=j){
while (a[j].price > temp.price) {
j--;
}
while (a[i].price < temp.price) {
i++;
}
if (i<=j) {
t = a[i];
a[i] = a[j];
a[j] = t;
//继续下一步
i++;
j--;
}
}
if(left<j)quickSort(a,left, j);//继续分治
if(i<right)quickSort(a,i, right);//继续分治
}
int m,n,ans = 0;
priceAndCnt arr[2000005];
int main(){
//输入部分
n = read();
m = read();
fi(1, m){
arr[i].price = read();
arr[i].cnt = read();
}
//排序,然后一一匹配
quickSort(arr, 1, m);
// fi(1, m){
// cout<<arr[i].price<<"\t"<<arr[i].cnt<<endl;
// }
fi(1, m){
while(n && arr[i].cnt){
n --;
arr[i].cnt --;
ans += arr[i].price;
}
}
cout<<ans;
}
/*
100 5
5 20
9 40
3 10
8 80
6 30
============
6
*/
网友评论