#include #define N 100010 #define M 1010 using namespace std; int a[N], b[N], k[M], p[M], f[11][M]; int main() { int n, m; while(scanf("%d%d", &n, &m) != EOF) { for(int i = 1; i <= n; i++) scanf("%d%d", &a[i], &b[i]); for(int i = 1; i <= m; i++) scanf("%d%d", &k[i], &p[i]); for(int B = 0; B <= 10; B++) { memset(f[B], 0x3f, sizeof(f[B])); f[B][0] = 0; for(int i = 1; i <= m; i++) { int now = p[i] - B; if(now <= 0)continue; for(int j = 1; j <= 1000; j++) f[B][j] = min(f[B][j], f[B][max(j - now, 0)] + k[i]); } } int flag = 1; long long ans = 0; for(int i = 1; i <= n; i++) { if(f[b[i]][a[i]] == 0x3f3f3f3f) { flag = 0; break; } ans += f[b[i]][a[i]]; } if(!flag)puts("-1"); else cout << ans << endl; } }