#include #include using namespace std; //#include typedef long long ll; const int max_defend = 12; const int max_val = 1010; const int maxn = 100010; const int INF = 1000000007; int dp[max_defend][max_val]; int a[maxn], b[maxn]; int costs[maxn], vals[maxn]; void init(){ //memset(dp, -1, sizeof(dp)); for(int i = 0; i < max_defend; i++){ dp[i][0] = 0; for(int j = 1; j < max_val; j++) dp[i][j] = INF; } } int main(){ // freopen("in.txt", "r", stdin); int n, m; while(scanf("%d %d", &n, &m) != EOF){ for(int i = 0; i < n; i++) scanf("%d %d", &a[i], &b[i]); init(); int cost, val; for(int k = 0; k < m; k++){ scanf("%d %d", &costs[k], &vals[k]); } for(int k = 0; k < m; k++){ int cost = costs[k], val=vals[k]; int upper = 10; if(val <= upper) upper = val - 1; for(int i = 0; i <= upper; i++){ int delta = val - i; for(int j = 1; j <= delta; j++){ dp[i][j] = min(dp[i][j], cost); //dp[i][j] = min(dp[i][j], dp[i][j-delta] + cost); } for(int j = delta+1; j < max_val; j++){ //if(j - delta <= 0) dp[i][j] = min(dp[i][j], cost); dp[i][j] = min(dp[i][j], dp[i][j-delta] + cost); } } } bool ok = true; ll ans = 0; for(int i = 0; i < n; i++){ if(dp[b[i]][a[i]] == INF) { ok = false; break; } ans += dp[b[i]][a[i]]; } if(ok) printf("%lld\n", ans); else printf("-1\n"); } return 0; }