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 <stdio.h>

int abs(int a) {
  return a > 0 ? a : -a;
}

int main(void) {
  int i, j, k, l, d, n, max = 0;
  scanf("%d%d", &d, &n);
  int dp[d][n], t[d], a[n], b[n], c[n];
  for(i = 0; i < d; ++i) for(j = 0; j < n; ++j) dp[i][j] = 0;
  for(i = 0; i < d; ++i) scanf("%d", &t[i]);
  for(i = 0; i < n; ++i) scanf("%d%d%d", &a[i], &b[i], &c[i]);
  for(i = 0; i < n; ++i) if(!(a[i] <= t[0] && t[0] <= b[i])) dp[0][i] = -200;
  for(i = 1; i < d; ++i) {
    for(j = 0; j < n; ++j) if(a[j] <= t[i] && t[i] <= b[j]) {
      for(k = 0; k < n; ++k) {
        if(dp[i - 1][k] + abs(c[j] - c[k]) > dp[i][j]) dp[i][j] = dp[i - 1][k] + abs(c[j] - c[k]);
      }
    }
  }
  /*for(i = 0; i < d; ++i) {
    for(j = 0; j < n; ++j) printf("%d ", dp[i][j]);
    printf("\n");
  }*/
  for(i = 0; i < n; ++i) if(dp[d - 1][i] > max) max = dp[d - 1][i];
  printf("%d\n", max);
  return 0;
}

C++: #include<iostream>
#include<vector>
#include<algorithm>
#include<queue>
#include<map>
#include<cmath>
#define F first
#define S second
using namespace std;
typedef pair<int,int> P;
typedef pair<P,int> Pi;
int D,N;

int f(vector<vector<int> >& list,vector<vector<int> >& used)
{
  
  int mex = 0;
  queue<Pi> que;// Pi(P(value,day),pre_index)
  for(int i=0;i<list[0].size();i++)
    {
      que.push(Pi(P(0,1),i));
      used[0][i] = 0;
    }
  
  while(!que.empty())
    {
      Pi p = que.front(); que.pop();
      int total,day,pre_index;
      total = p.F.F, day = p.F.S, pre_index = p.S;

      if(day >= D)
	{
	  mex = max(mex,total);
	  continue;
	}

      for(int i=0;i<list[day].size();i++)
	{
	  int sum = total + abs(list[day-1][pre_index]-list[day][i]);
	  if(used[day][i] >= sum)
	    continue;

	  used[day][i] = sum;
	  que.push(Pi(P(sum,day+1),i));
	}

    }

  return mex;
}


int main()
{

  cin >> D >> N;
  vector<int> T;
  vector<vector<int> > list,used;
  list.resize(D),used.resize(D);


  for(int i=0;i<D;i++)
    {
      int in; cin >> in;
      T.push_back(in);
    }

  for(int j=0;j<N;j++)
    {
      int A,B,C;
      cin >> A >> B >> C;
      for(int k=0;k<D;k++)
	{
	  if(A <= T[k] && T[k] <= B)
	    {
	      list[k].push_back(C),used[k].push_back(-1);
	    }
	}
    }
  cout << f(list,used) << endl;


  return 0;
}

C++: #include<iostream>

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 <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <cmath>
#include <cstring>
#define loop(i,b,n) for(int i = b; i<n; i++)
#define rep(i,n)  loop(i,0,n)
#define INF 10000
using namespace std;

int main (){
    
    int d,number;
    int temp[210] = {0};
    int dp[500][500];
    rep(i,500)
        rep(j,500)
    dp[i][j] = -INF;
    int Min[210] = {0},Max[210] = {0},c[210] = {0};
    cin  >> d >> number;
    rep(i,d)
        cin >> temp[i+1];
    rep(i,number)
        cin >> Min[i+1] >> Max[i+1] >> c[i+1];
    loop(j,1,d+1)//day
    {
        
        loop(k,1,number+1)// k
        {
            if(Min[k] <= temp[j] && temp[j] <=Max[k])
            {
                if(j == 1)
                    dp[j][c[k]] = 0;
                else
                {
                    dp[j][c[k]] = 0;
                    loop(l,0,500)
                    {
                        if(dp[j-1][l] != -INF)
                        {
                            dp[j][c[k]] = max(abs(c[k]-l)+dp[j-1][l],dp[j][c[k]]);
                            //cout << abs(c[k]-l)<< " " ;
                        }
                        
                    }
                    
                    
                }	
            }
            
        }
        
    }
    
    int answer = 2;
    rep(i,200)
        answer = max(answer,dp[d][i]);
    cout << answer << endl;
    
    return 0;
}

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

#define D 200
#define N 200

#define max(a,b) (a>b?a:b)

int main() {
	int d, n, t[D+1], a[N], b[N], c[N], dp[D+1][N], ans=0;
	cin >> d >> n;
	for(int i=1; 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<n; ++i) dp[0][i] = (a[i]<=t[0]&&t[0]<=b[i]?0:-1);
	for(int i=1; i<=d; ++i) {
		for(int j=0; j<n; ++j) {
			dp[i][j] = 0;
			if(a[j]<=t[i]&&t[i]<=b[j]) {
				for(int k=0; k<n; ++k) {
					if(dp[i-1][k]!=-1) {
						dp[i][j] = max(dp[i][j], dp[i-1][k] + abs(c[j]-c[k]));
					}
				}
			}else dp[i][j] = -1;
		}
	}
	for(int i=0; i<n; ++i) ans = max(ans, dp[d][i]);
	cout << ans << endl;
	return 0;
}

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

int myabs(int x){
	if(x>0){
		return x;
	}
	else{
		return -x;
	}
}

int main(){
	int D,N;
	cin>>N>>D;
	int Dbox[200],Ubox[200],Sbox[200],Hbox[200];
	for(int i=0;i<N;i++){
		cin>>Dbox[i];
	}
	for(int i=0;i<D;i++){
		cin>>Sbox[i]>>Ubox[i]>>Hbox[i];
	}

	int Fuku[200][200];
	for(int i=0;i<N;i++){
		for(int j=0;j<D;j++){
			Fuku[i][j]=-1;
		}
	}
	int cnt,kaz;
	//0つめ
	for(int cfuku=0;cfuku<D;cfuku++){
		if(Dbox[0]<=Ubox[cfuku]&&Dbox[0]>=Sbox[cfuku]){
			Fuku[0][cfuku]=0;
		}
	}
	//１から
	for(int day=1;day<N;day++){
		for(int cfuku=0;cfuku<D;cfuku++){
			if(Dbox[day]<=Ubox[cfuku]&&Dbox[day]>=Sbox[cfuku]){
				cnt=0;
				for(int men=0;men<D;men++){
					if(Fuku[day-1][men]!=-1){
						if(cnt<=myabs(Hbox[men]-Hbox[cfuku])){
							cnt=myabs(Hbox[men]-Hbox[cfuku]);
							kaz=men;
						}
					}
				}
				Fuku[day][cfuku]=cnt+Fuku[day-1][kaz];
			}
		}
	}
	cnt=0;
	for(int cfuku=0;cfuku<D;cfuku++){
		if(cnt<Fuku[N-1][cfuku]) cnt=Fuku[N-1][cfuku];
	}
	cout<<cnt<<endl;
	return 0;
}