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<bits/stdc++.h>
using namespace std;
int n,m,a[201],b[3][201];
int mem[200][20000];
int hot(int day,int bef);

int main(){

  cin >> n >> m;

  memset(mem,-1,sizeof(mem));

  for(int i=0;i<n;i++) cin >> a[i];
  for(int i=0;i<m;i++) cin >> b[0][i] >> b[1][i] >> b[2][i];

  cout << hot(0,0) << endl;
  
  return(0);
}

int hot(int day,int bef){
  int ret=0;
  //if(mem[day][bef] != -1) return mem[day][bef];
  if(day < n){
    for(int i=0;i<m;i++){
      if(a[day]>=b[0][i]&&a[day]<=b[1][i]){
	if(day==0){
	  ret=max(ret,hot(day+1,b[2][i]));
	}else{
	  ret=max(ret,hot(day+1,b[2][i])+(int)fabs((double)bef-b[2][i]));
	}
	
      }
    }
  }
  return /*mem[day][bef] =*/ ret;
}

C++: #include<bits/stdc++.h>
#define LL long long
#define FOR(i,a,b) for(int i=a;i<=b;++i)

using namespace std;

static const LL MAX_N = 1e+5;
static const LL INF = 1 << 25;

struct clothes{
    LL A[100],B[100],C[150];
};

int N,D;
LL res=-INF;
LL Day[300];
LL mem[300][160][160];
clothes T;

LL cal(LL day,LL p,LL pp,LL sum){
    //cout<<sum+abs(pp-p)<<" "<<day<<" "<<p<<" "<<pp<<endl;
    if(mem[day][p][pp]!=0){
        res=mem[day][p][pp];
        mem[day][p][pp]=0;
    }
    else{
        if(res<sum+abs(pp-p)&&day==N+1)mem[day][p][pp]=res=sum+abs(pp-p);
        if(day==N+1)return 0;
        if(day==0){
            FOR(i,1,D)
            if(Day[1]>=T.A[i]&&Day[1]<=T.B[i])cal(day+1,T.C[i],p,sum);
        }
        FOR(i,1,D){
            if(Day[day]>=T.A[i]&&Day[day]<=T.B[i]){
                if(day>2)cal(day+1,T.C[i],p,sum+abs(pp-p));
                else cal(day+1,T.C[i],p,sum);
            }
        }
    }
    return res;
}


int main(){
    cin>>N>>D;
    FOR(i,1,N)cin>>Day[i];
    FOR(i,1,D)
        cin>>T.A[i]>>T.B[i]>>T.C[i];
    cout<<cal(0,0,0,0)<<endl;
}

C++: #include <vector>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <algorithm>
#include <utility>
#include <functional>
#include <sstream>
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <ctime>
#include <climits>
#include <fstream>
using namespace std;
inline int toInt(string s) { int v; istringstream sin(s); sin >> v; return v;}
template<class T> inline string toStr(T x) { ostringstream sout; sout << x; return sout.str();}
typedef vector<int> vi;
typedef vector<vi>  vvi;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef long long ll;
#define ALL(a) (a).begin(),(a).end()
#define RALL(a) (a).rbegin(),(a).rend()
#define FOR(i,a,b) for(int i=(a);i<=(b);++i)
#define REP(i,n) FOR(i,0,(n)-1)
const double EPS = 1e-12;
const double PI = acos(-1.0);
const int INF = INT_MAX/10;

int main() {
	int D, N;
	cin >> D >> N;

	vi T(D), A(N), B(N), C(N);
	REP(i, D) {
		cin >> T[i];
	}
	REP(i, N) {
		cin >> A[i] >> B[i] >> C[i];
	}

	vvi dp(D+1, vi(N));
	vvi ok(D+1, vi(N));
	FOR(i, 1, D) {
		REP(j, N) {
			if(A[j] <= T[i-1] && T[i-1] <= B[j]) {
				ok[i][j] = 1;	
				if(i == 1) {

				} else {
					int m = -1;
					int mp = -1;
					REP(k, N) {
						if(ok[i-1][k]) {
							if(m < abs(C[k]-C[j])) {
								m = abs(C[k]-C[j]);
								mp = k;
							}
						}
					}
					dp[i][j] = m + dp[i-1][mp];
				}
			}
		}
	}

	int ans = 0;
	REP(i, N) {
		ans = max(ans, dp[D][i]);
	}
	cout << ans << endl;
	return 0;
}

