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 := 1; 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 <vector>
#include <algorithm>
#include <string.h>
#include <iomanip>
#include <math.h>
#include <string>
#include <queue>
#include <map>
#include <set>
#include <functional>

using namespace std;

typedef long long int ll;
typedef std::pair<int, int> pii;
typedef std::pair<ll, int> pli;
typedef std::pair<ll, ll> pll;

#define FOR(i,n,m) for(ll i=(ll)(m);i<(ll)(n);++i)
#define REP(i,n) FOR(i,n,0)
#define IREP(i,n) for(ll i=(ll)(n);i>=0;--i)
#define OF64 std::setprecision(10)

const ll MOD = 1000000007;
const ll INF = (ll)1e15;

int T[205];
int A[205], B[205], C[205];

//! dp[i日目に][服kを選んだ時の]=最大値
int dp[205][205];

int main()
{
	int D, N;
	cin >> D >> N;
	REP(i, D)
	{
		cin >> T[i];
	}
	REP(i, N)
	{
		cin >> A[i] >> B[i] >> C[i];
	}
	memset(dp, 0, sizeof(dp));

	FOR(i, D, 1)
	{
		REP(j, N)
		{
			//! i日目に選ぶ服
			if (T[i] < A[j] || B[j] < T[i])
			{
				dp[i][j] = 0;
				continue;
			}
			int m = 0;
			REP(k, N)
			{
				//! i-1に選ぶ服
				if (T[i - 1] < A[k] || B[k] < T[i - 1])
				{
					continue;
				}
				m = std::max(m, abs(C[k] - C[j]) + dp[i - 1][k]);
			}
			dp[i][j] = m;
		}
	}

	int max = 0;
	REP(i, N)
	{
		max = std::max(max, dp[D - 1][i]);
	}
	cout << max << endl;
	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;
    if (i == 1) {
      REP(j, n) {
        if (a[j] <= t[1] && t[1] <= b[j]) {
          if (dp[0] < abs(yest - c[j])) {
            dp[0] = abs(yest - c[j]);
            tod = c[j];
            //debug(tod);
          }
        }
      }
    } else {
      REP(j, n) {
        if (a[j] <= t[i] && t[i] <= b[j]) {
          if (dp[i - 1] < dp[i - 2] + abs(yest - c[j])) {
            dp[i - 1] = dp[i - 2] + abs(yest - c[j]);
            tod = c[j];
          }
        }
      }
    }
    //cerr << yest << " " << tod << endl;
    yest = tod;
  }

  print(dp[d - 2]);

  return 0;
}

C++: #include <cstdio>
#include <cstdint>
#include <cstdlib>
#include <vector>
#include <algorithm>
#include <utility>
#include <numeric>
#include <tuple>

int testcase_ends() {
  size_t D, N;
  scanf("%zu %zu", &D, &N);

  std::vector<int> T(D);
  for (auto& ti: T) scanf("%d", &ti);

  std::vector<std::tuple<int, int, int>> abc(N);
  int max_c = 0;
  for (auto& abci: abc) {
    int a, b, c;
    scanf("%d %d %d", &a, &b, &c);
    abci = {a, b, c};
    max_c = std::max(max_c, c);
  }

  int inf = 1e8;
  std::vector<std::vector<int>> dp(D, std::vector<int>(max_c+1, -inf));

  for (size_t j = 0; j < N; ++j) {
    int a, b, c;
    std::tie(a, b, c) = abc[j];
    if (!(a <= T[0] && T[0] <= b)) continue;
    dp[0][c] = 0;
  }

  for (size_t i = 1; i < D; ++i) {
    for (size_t j = 0; j < N; ++j) {
      int a, b, c;
      std::tie(a, b, c) = abc[j];
      if (!(a <= T[i] && T[i] <= b)) continue;
      for (int k = 0; k <= max_c; ++k) {
        dp[i][c] = std::max(dp[i][c], dp[i-1][k] + std::abs(k-c));
      }
    }
  }

  int res = *std::max_element(dp[D-1].begin(), dp[D-1].end());
  printf("%d\n", res);
  return 1;
}

int main() {
  while (!testcase_ends()) {}
}

C++: #include <iostream>
#include <vector>
#include <functional>
#include <queue>
#include <utility>

constexpr int INF = 1000000000;

int n, d;
int dp[210][2]; // 0: max, 1: min
int data[210][2];
int temp[210];

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

    for(int i = 0; i < d; ++i) {
        std::cin >> temp[i];
    }

    for(int i = 0; i < d; ++i) {
        data[i][0] = -1;
        data[i][1] = INF;
    }

    int a, b, c;
    for(int i = 0; i < n; ++i) {
        std::cin >> a >> b >> c;

        for(int j = 0; j < d; ++j) {
            if(temp[j] >= a && temp[j] <= b) {
                data[j][0] = std::max(data[j][0], c);
                data[j][1] = std::min(data[j][1], c);
            }
        }
    }

    for(int i = 1; i < d; ++i) {
        dp[i][0] = std::max(
                        dp[i - 1][1] + std::abs(data[i - 1][1] - data[i][0]),
                        dp[i - 1][0] + std::abs(data[i - 1][0] - data[i][0])
                        );
        dp[i][1] = std::max(
                        dp[i - 1][1] + std::abs(data[i - 1][1] - data[i][1]),
                        dp[i - 1][0] + std::abs(data[i - 1][0] - data[i][1])
                        );
    }

    std::cout << std::max(dp[d - 1][0], dp[d - 1][1]) << std::endl;
}