数据范围:
1 ≤ M ≤ N ≤ 2 × 105
代码1:70pts
#define LOCAL
#include<iostream>
#include<cstdio>
using namespace std;
namespace ly
{
namespace IO
{
#ifndef LOCAL
#define SIZE (1<<20)
char in[SIZE],out[SIZE],*p1=in,*p2=in,*p3=out;
#define getchar() (p1==p2&&(p2=(p1=in)+fread(in,1,SIZE,stdin),p1==p2)?EOF:*p1++)
#define flush() (fwrite(p3=out,1,SIZE,stdout))
#define putchar(ch) (p3==out+SIZE&&flush(),*p3++=(ch))
class Flush{public:~Flush(){flush();}}_;
#endif
template<typename type>
inline void read(type &x)
{
x=0;bool flag(0);char ch=getchar();
while(!isdigit(ch)) flag^=ch=='-',ch=getchar();
while(isdigit(ch)) x=(x<<1)+(x<<3)+(ch^48),ch=getchar();
flag?x=-x:0;
}
template<typename type>
inline void write(type x,bool flag=1)
{
x<0?x=-x,putchar('-'):0;static short Stack[50],top(0);
do Stack[++top]=x%10,x/=10;while(x);
while(top) putchar(Stack[top--]|48);
flag?putchar('\n'):putchar(' ');
}
#ifndef LOCAL
#undef SIZE
#undef getchar
#undef putchar
#undef flush
#endif
}
}using namespace ly::IO;
#include<bits/stdc++.h>
#define int long long
#define LL long long
int m,n;
const int N=2*1e5+10;
int p[N],ans;
bool pd[N];
//char *p1, *p2, buf[N];
//#define nc() (p1 == p2 && (p2 = (p1 = buf) +\
//fread(buf, 1, N, stdin), p1 == p2) ? EOF : *p1 ++ )
//LL read()
//{
// LL x = 0, f = 1;
// char ch = nc();
// while (ch < 48 || ch > 57)
// {
// if (ch == '-') f = -1;
// ch = nc();
// }
// while (ch >= 48 && ch <= 57)
// x = (x << 3) + (x << 1) + (ch ^ 48), ch = nc();
// return x * f;
//}
int main()
{
ios::sync_with_stdio(false);
cin.tie(NULL),cout.tie(NULL);
read(n),read(m);
for(int i=1;i<=n;i++)
{
read(p[i]);
}
sort(p+1,p+1+n);
for(int i=1;i<=m;i++)
{
int b;
read(b);
int k=lower_bound(p+1,p+1+n,b)-p;
while(pd[k]) k++;
if(p[k]<b||k>n)
{
write(-1);
return 0;
}
else
{
ans+=p[k];
pd[k]=1;
}
}
write(ans);
return 0;
}
代码2:100pts:
#define LOCAL
#include<bits/stdc++.h>
#define int long long
using namespace std;
namespace ly
{
namespace IO
{
#ifndef LOCAL
#define SIZE (1<<20)
char in[SIZE],out[SIZE],*p1=in,*p2=in,*p3=out;
#define getchar() (p1==p2&&(p2=(p1=in)+fread(in,1,SIZE,stdin),p1==p2)?EOF:*p1++)
#define flush() (fwrite(p3=out,1,SIZE,stdout))
#define putchar(ch) (p3==out+SIZE&&flush(),*p3++=(ch))
class Flush{public:~Flush(){flush();}}_;
#endif
template<typename type>
inline void read(type &x)
{
x=0;bool flag(0);char ch=getchar();
while(!isdigit(ch)) flag^=ch=='-',ch=getchar();
while(isdigit(ch)) x=(x<<1)+(x<<3)+(ch^48),ch=getchar();
flag?x=-x:0;
}
template<typename type>
inline void write(type x,bool flag=1)
{
x<0?x=-x,putchar('-'):0;static short Stack[50],top(0);
do Stack[++top]=x%10,x/=10;while(x);
while(top) putchar(Stack[top--]|48);
flag?putchar('\n'):putchar(' ');
}
#ifndef LOCAL
#undef SIZE
#undef getchar
#undef putchar
#undef flush
#endif
}
}using namespace ly::IO;
int m,n;
const int N=2e6+10;
int a[N],b[N];
bool pd[N];
long long ans;
signed main()
{
read(n),read(m);
for(int i=1;i<=n;i++)
read(a[i]);
for(int i=1;i<=m;i++)
read(b[i]);
sort(a+1,a+1+n);
sort(b+1,b+1+m);
int k=1;
for(int i=1;i<=m;i++)
{
while(a[k]<b[i]&&k<=n) k++;
while(pd[k])k++;
if(k>n)
{
write(-1);
return 0;
}
ans+=a[k];
pd[k]=1;
}
write(ans);
return 0;
}