func_code_string: #include <bits/stdc++.h>   using namespace std;   const int MOD = (int)1e9 + 7;   const int INF = (int)1e9;   const long long LINF = (long long)1e18;   const long double PI = 2 * acos((long double)0);   long long gcd(long long a, long long b) {     long long r;     while (b) {       r = a % b;       a = b;       b = r;     }     return a;   }   long long lcm(long long a, long long b) { return a / gcd(a, b) * b; }   long long fpow(long long n, long long k, int p = MOD) {     long long r = 1;     for (; k; k >>= 1) {       if (k & 1) r = r * n % p;       n = n * n % p;     }     return r;   }   template <class T>   void setmin(T& a, T val) {     if (a > val) a = val;   }   template <class T>   void setmax(T& a, T val) {     if (a < val) a = val;   }   void addmod(int& a, int val, int p = MOD) {     if ((a = (a + val)) >= p) a -= p;   }   void submod(int& a, int val, int p = MOD) {     if ((a = (a - val)) < 0) a += p;   }   int mult(int a, int b, int p = MOD) { return (long long)a * b % p; }   int inv(int a, int p = MOD) { return fpow(a, p - 2, p); }   const int maxn = 200010;   long long n, d, m;   long long x[maxn];   long long p[maxn];   long long st[4 * maxn];   void build(int node, int L, int R) {     if (L == R) {       st[node] = p[L];       return;     }     build(node << 1, L, (L + R) >> 1);     build((node << 1) + 1, ((L + R) >> 1) + 1, R);     st[node] = min(st[node << 1], st[(node << 1) + 1]);   }   long long query(int node, int l, int r, int L, int R) {     if (l > R || r < L) return LINF;     if (l <= L && r >= R) return st[node];     return min(query(node << 1, l, r, L, (L + R) >> 1),                query((node << 1) + 1, l, r, ((L + R) >> 1) + 1, R));   }   void solve() {     cin >> d >> n >> m;     vector<pair<int, int> > v;     for (int i = (1); i < (m + 1); i++) {       cin >> x[i] >> p[i];       v.push_back(make_pair(x[i], p[i]));     }     sort((v).begin(), (v).end());     for (int i = (1); i < (m + 1); i++) {       x[i] = v[i - 1].first;       p[i] = v[i - 1].second;     }     x[m + 1] = d;     build(1, 0, m + 1);     int cur = 0;     long long ans = 0, r = 0;     while (cur < m + 1) {       if (x[cur] + n < x[cur + 1]) {         cout << -1;         return;       }       int lo = cur + 1, hi = upper_bound(x, x + m + 2, x[cur] + n) - x - 1;       while (lo < hi) {         int mid = (lo + hi) >> 1;         if (query(1, cur + 1, mid, 0, m + 1) > p[cur])           lo = mid + 1;         else           hi = mid;       }       if (query(1, cur + 1, lo, 0, m + 1) <= p[cur]) {         ans += p[cur] * max(0LL, x[lo] - x[cur] - r);         r = max(0LL, x[cur] + r - x[lo]);         cur = lo;       } else {         ans += (n - r) * p[cur];         r = x[cur] + n - x[cur + 1];         cur++;       }     }     cout << ans;   }   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     solve();     return 0;   }   