func_code_string: #include <bits/stdc++.h>   using namespace std;   struct data {     int x, p;     bool operator<(const data &b) const { return x < b.x; }   } a[200010];   int seg[200010 * 4];   int query(int l, int r, int from, int to, int x, int p) {     if (l == from && r == to)       if (seg[p] > x) return 0;     if (l + 1 == r) return l;     int m = l + r >> 1, t;     if (to <= m) return query(l, m, from, to, x, p << 1);     if (from >= m) return query(m, r, from, to, x, p << 1 | 1);     t = query(l, m, from, m, x, p << 1);     if (t) return t;     return query(m, r, m, to, x, p << 1 | 1);   }   void build(int l, int r, int p) {     if (l + 1 == r) {       seg[p] = a[l].p;       return;     }     build(l, l + r >> 1, p << 1);     build(l + r >> 1, r, p << 1 | 1);     seg[p] = min(seg[p << 1], seg[p << 1 | 1]);   }   int main() {     int d, n, m, i;     long long ans = 0;     cin >> d >> n >> m;     for (i = 1; i <= m; i++) {       scanf( %d%d , &a[i].x, &a[i].p);     }     sort(a + 1, a + 1 + m);     a[0].x = 0;     a[m + 1].x = d, a[m + 1].p = -1;     for (i = 1; i <= m + 1; i++)       if (a[i].x - a[i - 1].x > n) ans = -1;     build(1, m + 2, 1);     if (ans != -1) {       int now, j, nex;       for (now = n - a[1].x, i = j = 1; i <= m;) {         while (j <= m && a[j + 1].x - a[i].x <= n) j++;         nex = query(1, m + 2, i + 1, j + 1, a[i].p, 1);         if (nex) {           if (a[nex].x - a[i].x <= now) {             now -= a[nex].x - a[i].x;           } else {             ans += ((long long)a[nex].x - a[i].x - now) * a[i].p;             now = 0;           }           i = nex;         } else {           ans += ((long long)n - now) * a[i].p;           now = n - (a[i + 1].x - a[i].x);           i++;         }       }     }     cout << ans;     return 0;   }   