链接:
https://www.nowcoder.com/acm/contest/112/A
来源:牛客网
来源:牛客网
时间限制:C/C++ 1秒,其他语言2秒
空间限制:C/C++ 32768K,其他语言65536K
64bit IO Format: %lld
空间限制:C/C++ 32768K,其他语言65536K
64bit IO Format: %lld
题目描述
现有n组人,m个地点,给出每组人的人数,每个地点可容纳的最大人数和选择的价格
要求一种方式,使得每组人都到一个各不相同的地点,最小化选择的价格
每个队伍的人都要在同一个地方每个地方只能有一个队伍
输入描述:
第一行n,m 第二行n个数,表示每组的人数 接下来m行,每行两个数,表示可容纳的最大人数和选择的价格
输出描述:
输出最小化选择的价格,无解输出-1
示例1
输入
3 4 2 3 4 1 2 2 3 3 4 4 5
输出
12
备注:
所有数据小于1e5
#include<bits/stdc++.h> using namespace std; #define ll long long #define rep(i,a,b) for(int i=a;i<=b;i++) ll rd(){ ll x=0,f=1;char ch=getchar(); while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();} while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();} return x*f; } const int N=1e5+10; int a[N]; struct AA{ int p,m; bool operator<(const AA bb){ return p<bb.p; } }b[N]; int main(){ // freopen("in.txt","r",stdin); int n=rd(),m=rd(); rep(i,1,n)a[i]=rd(); sort(a+1,a+1+n); rep(i,1,m) b[i].p=rd(),b[i].m=rd(); sort(b+1,b+1+m); priority_queue<int> q; ll ans=0; for(int i=n,j=m;i>=1;i--){ while(b[j].p>=a[i]&&j>=1){ q.push(-b[j].m); j--; } if(q.empty()) return puts("-1"),0; ans-=q.top(); q.pop(); } printf("%lld\n",ans); return 0; }