#include #include #include #include #include #define MAXN 100010 #define MAXM 3010 #define INF 0x3F3F3F3F using namespace std; typedef long long i64; int a[MAXN], b[MAXN], k[MAXM], p[MAXM], dp[12][MAXM]; void Calc(int n, int d, int* dp) { fill(dp, dp + MAXM, INF); dp[0] = 0; for (int i = 0; i < n; ++i) { if (p[i] <= d) continue; for (int j = p[i] - d; j < MAXM; ++j) { dp[j] = min(dp[j], dp[j - p[i] + d] + k[i]); } } for (int i = MAXM - 2; i; --i) { dp[i] = min(dp[i], dp[i + 1]); } } i64 Solve(int n, int m) { for (int i = 0; i <= 10; ++i) { Calc(m, i, dp[i]); } i64 ret = 0; for (int i = 0; i < n; ++i) { if (dp[b[i]][a[i]] >= INF) { return -1; } ret += (i64)dp[b[i]][a[i]]; } return ret; } int main() { int n, m; while (~scanf("%d%d", &n, &m)) { for (int i = 0; i < n; ++i) { scanf("%d%d", &a[i], &b[i]); } for (int i = 0; i < m; ++i) { scanf("%d%d", &k[i], &p[i]); } i64 ans = Solve(n, m); cout << ans << endl; } return 0; }