#include <bits/stdc++.h>   using namespace std;   const int MAXN = 1000100;   struct Vertex {     long long cnt;     long long sum;     Vertex() { cnt = sum = 0; }   };   struct SegmentTree {     Vertex tree[4 * MAXN];     SegmentTree() = default;     void calc(int v) {       int lv = 2 * v + 1;       int rv = 2 * v + 2;       tree[v].cnt = tree[lv].cnt + tree[rv].cnt;       tree[v].sum = tree[lv].sum + tree[rv].sum;     }     void modify(int v, int l, int r, int pos, long long c) {       if (l == r) {         tree[v].cnt += c;         tree[v].sum += c * (long long)pos;         return;       }       int med = (l + r) / 2;       if (pos <= med) {         modify(2 * v + 1, l, med, pos, c);       } else {         modify(2 * v + 2, med + 1, r, pos, c);       }       calc(v);     }     long long rec(int v, int l, int r, long long need) {       if (tree[v].cnt <= need) {         return tree[v].sum;       }       if (l == r) {         return (long long)l * (long long)need;       }       int lv = 2 * v + 1;       int rv = 2 * v + 2;       int med = (l + r) / 2;       if (tree[lv].cnt >= need) {         return rec(lv, l, med, need);       } else {         long long rest = need - tree[lv].cnt;         long long L = rec(lv, l, med, need);         long long R = rec(rv, med + 1, r, rest);         return L + R;       }     }   };   vector<pair<int, int>> events[MAXN];   SegmentTree tr;   int main() {     int n, k, m;     scanf( %d%d%d , &n, &k, &m);     for (int i = 1; i <= m; i++) {       int l, r, c, p;       scanf( %d%d%d%d , &l, &r, &c, &p);       events[l].emplace_back(p, c);       events[r + 1].emplace_back(p, -c);     }     long long ans = 0;     for (int i = 1; i <= n; i++) {       for (auto& ev : events[i]) {         tr.modify(0, 1, 1000000, ev.first, ev.second);       }       long long result = tr.rec(0, 1, 1000000, k);       ans += result;     }     cout << ans << endl;     return 0;   }   