This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <iostream>
#include <algorithm>
#include <vector>
#include <tuple>
#include <queue>
using namespace std;
int main(){
    ios::sync_with_stdio(0);
    cin.tie(0);
    int ans = 0;
    int truck_loaded = 0;
    int truck_loaded_dest[10001];
    int N, C, M, a, b, c;
    cin >> N >> C;
    cin >> M;
    vector<tuple<int, int, int>> v;
    priority_queue<int> pq;
    for(int i = 0; i < M; i++){
        cin >> a >> b >> c;
        v.push_back(make_tuple(a, b, c));
    }
    sort(v.begin(), v.end());
    for(int i = 0; i < M; i++){
        tie(a, b, c) = v[i];
        int cur_loaded = 0;
        if(i && (a != get<0>(v[i-1]))){
            for(int j = get<0>(v[i-1])+1; j <= a; j++){
                ans += truck_loaded_dest[j];
                truck_loaded -= truck_loaded_dest[j];
                truck_loaded_dest[j] = 0;
            }
        }
        if(truck_loaded+c <= C){
            if(!truck_loaded_dest[b])
                pq.push(b);
            truck_loaded += c;
            truck_loaded_dest[b] += c;
            continue;
        }
        if(truck_loaded+c > C && truck_loaded < C){
            if(!truck_loaded_dest[b])
                pq.push(b);
            truck_loaded_dest[b] += C-truck_loaded;
            cur_loaded = C-truck_loaded;
            truck_loaded = C;
        }
        if(truck_loaded == C){
            while(!pq.empty() && pq.top() > b && cur_loaded < c){
                int biggest = pq.top();
                if(truck_loaded_dest[biggest] <= c-cur_loaded){
                    if(!truck_loaded_dest[b])
                        pq.push(b);
                    truck_loaded_dest[b] += truck_loaded_dest[biggest];
                    cur_loaded += truck_loaded_dest[biggest];
                    truck_loaded_dest[biggest] = 0;
                    pq.pop();
                }
                else{
                    if(!truck_loaded_dest[b])
                        pq.push(b);
                    truck_loaded_dest[b] += c-cur_loaded;
                    truck_loaded_dest[biggest] -= c-cur_loaded;
                    cur_loaded = c;
                }
            }
        }
    }
    cout << ans+truck_loaded << '\n';
}
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... | 
| # | Verdict | Execution time | Memory | Grader output | 
|---|
| Fetching results... |