C++: #include<stdio.h>
int dp[201][201];
int main(void)
{
	int d,n,t[201],a[201],b[201],c[201];
	int i,j,k,w=0,x,max;
	scanf("%d%d",&d,&n);
	for(i=1;i<=d;i++) scanf("%d",&t[i]);
	for(i=1;i<=n;i++) scanf("%d%d%d",&a[i],&b[i],&c[i]);
	for(i=1;i<=d;i++){
		for(j=1;j<=n;j++) dp[i][j]=-1;
	}
	for(i=1;i<=n;i++){
			if(t[1]<=b[i]&&t[1]>=a[i]) dp[1][i]=0;
	}
	for(i=1;i<d;i++){
		for(j=1;j<=n;j++){
			if(dp[i][j]!=-1){
				for(k=1;k<=n;k++){
					if(t[i+1]>=a[k]&&t[i+1]<=b[k]){
							w=c[j]-c[k];					
						if(w<0){
							w*=-1;
						}	
						if(dp[i+1][k]<dp[i][j]+w){
							dp[i+1][k]=dp[i][j]+w;
						}	
					}
				}
			}
		}
	}
	x=-1;
	for(i=1;i<=n;i++){
		if(x<dp[d][i]) x=dp[d][i];
	}
	printf("%d\n",x);
	return 0;
}

C++: /*
D??\??????????°??????±
???????°???????T???
N?¨??????????
???j??????????°???? A??\??? B??\???????´??????? C
??£?¶?????????\????????????????´????????????¶?????????????¨?
*/
/*
1.????????????????±???????
2.????????\??¨?¬??????\??????????????§?????§??????????±??????????
3.???????????°????????????
4.?????????????????????????????????????????????
*/
#include<iostream>
#include<algorithm>
#include<cmath>
using namespace std;

int main(){
	int D, N, T[201], A[201], B[201], C[201], max_day = 0;
	int dp[201][201];  //[????????\][?????????]
	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];
	}
	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] && B[j] >= T[i]){//??????????????????????????????
				dp[i][j] = 0;
			}
		}
	}
	for (int i = 0; i < D; i++){
		for (int j = 0; j < N; j++){
			if (dp[i][j] >= 0){
				for (int k = 0; k < N; k++){
					if (dp[i + 1][k] >= 0){//k????????????????????????
						dp[i + 1][k] = max(dp[i + 1][k], dp[i][j] + abs(C[j] - C[k]));//?´????????????¶????????´??°
					}
				}
			}
		}
	}
	for (int i = 0; i < N; i++){
		if (dp[D - 1][i] > max_day){
			max_day = dp[D - 1][i];//?????§??¶????????´??°
		}
	}
	cout << max_day << endl;
	return 0;
}

C++: #include <cstdio>
#include <vector>
#include <cstring>
#include <algorithm>
#include <cmath>
 
using namespace std;
 
#define MAX 200
#define MAX_T 60
typedef pair<int,int> P;
 
int T[MAX],D,dp[MAX][MAX];
vector<P> vec[MAX_T+1];
 
int solve(int d,P prev){
    if(d == D) return 0;
    if(dp[d][prev.second] >= 0) return dp[d][prev.second];
    int res = 0;
    for(int i = 0 ; i < (int)vec[T[d]].size() ; i++){
	int diff = abs(vec[T[d]][i].first-prev.first);
	res = max(res,solve(d+1,vec[T[d]][i])+diff);
    }
    return dp[d][prev.second] = res; 
}
 
int main(){
    int N,A,B,C;
    memset(dp,-1,sizeof(dp));
 
    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,&B,&C);
	for(int j = A ; j <= B ; j++){
	    vec[j].push_back(P(C,i));
	}
    }
    int ans = 0;
    for(int i = 0 ; i < (int)vec[T[0]].size() ; i++){
	ans = max(ans,solve(1,vec[T[0]][i]));
    }
    printf("%d\n",ans);
    return 0;
}