#include using namespace std; #define ll long long const ll MOD = 1000000007; const ll MOD2 = 998244353; int main() { cin.tie(NULL); cout.tie(NULL); ios::sync_with_stdio(false); ll n, m; cin >> n >> m; ll tree[n], light[m], dark[m], fish[m]; for (int i = 0; i> tree[i]; } for (int i = 0; i < m; i++) { cin >> fish[i] >> light[i] >> dark[i]; } sort(tree, tree+n); ll total; for (int i = 0; i < m; i++) { ll closest = 0; if (fish[i] < tree[0]) closest = abs(tree[0] - fish[i]); else if (fish[i] > tree[n-1]) closest = abs(tree[n-1] - fish[i]); else { ll mid, left = 0, right = n-1; while (left <= right) { mid = (left + right) / 2; if (tree[mid] == fish[i]) { closest = 0; break; } else if (fish[i] < tree[mid]) { if (fish[i] > tree[mid-1]) { closest = min(abs(fish[i] - tree[mid-1]), abs(fish[i] - tree[mid])); break; } right = mid - 1; } else if (fish[i] > tree[mid]) { if (fish[i] < tree[mid+1]) { closest = min(abs(fish[i] - tree[mid+1]), abs(fish[i] - tree[mid])); break; } left = mid + 1; } } } total += max(dark[i] - closest, light[i]); } cout << total; }