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 <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <cmath>
using namespace std;

#define REP(i, b, e) for(int i = b ; i < e ; i++)
#define rep(i, n) REP(i, 0, n)

struct Dress{
  int A, B, C;
};

const int MAX = 210;

bool canWear(int temp, Dress d){
  return (d.A <= temp && temp <= d.B);
}

int main(){
  int D, N;
  while(cin >> D >> N){
    vector<Dress> dress(MAX);    
    vector<int> temp(MAX);
    rep(i, D) cin >> temp[i];
    rep(i, N) cin >> dress[i].A >> dress[i].B >> dress[i].C;
    
    int dp[MAX][MAX];
    memset(dp, -1, sizeof(dp));
    rep(i, N) if(canWear(temp[0], dress[i])) dp[0][i] = 0;
    
    REP(i, 1, D){
      REP(j, 0, N){
	REP(k, 0, N){
	  if(dp[i-1][j] == -1) continue;
	  if(!canWear(temp[i], dress[k])) continue;
	  dp[i][k] = max(dp[i][k], dp[i-1][j] + abs(dress[j].C - dress[k].C));
	}
      }
    }
    
    int ans = -1;
    rep(i, N) ans = max(ans, dp[D-1][i]);
    cout << ans << endl;
  }
  return 0;
}

C++: # include <iostream>
# include <algorithm>
# define MAX_N 201
# define MAX_D 201

int d, n;
int a[MAX_N], b[MAX_N], c[MAX_N];
int t[MAX_D];
int dp[MAX_D][MAX_N];

int main() {
	std::cin >> d >> n;
	for (int i = 1; i <= d; ++i)
		std::cin >> t[i];
	for (int i = 1; i <= n; ++i)
		std::cin >> a[i] >> b[i] >> c[i];
	for (int i = 2; i <= d; ++i) {
		for (int j = 1; j <= n; ++j) {
			int max = -1;
			if (a[j] <= t[i] && b[j] >= t[i]) {
				for (int k = 1; k <= n; ++k) {
					if (a[k] <= t[i - 1] && t[i - 1] <= b[k]) {
						max = std::max(max, dp[i - 1][k] + std::abs(c[j] - c[k]));
					}
				}
				dp[i][j] = max;
			}
		}
	}
	int res = -1;
	for (int i = 1; i <= n; ++i)
		res = std::max(res, dp[d][i]);
	std::cout << res << std::endl;
	return 0;
}

C++: #include <stdio.h>
#include<stdlib.h>
#define MAX_D 210
#define MAX_N 210

int D, N;
int T[MAX_D];
int A[MAX_N], B[MAX_N], C[MAX_N];
int suit[MAX_D][MAX_N];
int dp[MAX_D][MAX_N];

int main() {
	int i, j, k;

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

	for (i = 0; i < D; ++i) for (j = 0; j < N; ++j) {
		suit[i][j] = (A[j] <= T[i] && T[i] <= B[j]);
	}

	for (i = 0; i < D; ++i) for (j = 0; j < N; ++j) {
		//	-1 : impossible
		dp[i][j] = -1;
	}
	for (j = 0; j < N; ++j) if (suit[0][j]) {
		dp[0][j] = 0;
	}
	for (i = 0; i < D - 1; ++i) for (j = 0; j < N; ++j) if (dp[i][j] >= 0) {
		for (k = 0; k < N; ++k) if (suit[i + 1][k]) {
			if (dp[i + 1][k] < dp[i][j] + abs(C[j] - C[k])) {
				dp[i + 1][k] = dp[i][j] + abs(C[j] - C[k]);
			}
		}
	}

	int ans = -1;
	for (j = 0; j < N; ++j) {
		if (ans < dp[D - 1][j]) {
			ans = dp[D - 1][j];
		}
	}
	printf("%d\n", ans);

	return 0;
}

C++: //include
//------------------------------------------
#include <vector>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <ctime>
#include <fstream>
using namespace std;

