Go: ///
// File:  0579.go
// Author: ymiyamoto
//
// Created on Mon Mar 19 18:46:50 2018
//
package main

import (
	"fmt"
)

var D, N int

type cloth struct {
	A, B, C int
}

func abs(a int) int {
	if a > 0 {
		return a
	}
	return -a
}

func max(a, b int) int {
	if a < b {
		return b
	}
	return a
}

func main() {
	fmt.Scan(&D, &N)
	thermals := make([]int, D+1)

	for i := 1; i <= D; i++ {
		fmt.Scan(&thermals[i])
	}

	clothes := make([]cloth, N)
	for i := 0; i < N; i++ {
		fmt.Scan(&clothes[i].A, &clothes[i].B, &clothes[i].C)
	}

	dp := make([][]int, D+1)
	for i := range dp {
		dp[i] = make([]int, N)
	}

	for i := 0; i < D; i++ {
		for j := 0; j < N; j++ {
			for k := 0; k < N; k++ {
				t1 := thermals[i]
				t2 := thermals[i+1]
				if clothes[j].A <= t2 && t2 <= clothes[j].B && clothes[k].A <= t1 && t1 <= clothes[k].B {
					dp[i+1][j] = max(dp[i+1][j], dp[i][k]+abs(clothes[j].C-clothes[k].C))
				}
			}
		}
	}

	val := 0
	for _, v := range dp[D] {
		val = max(val, v)
	}

	fmt.Println(val)
}

C++: #include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;

int dp[201][61], d, n, t[61], a[201], b[201], c[201], mx = 0;

int abs(int x, int y)
{
	if (x >= y) return x - y;
	else return y - x;
}

int main()
{
	cin >> d >> n;
	for (int i = 1; i <= d; i++){
		cin >> t[i];
	}
	for (int i = 1; i <= n; i++){
		cin >> a[i] >> b[i] >> c[i];
	}
	memset(dp, -1, sizeof dp);

	for (int i = 1; i <= n; i++){
		if (a[i] <= t[1] && t[1] <= b[i]){
			dp[1][i] = 0;
		}
	}

	//dp
	for (int today = 2; today <= d; today++){
		for (int n2 = 1; n2 <= n; n2++){
			for (int n1 = 1; n1 <= n; n1++){
				if (dp[today - 1][n1] == -1) continue;
				if (a[n2] > t[today] || t[today] > b[n2]) continue;
				dp[today][n2] = max(dp[today][n2], dp[today - 1][n1] + abs(c[n1], c[n2]));
			}
		}
	}

	for (int i = 1; i < n; i++){
		mx = max(dp[d][i], mx);
	}
	cout << mx << endl;
	return 0;
}

C++: #include <stdio.h>
#include <algorithm>

int D, N, T[212], A[212], B[212], C[212];
int dp[212][212], memo[212], ans;

int main(void) {
	scanf("%d%d", &D, &N);
	for (int i = 0; i < D; ++i) { scanf("%d", &T[i]); }
	for (int i = 0; i < N; ++i) { scanf("%d%d%d", &A[i], &B[i], &C[i]); }

	for (int i = 0; i < D; ++i) {
		for (int j = 0; j < N; ++j) {
			dp[i][j] = -1;
		}
	}
	for (int i = 0; i < D; ++i) {
		for (int j = 0; j < N; ++j) {
			if (A[j] <= T[i] && T[i] <= B[j]) {
				if (i == 0) { dp[i][j] = C[j]; }
				else {
					for (int k = 0; k < N; ++k) {
						if (dp[i - 1][k] != -1) {
							dp[i][j] = dp[i - 1][k] + abs(C[j] - C[k]);
							memo[i] = std::max(memo[i], abs(dp[i][j] - dp[i - 1][k]));
						}
					}
				}
			}
		}
	}

	for (int i = 0; i < D; ++i) {
		ans += memo[i];
	}
	printf("%d\n", ans);
	return 0;
}

C++: /* {{{ Shinobu kawaii */
/*
  ______   __        _                  __                 
.' ____ \ [  |      (_)                [  |                
| (___ \_| | |--.   __   _ .--.   .--.  | |.--.   __   _   
 _.____`.  | .-. | [  | [ `.-. |/ .'`\ \| '/'`\ \[  | | |  
| \____) | | | | |  | |  | | | || \__. ||  \__/ | | \_/ |, 
 \______.'[___]|__][___][___||__]'.__.'[__;.__.'  '.__.'_/ 

*/
/* }}} */

#include <bits/stdc++.h>
using namespace std;

// #define int long long
struct Fast {Fast(){std::cin.tie(0);ios::sync_with_stdio(false);}} fast;

/* cpp template {{{ */

/* short */
#define pb push_back
#define mp make_pair
#define Fi first
#define Se second
#define ALL(v) (v).begin(), (v).end()
#define X real()
#define Y imag()

/* REPmacro */
#define REPS(i, a, n) for (int i = (a); i < (n); ++i)
#define REP(i, n) REPS(i, 0, n)
#define RREP(i, n) REPS(i, 1, n + 1)
#define DEPS(i, a, n) for (int i = (a); i >= n; --i)
#define DEP(i, n) DEPS(i, n, 0)

/* debug */
#define debug(x) cerr << x << " " << "(L:" << __LINE__ << ")" << '\n';

/* typedef */
typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<vi> vii;
typedef vector<vii> viii;
typedef pair<int, int> pii;
typedef double D;
typedef complex<D> P;

