Welcome to Debug.
import java.io.*;
import java.util.*;
import static java.lang.System.in;
public class Main {
static long[][] dp = new long[2200][15];
static int[] a = new int[100110];
static int[] b = new int[100110];
static long[] k = new long [2200];
static int[] p = new int [2200];
public static void main(String[] args) throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer tokenizer = new StringTokenizer("");
tokenizer = new StringTokenizer(reader.readLine());
while (tokenizer.hasMoreTokens()) {
long result = 0;
int n = Integer.parseInt(tokenizer.nextToken());
if (!tokenizer.hasMoreTokens()) {
return;
}
int m = Integer.parseInt(tokenizer.nextToken());
int maxA = 0;
int maxB = 0;
for (int i = 0; i < n; i++) {
tokenizer = new StringTokenizer(reader.readLine());
a[i] = Integer.parseInt(tokenizer.nextToken());
b[i] = Integer.parseInt(tokenizer.nextToken());
maxA = Math.max(maxA, a[i]);
maxB = Math.max(maxB, b[i]);
}
for (int i = 0; i < m; i++) {
tokenizer = new StringTokenizer(reader.readLine());
k[i] = Integer.parseInt(tokenizer.nextToken());
p[i] = Integer.parseInt(tokenizer.nextToken());
}
for (int i = 0; i < dp.length; i++) {
for (int j = 0; j < dp[0].length; j++) {
if (i == 0) {
dp[i][j] = 0;
} else {
dp[i][j] = Long.MAX_VALUE;
}
}
}
for(int i = 0; i <= 11; i++) {
// dp[0][i] = 0;
for (int j = 0; j < m; j++) {
int attack = p[j] - i;
if (attack <= 0) {
continue;
}
for (int hp = 1; hp <= maxA; hp++) {
if (hp - attack <= 0) {
dp[hp][i] = Math.min(dp[hp][i], k[j]);
} else {
dp[hp][i] = Math.min(dp[hp][i], dp[hp - attack][i] + k[j]);
}
}
}
}
boolean flag = false;
for (int i = 0; i < n; i++) {
flag = false;
for (int t = 0; t < m; t++) {
if (p[t] > b[i]) {
flag = true;
break;
}
}
result += dp[a[i]][b[i]];
if (flag == false) {
break;
}
}
if (flag == false) {
System.out.println(-1);
} else {
System.out.println(result);
}
tokenizer = new StringTokenizer(reader.readLine());
}
}
}