#include using namespace std; int findClosest(int arr[], int n, int target) { if (n == 1) return arr[0]; if (target <= arr[0]) return arr[0]; if (target >= arr[n - 1]) return arr[n - 1]; int i = 0, j = n - 1, mid = 0; while (i < j) { mid = (i + j) / 2; if (arr[mid] == target) return arr[mid]; if (target < arr[mid]) { if (mid > 0 && target > arr[mid - 1]) return (target - arr[mid - 1] >= arr[mid] - target) ? arr[mid] : arr[mid - 1]; j = mid; } else { if (mid < n - 1 && target < arr[mid + 1]) return (arr[mid + 1] - target >= target - arr[mid]) ? arr[mid] : arr[mid + 1]; i = mid + 1; } } return arr[mid]; } int main() { int n, m, ans = 0; cin >> n >> m; int arr[n]; pair ikans[m]; for (int i = 0; i < n; i++) { cin >> arr[i]; } for (int i = 0; i < m; i++) { cin >> ikans[i].first; int pow1, pow2, selisih; cin >> pow1 >> pow2; selisih = pow2 - pow1; ikans[i].second = selisih; ikans[i].first = abs(ikans[i].first - findClosest(arr, n, ikans[i].first)); if (ikans[i].first < selisih) { ans += pow2; ans -= ikans[i].first; } else ans += pow1; } cout << ans; return 0; }