#include #include #include #include #include #include using namespace std; const int MAXN = 100000 + 100; const int MAXM = 1000 + 100; const int INF = 0x3f3f3f3f; int n, m, M; int h[MAXN], d[MAXN], c[MAXN], a[MAXN]; int s[MAXN]; int f[MAXM][MAXM]; long long ans = 0; void init() { ans = 0, M = 0; memset(f, 63, sizeof(f)); } void solve() { f[0][0] = 0; for(int i = 1; i <= m; i++) for(int j = 0; j <= M; j++) { f[i][j] = f[i - 1][j]; if(s[i] > 0) f[i][j] = min(f[i][j], (j - s[i] >= 0 ? f[i][j - s[i]] : 0) + c[i]); } } void work() { init(); for(int i = 1; i <= n; i++) cin >> h[i] >> d[i]; for(int i = 1; i <= m; i++) cin >> c[i] >> a[i]; for(int def = 0; def <= 10; def++) { for(int i = 1; i <= m; i++) s[i] = a[i] - def; M = 0; for(int i = 1; i <= n; i++) if(d[i] == def) M = max(M, h[i]); solve(); if(f[m][M] >= INF) { cout << -1 << endl; return ; } for(int i = 1; i <= n; i++) if(d[i] == def) ans += f[m][h[i]]; } cout << ans << endl; } int main() { ios::sync_with_stdio(false); // freopen("1.in", "r", stdin); // freopen("1.out", "w", stdout); while(cin >> n >> m) work(); return 0; }