#include #include #define maxa 1000 //#define maxa 15 #define maxb 10 #define maxn 100000 #define maxm 1000 #define maxf 100000001 //maxp(100000)*maxa(1000) //maxa*maxb*maxm*case long f[maxb+1][maxa+1]; long a[maxn+1],b[maxn+1]; long p[maxm+1],q[maxm+1],r[maxm+1]; long max(long a,long b) { if (a>b) return a; else return b; } long min(long a,long b) { if (a>b) return b; else return a; } int main() { long n,m,i,j,k; long bb,qq; __int64 ans; while (scanf("%ld%ld",&n,&m)!=EOF) { bb=0; qq=0; for (i=1;i<=n;i++) { scanf("%ld%ld",&a[i],&b[i]); bb=max(bb,b[i]); } for (i=1;i<=m;i++) { scanf("%ld%ld",&p[i],&q[i]); qq=max(qq,q[i]); } if (bb>=qq) { printf("-1\n"); continue; } for (i=0;i<=bb;i++) { f[i][0]=0; for (j=1;j<=maxa;j++) f[i][j]=maxf; for (k=1;k<=m;k++) r[k]=q[k]-i; for (j=1;j<=maxa;j++) for (k=1;k<=m;k++) if (r[k]>0) { if (j-r[k]<0) f[i][j]=min(f[i][j],p[k]); else f[i][j]=min(f[i][j],f[i][j-r[k]]+p[k]); } } ans=0; for (i=1;i<=n;i++) ans=ans+f[b[i]][a[i]]; printf("%I64d\n",ans); } return 0; } /* 1 2 4 5 7 10 6 8 7 */ /* 1 2 6 5 7 10 6 8 12 */