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 <vector>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <algorithm>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <string>
#include <cstring>
#include <complex>
#include <ctime>
#include <cstdlib>

using namespace std;

inline int to_int(string s) {int v; istringstream sin(s); sin >> v; return v;}
template<class T> inline string to_str(T x) {ostringstream sout; sout << x; return sout.str();}

typedef long long ll;

int main()
{
	int d, n, T[64], low[256], hi[256], M[256];
	int dp[256][256]; //dp[i][j] : i日目に服jを着た時の派手さの最大値

	cin >> d >> n;

	for(int i = 0; i < d; i++)
	{
		cin >> T[i];
	}
	for(int i = 0; i < n; i++)
	{
		cin >> low[i] >> hi[i] >> M[i];
	}	

	for(int i = 0; i < 200; i++)
	{
		for(int j = 0; j < 200; j++)
		{
			dp[i][j] = 0;
		}
	}

	int res = 0;

	for(int i = 1; i < d; i++)
	{
		for(int j = 0; j < n; j++)
		{
			if(T[i] >= low[j] && T[i] <= hi[j])
			{
				for(int k = 0; k < n; k++)
				{
					if(low[k] <= T[i-1] && hi[k] >= T[i-1])
					{
						dp[i][j] = max(dp[i][j], abs(M[j]-M[k])+ dp[i-1][k]);
						res = max(res, dp[i][j]);
					}
				}
			}
		}
	}

	cout << res << endl;
}

C++: #include<iostream>
#include<algorithm>
#include<cmath>
#include<cstdlib>
#define reep(num,num2,num3) for(int num=num3;num<num2;num++)
#define rep(num,num2) for(int num=0;num<num2;num++)
using namespace std;
int dp[1024][1024];
int D[1024];
int ht[1024];
int lt[1024];
int C[1024];
int main()
{
    int d,n;
    cin>>d>>n;
    rep(i,d){
        int t;
        cin>>t;
        D[i]=t;
    }
    rep(i,n){
        int h,l,c;
        cin>>h>>l>>c;
        lt[i]=h;
        ht[i]=l;
        C[i]=c;
    }
    rep(i,1024)
        rep(j,1024)dp[i][j]=-1;
    rep(i,n)if(ht[i]>=D[0]&&D[0]>=lt[i])dp[0][i]=0;
    rep(i,d-1)
        rep(j,n)
            if(dp[i][j]!=-1)
                rep(k,n)
                    if(ht[k]>=D[i+1]&&D[i+1]>=lt[k]&&dp[i+1][k]<dp[i][j]+abs(C[k]-C[j]))dp[i+1][k]=dp[i][j]+abs(C[k]-C[j]);
    int res=-1;
    rep(i,n)res=max(res,dp[d-1][i]);
    cout<<res<<endl;
}

C++: #include<bits/stdc++.h>
using namespace std;
int a,b,c,d,n,memo[500],a1,a2,m1,m2;
vector <pair<int,int> > updown;
vector <int> day;
vector <pair<int,int> > huku;
int solve(int dp,int one,int sum);


int main(){

    cin >> d >> n;
    for(int i=0;i<d;i++){
        cin >> a;
        day.push_back(a);
    }
    for(int i=0;i<n;i++){
        cin >> a >> b >> c;
        updown.push_back(make_pair(a,b));
        huku.push_back(make_pair(c,i));
    }

    std::sort(huku.begin(),huku.end());

    for(int i=0;i<n;i++){
            if(updown[huku[i].second].second>=day[0]&&day[0]>=updown[huku[i].second].first){
                m1=solve(1,huku[i].first,0);
                break;
            }
        }
    for(int i=n-1;i>-1;i--){
            if(updown[huku[i].second].second>=day[0]&&day[0]>=updown[huku[i].second].first){
                m2=solve(1,huku[i].first,0);
                break;
            }
        }


    cout << max(m1,m2) << endl;

return 0;
}


