#include #include #include #include #include #define ll long long #define inf 1e9 #define eps 1e-10 #define md #define N 1010 using namespace std; int a[100010],b[100010]; int c[N],d[N],f[13][N],mx[13]; int main() { int m,n; while (scanf("%d%d",&n,&m)!=EOF) { //memset(mx,0,sizeof(mx)); for (int i=1;i<=n;i++) { scanf("%d%d",&a[i],&b[i]); /// mx[b[i]]=max(mx[b[i]],a[i]); } for (int j=1;j<=m;j++) scanf("%d%d",&c[j],&d[j]); for (int fen=0;fen<=10;fen++) { int *g=f[fen]; for (int i=0;i<=2000;i++) g[i]=inf; g[0]=0; for (int i=1;i<=m;i++) { int sz=d[i]-fen; if (sz<=0) continue; for (int v=sz;v<=2000;v++) g[v]=min(g[v],g[v-sz]+c[i]); } for (int v=1999;v>=0;v--) g[v]=min(g[v],g[v+1]); } ll ans=0; bool ok=1; for (int i=1;i<=n;i++) if (f[b[i]][a[i]]