#include #include #include #include using namespace std; void Read(long long &p) { p=0; char c=getchar(); while(c<'0'||c>'9') c=getchar(); while(c>='0'&&c<='9') p=p*10+c-'0',c=getchar(); } const int MAXN=202017; const int MAXM=2027; long long n,m,hp[MAXN],pro[MAXN],mp[MAXM],atk[MAXM]; long long dp[12][MAXM],mxatk=-1,mxpro=-1,mxhp=-1; int main() { while(cin>>n>>m) { mxpro=-1,mxatk=-1,mxhp=-1; for(int i=1;i<=n;i++) Read(hp[i]),Read(pro[i]), mxpro=max(mxpro,pro[i]), mxhp=max(mxhp,hp[i]); for(int i=1;i<=m;i++) Read(mp[i]),Read(atk[i]), mxatk=max(mxatk,atk[i]); if(mxatk<=mxpro) { puts("-1"); continue; } memset(dp,0,sizeof dp); for(int i=0;i<=mxpro;i++) for(int j=1;j<=mxhp;j++) { dp[i][j]=0x7ffffff7; for(int k=1;k<=m;k++) { int sub=atk[k]-i; if(sub>=j) dp[i][j]=min(dp[i][j],mp[k]); else if(sub>0) dp[i][j]=min(dp[i][j],dp[i][j-sub]+mp[k]); } } long long ans=0; for(int i=1;i<=n;i++) ans+=dp[pro[i]][hp[i]]; cout<