#include #define nah_id_win ios_base::sync_with_stdio(0); cin.tie(0) #define ull unsigned long long #define int long long #define pb push_back #define pob pop_back #define pf push_front #define pof pop_front #define fi first #define se second #define all(x) (x).begin(), (x).end() using namespace std; const int MOD = 1e9 + 7; void with_this_treasure_i_summon(string name){ freopen((name + ".in").c_str(), "r", stdin); freopen((name + ".out").c_str(), "w", stdout); } signed main(){ nah_id_win; int n, m; cin >> n >> m; vector l(n); for (auto &x : l){ cin >> x; } vector>> ikan(m); for (auto &[f, bw] : ikan){ int b, w; cin >> f >> b >> w; bw = {b, w}; } int ans = 0; int cost = 0; for (auto &[f, bw] : ikan){ //cout << "ikan-----------------\n"; //cout << "f " << f << "\n"; auto it1 = upper_bound(all(l), f); int shortest = 1e18; if (it1 != l.end()){ //cout << "kekanan " << *it1 - f << "\n"; shortest = min(shortest, *it1 - f); } if (it1 != l.begin()){ auto it2 = prev(it1); //cout << "kekiri " << f - *it2 << "\n"; shortest = min(shortest, f - *it2); } //cout << "bedanya " << bw.se - bw.fi << "\n"; if (shortest <= (bw.se - bw.fi)){ //cout << "gelap\n"; ans += bw.se; cost += shortest; } else{ //cout << "terang\n"; ans += bw.fi; } } cout << ans - cost << "\n"; }