int solve (int dp,int one,int sum){

    int s1=0,s2=0;
    if(dp==d)return sum;
    else {
        if(memo[dp]!=0&&memo[dp]==sum)return memo[dp];

        for(int i=0;i<n;i++){
            if(updown[huku[i].second].second>=day[dp]&&day[dp]>=updown[huku[i].second].first){
                int sa=max(one,huku[i].first)-min(one,huku[i].first);
                s1=solve(dp+1,huku[i].first,sum+sa);
            }
        }
        for(int i=n-1;i>-1;i--){
            if(updown[huku[i].second].second>=day[dp]&&day[dp]>=updown[huku[i].second].first){
                int sa=max(one,huku[i].first)-min(one,huku[i].first);
                s2=solve(dp+1,huku[i].first,sum+sa);
            }
        }
        return memo[dp]=max(s1,s2);
    }

}

C++: #include <iostream>
#include <fstream>
#include <math.h>

using namespace std;
int T[200], A[200], B[200], C[200], D, N;
int Cs[200][200], CsNumber[200], valueCs[200][200];

inline void readinput(){
	//ifstream fin("input_2012_4.dat");
	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];
}

inline void output_2d(int X[200][200]){
	for(int i=0; i<D; i++){
		for(int j=0; j<N; j++){
			cout << X[i][j];
			if(j != N-1) cout << " ";
			else cout << endl;
		}
	}
}

inline void putCs(){
	int idx;
	for(int i=0; i<D; i++){
		idx = 0;
		for(int j=0; j<N; j++){
			if(A[j]<=T[i] && T[i]<=B[j]){
				Cs[i][idx] = C[j];
				idx += 1;
			}
		CsNumber[i] = idx;
		}
	}
}

inline int solver(){
	int max, maxidx, buf;
	
	//initialize
	for(int i=0; i<200; i++){
		for(int j=0; j<200; j++) valueCs[i][j] = 0;
	}
	
	for(int i=D-1; i>0; i--){	// D-1 to 1
		for(int j1=0; j1<CsNumber[i-1]; j1++){
			max = 0;
			for(int j2=0; j2<CsNumber[i]; j2++){
				buf = fabs(Cs[i-1][j1] - Cs[i][j2]) + valueCs[i][j2];
				if(max < buf){
					max = buf;
					maxidx = j2;
				}
			}
			valueCs[i-1][j1] += max;
		}
	}
	max = 0;
	for (int j=0; j<CsNumber[0]; j++){
		buf = valueCs[0][j];
		if(max < buf) max = buf;
	}
	return max;
}

int main(){
	readinput();
	putCs();
	cout << "answer: " << solver() << endl;
//	cout << "--- Cs ---" << endl;
//	output_2d(Cs);
//	cout << "--- valueCs ---" << endl;
//	output_2d(valueCs);
	return 0;
}

C++: #include<iostream>
#include<vector>
#include<algorithm>

using namespace std;

int main(void){

  int d,n,t,a,b,c,dp[202][202];
  vector<int>T,A,B,C;

  cin >> d >> n;
  
  for(int i=0;i<d;i++){
    cin >> t;
    T.push_back(t);
  }
  
  for(int i=0;i<n;i++){
    cin >> a >> b >> c;
    A.push_back(a);
    B.push_back(b);
    C.push_back(c);
  }
  
  for(int i=0;i<202;i++)
    for(int j=0;j<202;j++)
      dp[i][j]=-1;
  
  for(int i=0;i<n;i++)
    for(int j=0;j<n;j++)
      if(A[i]<=T[0] && T[0]<=B[i] && A[j]<=T[1] && T[1]<=B[j])
	dp[0][j]=max(dp[0][j],abs(C[i]-C[j]));
  
  for(int i=1;i<=d;i++)
    for(int j=0;j<n;j++)
      for(int k=0;k<n;k++)
	if(A[k]<=T[i+1] && T[i+1]<=B[k] && dp[i-1][j]>=0)
	  dp[i][k]=max(dp[i][k],dp[i-1][j]+abs(C[j]-C[k]));
  
  int ans=0;
  for(int i=0;i<n;i++)
    ans=max(ans,dp[d-2][i]);
  
  cout << ans << endl;
  
  return 0;
}