#include using namespace std; #ifdef tabr #include "library/debug.cpp" #else #define debug(...) #endif int main() { ios::sync_with_stdio(false); cin.tie(0); int n, m; while (cin >> n >> m) { vector a(n), b(n); for (int i = 0; i < n; i++) { cin >> a[i] >> b[i]; } vector k(m), p(m); for (int i = 0; i < m; i++) { cin >> k[i] >> p[i]; } const int inf = (int) 1e9; vector> c(11, vector(2501, inf)); for (int x = 0; x <= 10; x++) { c[x][0] = 0; for (int i = 0; i < m; i++) { if (p[i] <= x) { continue; } for (int y = 0; y <= 2500 - (p[i] - x); y++) { c[x][y + (p[i] - x)] = min(c[x][y + (p[i] - x)], c[x][y] + k[i]); } } for (int i = 2499; i >= 0; i--) { c[x][i] = min(c[x][i], c[x][i + 1]); } } long long ans = 0; for (int i = 0; i < n; i++) { if (c[b[i]][a[i]] == inf) { ans = -1; break; } ans += c[b[i]][a[i]]; } cout << ans << '\n'; } return 0; }