#include #include #include using namespace std; typedef long long ll; int k[1005],p[1005],a[100005],b[100005]; ll dp[1005][12]; const int inf=1e9+7; inline void read(int &x) { x=0;char p=getchar(); while(!(p<='9'&&p>='0'))p=getchar(); while(p<='9'&&p>='0') x*=10,x+=p-48,p=getchar(); } int main() { int n,m; while(~scanf("%d%d",&n,&m)) { int mb=0,ma=0; for(int i=1;i<=n;i++) { read(a[i]);read(b[i]); mb=max(mb,b[i]); ma=max(ma,a[i]); } for(int i=1;i<=m;i++) { read(k[i]); read(p[i]);} for(int i=0;i<=mb;i++) { dp[0][i]=0; for(int j=1;j<=ma;j++) { dp[j][i]=inf; for(int q=1;q<=m;q++) { if(p[q]<=i) continue; int t=j-p[q]+i; if(t<0) t=0; dp[j][i]=min(dp[j][i],dp[t][i]+k[q]); } } } ll ans=0; for(int i=1;i<=n;i++) if(dp[a[i]][b[i]]==inf) { ans=-1;break; } else ans +=dp[a[i]][b[i]]; printf("%I64d\n",ans); } return 0; }