根据题意可知可以先将单价排序然后判断数量是否达到n,若到n则直接+pn,否则+pnum。
#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
int n,m,t;
long long ans;
struct node
{
int p,num;
}a[5010];
int cmp(node x,node y)
{
return x.p<y.p;
}
int main()
{
scanf("%d%d",&n,&m);
for(int i=1;i<=m;i++)
scanf("%d%d",&a[i].p,&a[i].num);
sort(a+1,a+m+1,cmp);
t=1;
while(n)
{
if(n>a[t].num)
{
ans+=a[t].num*a[t].p;
n-=a[t].num;
}
else
{
ans+=a[t].p*n;
n-=n;
}
t++;
}
cout<<ans;
return 0;
}