intmain(){ ios::sync_with_stdio(false); while (cin >> n >> m) { memset(num, 0, sizeof(num)); LL ipt1, ipt2; for (int i = 0; i < n; ++i) { cin >> ipt1 >> ipt2; ++num[ipt1][ipt2]; } for (int i = 0; i < m; ++i) { cin >> k[i] >> p[i]; }
LL res = 0; for (int b = 0; b <= 10; ++b) { memset(dp, 0x7f, sizeof(dp)); dp[0] = 0; bool fail = true; for (int c = 0; c < m; ++c) { LL atk = p[c] - b; if (atk <= 0) { continue; } fail = false; for (int a = 1; a <= 1000; ++a) { if (a - atk <= 0)dp[a] = min(dp[a], k[c]); else { dp[a] = min(dp[a], dp[a - atk] + k[c]); } } } if (fail) { for (int i = 1; i <= 1000; ++i) { if (num[i][b]) goto NOO; } } else { for (int i = 1; i <= 1000; ++i) { res += num[i][b] * dp[i]; } } } cout << res << '\n'; continue; NOO:cout << -1 << '\n'; } return0; }