/* const */
const int INF = 999999999;
const int MOD = 1e9 + 7;
const D EPS = 1e-9;
const int dx[] = {0, 1, 0, -1, 1, -1, 1, -1}, dy[] = {1, 0, -1, 0, 1, -1, -1, 1};

/* func */
inline bool inside(int y, int x, int H, int W) {return y >= 0 && x >= 0 && y < H && x < W;}
inline int in() {int x; std::cin >> x; return x;}
template <typename T> void print(T x) {std::cout << x << '\n';}
template <typename T>
void print(std::vector<T>& v, string s = " ") {
  REP(i, v.size()) {
    if (i != 0) std::cout << s;
    std::cout << v[i];
  }
  std::cout << '\n';
}

/* }}} */

int d, n;
vi t(200);
vi a(200), b(200), c(200);
vi dp(210, -INF);

signed main()
{
  cin >> d >> n;
  REP(i, d) t[i] = in();
  REP(i, n) cin >> a[i] >> b[i] >> c[i];

  int yest = INF;
  REP(i, n) {
    if (a[i] <= t[0] && t[0] <= b[i]) {
      yest = min(yest, c[i]);
    }
  }

  REPS(i, 1, d) {
    int tod = -INF;
    REP(j, n) {
      if (a[j] <= t[i] && t[i] <= b[j]) {
        if (dp[i - 1] < abs(yest - c[j])) {
          dp[i - 1] = abs(yest - c[j]);
          tod = c[j];
        }
      }
    }
    yest = tod;
  }
  print(accumulate(dp.begin(), dp.begin() + d - 1, 0));

  return 0;
}

C++: #include<iostream>
#include<string>
#include<math.h>
#include<algorithm>
#include<stack>
#include<vector>
#include<set>
#include<queue>

using namespace std;

int main()
{
	int A[200],B[200],C[200],D,N,T[200];
	int sum1 = 0;
	int sum2 = 0;
	vector<int>select[200];
	cin >> D >> N;
	for(int a = 0;a<D;a++)
	{
		cin >> T[a];
	}
	for(int a = 0;a<N;a++)
	{
		cin >> A[a] >> B[a] >> C[a];

	}

	int b;

	for(int a = 0;a<D;a++)
	{
		for(b = 0;b<N;b++)
		{
			if(A[b]<=T[a]&&T[a]<=B[b])
			{
				select[a].push_back(C[b]);
			}
		}
	}

	

	sort(select[0].begin(),select[0].end());

	int c = 0;

	for(int a = 0;a<D-1;a++)
	{
		if(c == 0)
		{
			sort(select[a+1].begin(),select[a+1].end(),greater<int>());
			if(*select[a].begin() > *select[a+1].begin())
			{
				sum1 += *select[a].begin() - *select[a+1].begin();
			}
			else
			{
				sum1 += *select[a+1].begin() - *select[a].begin();
			}
			c++;
		}
		else
		{
			sort(select[a+1].begin(),select[a+1].end());
			if(*select[a].begin() > *select[a+1].begin())
			{
				sum1 += *select[a].begin() - *select[a+1].begin();
			}
			else
			{
				sum1 += *select[a+1].begin() - *select[a].begin();
			}
			c--;
		}
	}

	c = 0;
	sort(select[0].begin(),select[0].end(),greater<int>());

	for(int a = 0;a<D-1;a++)
	{
		if(c == 1)
		{
			sort(select[a+1].begin(),select[a+1].end(),greater<int>());
			if(*select[a].begin() > *select[a+1].begin())
			{
				sum2 += *select[a].begin() - *select[a+1].begin();
			}
			else
			{
				sum2 += *select[a+1].begin() - *select[a].begin();
			}
			c--;
		}
		else
		{
			sort(select[a+1].begin(),select[a+1].end());
			if(*select[a].begin() > *select[a+1].begin())
			{
				sum2 += *select[a].begin() - *select[a+1].begin();
			}
			else
			{
				sum2 += *select[a+1].begin() - *select[a].begin();
			}
			c++;

			
		}
	}

	cout << max(sum1,sum2) << endl;

	return 0;
}

C++: #include <bits/stdc++.h>

using namespace std;

typedef struct {
	int a;
	int b;
	int c;
} fuku_t;

int d, n;

int t[200];
fuku_t fuku[200];

int dp[200][200];

int main()
{
	cin >> d >> n;

	for (int i = 0; i < d; ++i) cin >> t[i];

	for (int i = 0; i < n; ++i) scanf("%d %d %d", &fuku[i].a, &fuku[i].b, &fuku[i].c);

	for (int i = 0; i < n; ++i) if (fuku[0].a <= t[0] && t[0] <= fuku[0].b) dp[0][i] = fuku[i].c;

	for (int i = 1; i < d; ++i) {
		for (int j = 0; j < n; ++j) {
			for (int k = 0; k < n; ++k) {
				if (fuku[j].a <= t[i] && t[i] <= fuku[j].b &&
					fuku[k].a <= t[i-1] && t[i-1] <= fuku[k].b)
				{
					dp[i][j] = max(dp[i][j], dp[i - 1][k] + abs(fuku[j].c - fuku[k].c));
				}
			}
		}
	}

	int ans = 0;

	for (int i = 0; i < n; ++i) ans = max(ans, dp[d - 1][i]);

	cout << ans << endl;
}