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 <bits/stdc++.h>
using namespace std;
long long N,M,Q;
vector<long long> lst[505];
int main() {
    ios_base::sync_with_stdio(false); cin.tie(NULL);
    cin >> N >> M;
    for (long long i = 0; i < M; ++i) {
        long long u,v,w; cin >> u >> v >> w; u--, v--;
        lst[u].push_back(w);
    }
    for (long long i = 0; i < N; ++i) sort(lst[i].begin(),lst[i].end());
    cin >> Q;
    while (Q--) {
        long long x; cin >> x;
        long long ans = 0;
        for (long long i = 0; i < N-1; ++i) {
            auto it = lower_bound(lst[i].begin(),lst[i].end(),x);
            long long val = 4e18;
            if (it != lst[i].end()) val = min(val,*it - x);
            if (it != lst[i].begin()) val = min(val,x-*prev(it));
            ans += val;
        }
        cout << ans << '\n';
    }
    return 0;
}
| # | 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... | 
| # | Verdict  | Execution time | Memory | Grader output | 
|---|
| Fetching results... |