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<vector>
#include<queue>
#include<functional>
#include<string.h>
#include<bitset>
#include<set>
using namespace std;
int day, N, ans;
int d[201], n[201][3];
int dp[201][201];
int main(){
	cin >> day >> N;
	for (int i = 1; i <= day; i++)
		cin >> d[i];
	for (int i = 1; i <= N; i++)
		cin >> n[i][0]>>n[i][1]>>n[i][2];
	for (int i = 0; i < 201; i++)
		fill(dp[i], dp[i] + 201, -1);
	for (int i = 1; i <= day; i++){
		for (int j = 1; j <= N; j++){
				if (d[i] >= n[j][0] && d[i] <= n[j][1]){
					if (i == 1){
						dp[1][j] = 0;
						continue;
					}
					for (int k = 1; k <= N; k++){
						if (dp[i - 1][k] != -1)
							dp[i][j] = max(dp[i][j], abs(n[j][2] - n[k][2]) + dp[i - 1][k]);
				}
			}
				if (i == day)
					ans = max(dp[i][j], ans);
		}
	}
	
	cout << ans << endl;
	return 0;
}

C++: #include<cstdio>
#include<cmath>
#include<iostream>
using namespace std;

int main(){
  int memo_tf[222][222]={};
  int memo[222][222]={};
  int A[66],B[66],C[111],T[222];
  int N,D;
  int ab[222][222]={};

  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++){
      if(A[j]<=T[i] && T[i]<=B[j]){
	memo_tf[i][j]=1;
      }
    }
  }
  
  int sum=0;

  for(int i=1;i<D;i++){
    for(int j=0;j<N;j++){
      if(memo_tf[i][j]==1){
	int max=0;
	for(int k=0;k<N;k++){
	  if(memo_tf[i-1][k]==1){
	    if(ab[k][j-1]==0){
	      ab[k][j-1]=abs(C[k]-C[j]);
	      ab[j-1][k]=ab[k][j-1];
	    }
	    if(max<(memo[i-1][k]+ab[k][j-1])){
	      max=memo[i-1][k]+ab[k][j-1];
	    }
	  }
	}
	memo[i][j]=max;
      }
      if(sum<memo[i][j]){
	sum=memo[i][j];
      }
    }
  }

  /*
  for(int i=0;i<D;i++){
    for(int j=0;j<N;j++){
      printf("%2d ",memo[i][j]);
    }
    printf("\n");
  }
  */  

  printf("%d\n",sum);


  return 0;
}

C++: //============================================================================
// Name        : JOI.cpp
// Author      : Akari Fujii
// Version     :
// Copyright   : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================

#include <iostream>
#include <cctype>
#include <string>
#include<algorithm>

using namespace std;


// 1. dp?????????
int dp[201][201];
// 2. n????????????
int d,n;
int a[201],b[201],c[201],t[201];
// ???. calc????????????
int calc(int k,int m){

	// 8. dp???????????°??????
	if(dp[k][m]>=0)return dp[k][m];
	if(t[m]<a[k]||t[m]>b[k])return -1;
	for(int i=1; i<n+1; ++i){
		if(calc(i,m-1)>=0)dp[k][m]=max(dp[k][m],abs(c[k]-c[i])+calc(i,m-1));
	}
	return dp[k][m];

}

int main() {

	cin>>d>>n;

	// 3. ?????????
	for(int i=0;i<201;++i){
		for(int j=0;j<201;++j){
			dp[i][j]=-1;
		}
	}
	// 4.??\???
	for(int i=1; i<d+1; ++i)cin>>t[i];
	for(int j=1; j<n+1; ++j)cin>>a[j]>>b[j]>>c[j];

	// 5. ???????????¨??????

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

	// 6. ??????

	int total=0;
	for(int i=1;i<n+1;++i){
		total=max(total,calc(i,d));
	}
	cout<<total<<endl;
	return 0;
}

C++: #include <iostream>
#include <vector>
#define abs(x) (x>0?x:-(x))
#define MAX(x,y) (x>y?x:y)
#define D 200
#define N 200
#define INF 1000000
using namespace std;

typedef struct{
	int max_t,min_t,c;
} clothes;

int main(){
	clothes clt[210];
	int t[210];
	int d,n,old_c=0,sum=0;
	cin>>d>>n;

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

	for(int i=0;i<n;i++){
		cin>>clt[i].min_t>>clt[i].max_t>>clt[i].c;
	}

	int dp[D][N];
	
	for(int i=0;i<d;i++){
		for(int j=0;j<n;j++){
			if(i==0 && clt[j].min_t<=t[0] && clt[j].max_t>=t[0]) dp[i][j]=0;
			else dp[i][j]=-INF;
		}
	}

	for(int i=1;i<d;i++){
		for(int j=0;j<n;j++){
			if(clt[j].min_t<=t[i] && clt[j].max_t>=t[i]){
				for(int k=0;k<n;k++){
					dp[i][j]=MAX(dp[i][j],dp[i-1][k]+abs(clt[k].c-clt[j].c));
				}
			}
		}
	}
	
	int m=0;
	for(int i=0;i<n;i++){
		m=MAX(m,dp[d-1][i]);
	}

	cout<<m<<endl;
	return 0;
}