#pragma comment(linker, "/STACK:1024000000,1024000000") #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #define debug() puts("++++"); #define gcd(a, b) __gcd(a, b) #define lson l,m,rt<<1 #define rson m+1,r,rt<<1|1 #define freopenr freopen("in.txt", "r", stdin) #define freopenw freopen("out.txt", "w", stdout) using namespace std; typedef long long LL; typedef unsigned long long ULL; typedef pair P; const int INF = 0x3f3f3f3f; const LL LNF = 1e16; const double inf = 0x3f3f3f3f3f3f; const double PI = acos(-1.0); const double eps = 1e-8; const int maxn = 1e5 + 10; const int mod = 1e9 + 7; const int dr[] = {-1, 0, 1, 0}; const int dc[] = {0, 1, 0, -1}; const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"}; int n, m; const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; inline bool is_in(int r, int c){ return r >= 0 && r < n && c >= 0 && c < m; } int dp[1005][12]; int a[maxn], b[maxn]; struct Node{ int k, p; Node(){ } Node(int kk, int pp) : k(kk), p(pp) { } bool operator < (const Node &rhs) const{ return p < rhs.p; } }; Node c[1005]; int main(){ while(scanf("%d %d", &n, &m) == 2){ int t = 0, tt = 0; for(int i = 0; i < n; ++i){ scanf("%d %d", a+i, b+i); t = max(t, b[i]); tt = max(tt, a[i]); } for(int j = 0; j < m; ++j) scanf("%d %d", &c[j].k, &c[j].p); sort(c, c + m); if(c[m-1].p <= t){ printf("-1\n"); continue; } // int v; // while(cin >> v){ // printf("%d\n", upper_bound(c, c + m, Node(0, v)) - c); // } // for(int j = ) memset(dp, INF, sizeof dp); for(int k = 0; k <= t; ++k){ dp[0][k] = 0; int pp = upper_bound(c, c + m, Node(-1, k)) - c; for(int i = pp; i < m; ++i){ for(int j = 1; j <= tt; ++j) dp[j][k] = min(dp[j][k], dp[max(0, j-c[i].p+k)][k] + c[i].k); } } LL ans = 0; for(int i = 0; i < n; ++i) ans += dp[a[i]][b[i]]; printf("%I64d\n", ans); } return 0; }