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<string>
#include<algorithm>
using namespace std;

int main() {
	int da, n,p=0,q=0;
	cin >> da >> n;
	int hot[201] = {0};
	int wear[3][101];
	int d[201][101] = {0};
	int kiroku[2][101] = {0};
	for (int i = 0; i < da; i++) {
		cin >> hot[i];
		
	}
	for (int i = 0; i < n; i++) {
		cin >> wear[0][i] >> wear[1][i] >> wear[2][i];
		
	}
	for (int i = 0; i < da; i++) {

		p = 0;
		for (int j = 0; j < n; j++) {
			
		
			if (wear[0][j] <= hot[i] && wear[1][j] >= hot[i]) {
				
				if (i == 0) {
					d[i][j] = wear[2][j];
					kiroku[1][p] = j;
					
				}
				else if (i == 1) {
					for (int k = 0; k<q; k++) {
						int a;
						if (wear[2][kiroku[0][k]] < wear[2][j]) {
							a = wear[2][j] - wear[2][kiroku[0][k]];
						}
						else {
							a = wear[2][kiroku[0][k]] - wear[2][j];
						}
				
						if (a  > d[i][j]) {
							d[1][j] = a ;
						}
						
						kiroku[1][p] = j;
						
						
					}
				}
				else {
					for(int k=0;k<q;k++){
						int a;
						if (wear[2][kiroku[0][k]] < wear[2][j]) {
							a=wear[2][j] - wear[2][kiroku[0][k]];
						}
						else {
							a = wear[2][kiroku[0][k]] - wear[2][j];
						}
						
						if (a + d[i - 1][kiroku[0][k]] > d[i][j]) {
							d[i][j] = a + d[i - 1][kiroku[0][k]];
							kiroku[1][p] = j;
							
						}
						
						kiroku[1][p] = j;

						
					}
				}
				p++;
				

			}

			
			
		}
		q = p;
		
		for (int k = 0; k < q; k++) {
			
			kiroku[0][k] = kiroku[1][k];
		}
		
	}
	int max = d[da - 1][0];
	for (int i = 1; i < n; i++) {
		if (d[da - 1][i] > max) {
			max = d[da - 1][i];
		}
	}
	cout << max << endl;
	return 0;
}

C++: #include<iostream>
#include<algorithm>
#include<queue>
using namespace std;
struct hage {
	int MIN, MAX, POINT;
};
hage huku[250];
int day[250];
int n, m;
int memo[250][250];

int check(int now, int num)
{
	if (now == n-1)return 0;
	if (memo[now][num] != -1)return memo[now][num];
	int aho;
	int ans=0;
	int HJ=0, JK=0;
	for (int i = 0; i < m; i++)
	{
		if (day[now + 1] > huku[i].MAX || day[now + 1] < huku[i].MIN)continue;
		aho = check(now + 1, i);
		if (HJ < aho){
			HJ = aho; JK = i;
		}
	}
//	cout << now << ":" << max(huku[JK].POINT, huku[num].POINT) - min(huku[JK].POINT, huku[num].POINT) <<":"<<aho<< endl;
	return memo[now][num]=aho + (max(huku[JK].POINT, huku[num].POINT) - min(huku[JK].POINT, huku[num].POINT));

}

int main()
{
	cin >> n >> m;
	for (int i = 0; i < n; i++)
		cin >> day[i];
	for (int i = 0; i < m; i++)
		cin >> huku[i].MIN >> huku[i].MAX >> huku[i].POINT;
	for (int i = 0; i < 250; i++)
		for (int t = 0; t < 250; t++)
			memo[i][t] = -1;
	int answer = 0;
//	cout<<check(0, 3)<<endl;
	//cout << endl << endl << endl;
	for (int i = 0; i < m; i++)
	{
		if (day[0]>huku[i].MAX || day[0] < huku[i].MIN)continue;
		int a = check(0, i);
	//	cout << endl;
		if (answer < a){
			answer = a;
		}
	}
	cout << answer << endl;




	return 0;
}

C++: #include<iostream>
#include<cmath>
#include<algorithm>
#define MINF -1e9
using namespace std;

struct cloth
{
  int hi,lo,point;
};

int dp[201][201];

int main(){
  int d,n;
  cin >> d >> n;
  int t[d];
  cloth *c;
  c = new cloth[n];
  for(int i = 0; i < d; i++){
    cin >> t[i];
  }
  for(int i = 0; i < n; i++){
    cin >> c[i].lo >> c[i].hi >> c[i].point;
  }

  for(int i = 0; i < n; i++){
    if(c[i].lo <= t[0] && t[0] <= c[i].hi){
    }else{
      dp[0][i] = MINF;
    }
  }

  for(int i = 1; i < d; i++){
    for(int j = 0; j < n; j++){
      for(int k = 0; k < n; k++){
	if(c[j].lo <= t[i] && t[i] <= c[j].hi){
	  dp[i][j] = max(dp[i][j],
			 dp[i-1][k] + abs(c[k].point - c[j].point));
	}else{
	  dp[i][j] = MINF;
	}
      }
    }
  }
  int res = 0;
  for(int i = 0; i < n; i++){
    res = max(dp[d - 1][i], res);
  }
  
  // for(int i = 0; i < d; i++){
  //   for(int j = 0; j < n; j++){
  //     cout << dp[i][j] << " ";
  //   }
  //   cout << endl;
  // }
  
  cout << res << endl;
  return 0;
}

C++: #include<iostream>
#include<map>
#include<algorithm>
#include<math.h>
using namespace std;
typedef pair<int,int> P;
typedef pair<int, P > wear;
int n,d;
int h[1001];
wear w[1001];
int mem[202][202]={0};
int check(int day,int kinounohade){
  int maxs=0;
  if(mem[day][kinounohade]!=0){
    return mem[day][kinounohade];//メモ
  }
  if(day==d+1){
    return mem[day][kinounohade]=0;//最終日の１個前なので０いれとく
  }
  else{
    for(int i=1;i<=n;i++){
      if(h[day]>=w[i].second.first&&h[day]<=w[i].second.second){
	//気温と服のチェック
	maxs=max(maxs,check(day+1,w[i].first)+(day!=1?abs(kinounohade-w[i].first):0));
	//maxsにmaxsと最終日から足していった累積の値と今for文に入っている服と昨日の服の絶対値を

	//足して合した値の計の2つのおおきい方を入れる（day=1の時は昨日が存在しないので0を返す）
      }
    }
    return mem[day][kinounohade]=maxs;
  }
}
main(){
  cin>>d>>n;
  for(int i=1;i<=d;i++){
    cin>>h[i];
  }
  for(int i=1;i<=n;i++){
    cin>>w[i].second.first>>w[i].second.second>>w[i].first;
  }
  cout<<check(1,0)<<endl;
}

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]==0){
	      ab[k][j]=abs(C[k]-C[j]);
	      ab[j][k]=ab[k][j];
	    }
	    if(max<(memo[i-1][k]+ab[k][j])){
	      max=memo[i-1][k]+ab[k][j];
	    }
	  }
	}
	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;
}