#include #include #include #include using namespace std; typedef long long ll; const int maxw = 1e3 + 5 , maxb = 10 + 5; int n , m; ll s[maxb][maxw] , f[maxb][maxw]; int main() { while (~scanf("%d %d" , &n , &m)) { for (int i = 0; i <= 10; i++) { f[i][0] = s[i][0] = 0; for (int j = 1; j <= 1000; j++) f[i][j] = INT_MAX , s[i][j] = 0; } int mb = 0 , ma = 0; for (int i = 1; i <= n; i++) { int a , b; scanf("%d %d" , &a , &b); ma = max(ma , a); mb = max(mb , b); s[b][a]++; } for (int k = 1; k <= m; k++) { int w , v; scanf("%d %d" , &v , &w); for (int i = 0; i <= mb; i++) if (w > i) for (int j = 1; j <= ma; j++) { int from = j + i - w; if (from < 0) from = 0; f[i][j] = min(f[i][j] , f[i][from] + v); } } ll ans = 0; bool flag = false; for (int i = 0; i <= mb; i++) for (int j = 0; j <= ma; j++) if (s[i][j]) { if (f[i][j] == INT_MAX) { ans = -1; goto num; } ans += s[i][j] * f[i][j]; } num:cout << ans << endl; } return 0; }