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

const int INF = -100000000;

int main(){
  int d,n,a[200],b[200],c[200],t[200];
  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);
  int dp[200][200];
  fill(dp[0],dp[0]+200,INF);
  for(int i=0;i<d;i++){
    for(int j=0;j<n;j++){
      if(i==0){
        if(a[j]<=t[i]&&t[i]<=b[j]){
          dp[i][j] = 0;
        }
        continue;
      }
      dp[i][j] = 0;
      for(int k=0;k<n;k++){
        if(a[j]<=t[i]&&t[i]<=b[j]){
          dp[i][j] = max(dp[i][j],dp[i-1][k]+max(c[j],c[k])-min(c[j],c[k]));
        }
      }
    }
  }
  int ans = 0;
  for(int i=0;i<n;i++) ans = max(ans,dp[d-1][i]);
  printf("%d\n",ans);
  return 0;
}

C++: #include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <map>
#include <sstream>
#include <functional>
#include <numeric>
#include <cmath>
#include <cstring>
#include <queue>
#include <stack>
#include <set>

using namespace std;

#define ALL(co) co.begin(), co.end()

typedef long long LL;
typedef pair<int, int> P;
typedef pair<int, P> IP;
typedef pair<P, P> PP;
typedef vector<int> Array;
typedef vector<int> LArray;

const int INF = 1 << 29;
const LL LINF = 1LL << 60;

int itiv;
inline int getInt() { return (cin >> itiv, itiv); }
void readAll(Array& vec, int n) { for (int i = 0; i < n; i++) cin >> vec[i]; }
inline bool between(int first, int last, int n) { return first <= n && n <= last; }
inline bool inRange(int begin, int end, int n) { return begin <= n && n < end; }
inline bool inRange(int size, int n) { return 0 <= n && n < size; }

int d, n;
int dx[] = { -1, 0, 1, 0 }, dy[] = { 0, -1, 0, 1 };
int dr[] = { 0, -1, 0, 1 }, dc[] = { -1, 0, 1, 0 };

// day, cloth
int dp[201][201];
int main(void)
{
	while(cin >> d >> n && n)
	{
		Array t(d), a(n), b(n), c(n);
		readAll(t, d);
		for (int i = 0; i < n; i++)
			cin >> a[i] >> b[i] >> c[i];
		memset(dp, 0, sizeof(dp));

		int ans = 0;
		for (int i = 1; i < d; i++)//日にち
		{
			for (int j = 0; j < n; j++)//今日着る
			{
				if(!between(a[j], b[j], t[i])) continue;

				for (int k = 0; k < n; k++)//昨日着た
				{
					if(!between(a[k], b[k], t[i-1])) continue;

					dp[i][j] = max(dp[i][j], dp[i - 1][k] + abs(c[j] - c[k]));
					ans = max(ans, dp[i][j]);
				}
			}
		}
		cout << ans << endl;
	}
	return 0;
}

C++: #include<iostream>
using namespace std;
int main(void)
{
    int da,No,ti[201],max1[201],low1[201],cloth[201],A=0,e,f;
    int kawa[201][201],A3[201][201];
    cin>>da>>No;
    for(int i=0;i<da;i++){
        cin>>ti[i];
    }
    for(int i=0;i<No;i++){
        cin>>low1[i]>>max1[i]>>cloth[i];
    }
    for(int i=0;i<da;i++){
            int havea=0;
        for(int j=0;j<No;j++){
            if(low1[j]<=ti[i]&&max1[j]>=ti[i]){
                if(i!=0){
                    for(int k=0;k<No;k++){
                            e=kawa[i-1][k]-cloth[j];
                            if(e<0)e*=-1;
                          //  cout<<e<<" "<<i<<"\n";
                        if(havea<e&&A3[i-1][k]==1){
                                havea=e;
                               // cout<<i<<" ";
                        }
                    }//cout<<"\n";
                }
                A3[i][j]=1;
                    kawa[i][j]=cloth[j];
            }
            else {
                    A3[i][j]=0;
                    kawa[i][j]=0;
            }
        }
        //cout<<"\n";
        A+=havea;
    }
    cout<<A<<"\n";

}

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

