#include #include #include #include #include #include using namespace std; int n, m; vector a, b, k, p; int cache[11][1001]; int calc(int b) { vector np; for (int i = 0; i < m; ++i) { np.push_back(p[i] - b); } int M = 100000001; for (int i = 1; i <= 1000; ++i) { int t = M; for (int j = 0; j < np.size(); ++j) if (np[j] > 0) { int t1 = k[j]; if (i > np[j]) { t1 += cache[b][i - np[j]]; } if (t1 < t) t = t1; } cache[b][i] = t; } for (int i = 1; i <= 1000; ++i) { if (cache[b][i] >= M) cache[b][i] = -1; } } void process() { a.resize(n); b.resize(n); for (int i = 0; i < n; ++i) { cin >> a[i] >> b[i]; } k.resize(m); p.resize(m); for (int i = 0; i < m; ++i) { cin >> k[i] >> p[i]; } for (int b = 0; b <= 10; ++b) { calc(b); } long long ans = 0; for (int i = 0; i < n; ++i) { int t = cache[b[i]][a[i]]; if (t == -1) { cout << -1 << endl; return; } ans += t; } cout << ans << endl; /* int n; cin >> n; vector nums(n); string s; // NOTE: if using getline() to read the input, the following two lines should be // added to read the line sepeartor in the first line. getline(cin, s); getline(cin, s); stringstream S(s); */ } int main(void) { while (cin >> n >> m) { process(); } return 0; }