//conversion
//------------------------------------------
inline int toInt(string s) {int v; istringstream sin(s);sin>>v;return v;}
template<class T> inline string toString(T x) {ostringstream sout;sout<<x;return sout.str();}

//math
//-------------------------------------------
template<class T> inline T sqr(T x) {return x*x;}

//typedef
//------------------------------------------
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef pair<long, long> PLL;
typedef long long LL;
//container util
//------------------------------------------
#define ALL(a)  (a).begin(),(a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define MP make_pair
#define SZ(a) int((a).size())
#define EACH(i,c) for(typeof((c).begin()) i=(c).begin(); i!=(c).end(); ++i)
#define EXIST(s,e) ((s).find(e)!=(s).end())
#define SORT(c) sort((c).begin(),(c).end())

//repetition
//------------------------------------------
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n)  FOR(i,0,n)

//constant
//--------------------------------------------
const double EPS = 1e-10;
const double PI  = acos(-1.0);

//clear memory
#define CLR(a) memset((a), 0 ,sizeof(a))
#define INF 100000

struct cloth{
	int a,b,c;
};

int T[201];
int main(){
	int D,N;
	cin>>D>>N;
	REP(i,D)cin>>T[i];
	vector<cloth>C;
	REP(i,N){
		cloth cl;
		cin>>cl.a>>cl.b>>cl.c;
		C.PB(cl);
	}
	int res=0;
	REP(k,2){
		int turn=k,sum=0;
		int l=0,r=0;
		REP(i,D){
			r=l;
			if(!turn){//範囲内で最大
				int temp=0;
				REP(j,N)if(C[j].a<=T[i]&&C[j].b>=T[i])temp=max(temp,C[j].c);
				l=temp;
			}else{//範囲内で最小
				int temp=INF;
				REP(j,N)if(C[j].a<=T[i]&&C[j].b>=T[i])temp=min(temp,C[j].c);
				l=temp;
			}
			if(i!=0)sum+=abs(l-r);
			turn^=1;
		}
		res=max(res,sum);
	}
	cout<<res<<endl;
	return 0;
}

C++: #include<iostream>
#include<cstdlib>

int D, N;
int T[200];
int A[200], B[200], C[200];
bool suit[200][200];
int dp[200][200];

int main()
{
	std::cin >> D >> N;
	for( int i = 0; i != D; ++i )
		std::cin >> T[i];
	for( int i = 0; i != N; ++i )
		std::cin >> A[i] >> B[i] >> C[i];

	for( int i = 0; i != D; ++i )
		for( int j = 0; j != N; ++j )
			suit[i][j] = A[j] <= T[i] && T[i] <= B[j];

	for( int i = 0; i != D - 1; ++i )
		for( int j = 0; j != N; ++j )
			if( suit[i+1][j] )
				for( int k = 0; k != N; ++k )
					if( suit[i][k] )
						dp[i+1][j] = std::max( dp[i+1][j], dp[i][k] + abs( C[j] - C[k] ) );

	int ans = 0;
	for( int i = 0; i != N; ++i )
		ans = std::max( ans, dp[D-1][i] );

	std::cout << ans << std::endl;

	return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;
int D, N, T[200], A[200], B[200], C[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++) cin >> A[i] >> B[i] >> C[i];

  fill_n(*dp, 200 * 200, -1);
  for(int i = 0; i < N; i++) {
    if(B[i] >= T[0] && T[0] >= A[i]) {
      for(int j = 0; j < N; j++) {
        if(B[j] >= T[1] && T[1] >= A[j]) {
          dp[1][j] = max(C[j] - C[i], C[i] - C[j]);
        }
      }
    }
  }

  for(int i = 2; i < D; i++) {
    for(int j = 0; j < N; j++) {
      if(~dp[i - 1][j]) {
        for(int k = 0; k < N; k++) {
          if(B[k] >= T[i] && T[i] >= A[k]) {
            dp[i][k] = max(dp[i][k], dp[i - 1][j] + max(C[j] - C[k], C[k] - C[j])); 
          }
        }
      }
    }
  }
  cout << *max_element(dp[0], dp[200]) << endl;
}