int D, N;
int T[200];
int A[200], B[200], C[200];
// suit[i][j]: i日目に服jが着れるか
bool suit[200][200];
// dp[i][j]: i日目までで当日に服jを選んだ場合の派手さの差の絶対値の合計
int dp[201][201];

int main()
{
	std::cin >> D >> N;
	for( int i = 0; i < D; i++ )
		std::cin >> T[i];

	for( int j = 0; j < N; j++ )
		std::cin >> A[j] >> B[j] >> C[j];

	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; i++ )
		for( int j = 0; j < N; j++ )
			dp[i][j] = INT_MIN;

	// 1日目で着れる服は0
	for( int j = 0; j < N; j++ )
		if( suit[0][j] )
			dp[0][j] = 0;

	// i日目
	for( int i = 0; i < D - 1; i++ )
		// 当日に選んだ服j
		for( int j = 0; j < N; j++ )
			// 前日に選んだ服k
			for( int k = 0; k < N; k++ )
				if( suit[i+1][j] )
					dp[i+1][j] = std::max( dp[i+1][j], dp[i][k] + abs( C[j] - C[k] ) );

	int ans = 0;
	for( int j = 0; j < N; j++ )
		ans = std::max( ans, dp[D-1][j] );

	std::cout << ans << std::endl;

	return 0;
}

C++: #include<bits/stdc++.h>
int P[250][150],T[250],x[250][3],D,N,M,i,j,k;
main(){memset(P,-999999,sizeof(P));std::cin>>D>>N;
for(i=1;i<=D;i++){std::cin>>T[i];}
for(i=1;i<=N;i++){std::cin>>x[i][0]>>x[i][1]>>x[i][2];if(T[1]>=x[i][0] && T[1]<=x[i][1]){P[1][x[i][2]]=0;}}
for(i=2;i<=D;i++){for(j=1;j<=N;j++){if(T[i]>=x[j][0] && T[i]<=x[j][1]){
for(k=0;k<=100;k++){P[i][x[j][2]]=max(P[i][x[j][2]],P[i-1][k]+abs(x[j][2]-k));}}}}
for(i=0;i<=100;i++){M=max(M,P[D][i]);}std::cout<<M<<"\n";}

C++: #include<iostream>
#include<cassert>
#include<algorithm>
#include<vector>
#include<map>
#define REP(i,s,n) for(int i=s;i<n;i++)
#define rep(i,n) REP(i,0,n)
#define INF 10000000
#define S second
#define F first
using namespace std;

typedef pair<int,int> P;
typedef pair<P,int> Pi;
vector<int> vec[300];
int mex;
int D,N;
int premex = 0;
void rec(int day,int val,int total)
{

  //cout << "day = " << day << " va= " << val << " tootal = " << total << endl;
  for(int i=0;i<vec[day].size();i++)
    {
      if(day >= D-1)
	{
	  premex = max(premex,total+abs(val-vec[day][i]));
	  continue;
	}
      rec(day+1,vec[day][i],total+abs(val-vec[day][i]));
    }
}

int main()
{
 
  cin >> D >> N;
  //cout << "___ " << D << endl;
  vector<int> T;
  rep(i,D)
    {
      int input;
      cin >> input;
      T.push_back(input);
    }
  mex = 0;
  vector<Pi> ABC;
  rep(i,N)
    {
      int A,B,C;
      cin >> A >> B >> C;
      ABC.push_back(Pi(P(A,B),C));
    }

  rep(i,D)
    {
      rep(j,N)
	{
	  if(ABC[j].F.F <= T[i] && T[i] <= ABC[j].F.S)
	    {
	      vec[i].push_back(ABC[j].S); 
	    }
	}
    }
  /* 
  rep(i,D)
    {
      cout << i << " - th" << endl;
      rep(j,vec[i].size())
	cout << vec[i][j] << endl;
    }
  */

      for(int j=0;j<vec[0].size();j++)
	{
	  //cout << "INF" << endl;
	  rec(1,vec[0][j],0);
	}
      //cout << "premex = " << premex << endl;
      mex = max(mex,premex);
    
  cout << mex << endl;


  return 0;
}