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<bits/stdc++.h>
#define LL long long
#define FOR(i,a,b) for(int i=a;i<=b;++i)

using namespace std;

static const LL MAX_N = 1e+5;
static const LL INF = 1 << 25;

struct clothes{
    LL A[100],B[100],C[150];
};

int N,D;
LL res=-INF;
LL Day[300];
LL mem[300][160][160];
LL s_mem[1000000];
clothes T;

LL cal(LL day,LL p,LL pp,LL sum){
    if(mem[day][p][pp]!=0&&mem[day][p][pp]>=sum+abs(pp-p)){
        res=mem[day][p][pp];
    }
    else{
        if(res<sum+abs(pp-p)&&day==N+1)mem[day][p][pp]=res=sum+abs(pp-p);
        if(day==N+1)return 0;
        if(day==0){
            FOR(i,1,D)
            if(Day[1]>=T.A[i]&&Day[1]<=T.B[i])cal(day+1,T.C[i],p,sum);
        }
        FOR(i,1,D){
            if(Day[day]>=T.A[i]&&Day[day]<=T.B[i]){
                if(day>2)cal(day+1,T.C[i],p,sum+abs(pp-p));
                else cal(day+1,T.C[i],p,sum);
            }
        }
    }
    return res;
}


int main(){
    cin>>N>>D;
    fill(s_mem,s_mem,-1);
    FOR(i,1,N)cin>>Day[i];
    FOR(i,1,D)
        cin>>T.A[i]>>T.B[i]>>T.C[i];
    cout<<cal(0,0,0,0)<<endl;
}

C++: #include<iostream>
#include<cstdlib>

using namespace std;

int main(){
  int d, n;
  cin >> d >> n;
  int t[d+1], c[n][3];
  for(int i = 1; i < d+1; i++) cin >> t[i];
  for(int i = 0; i < n; i++) cin >> c[i][0] >> c[i][1] >> c[i][2];

  int dp[n][d+1];
  for(int i = 0; i < n; i++) dp[i][0] = -1;
  for(int i = 0; i < n; i++){
    if(c[i][0] <= t[1] && t[1] <= c[i][1]){
      //dp[i][1] = c[i][2];
      dp[i][1] = 0;
    }else{
      dp[i][1] = -1;
    }
  }

  for(int i = 2; i < d+1; i++){
    for(int j = 0; j < n; j++){
      if(!(c[j][0] <= t[i] && t[i] <= c[j][1])){
        dp[j][i] = -1;
      }else{
        int max = -1;//, index;
        for(int k = 0; k < n; k++){
          if(dp[k][i-1] == -1) continue;
          if(max < dp[k][i-1] + abs(c[j][2] - c[k][2])){
            max = dp[k][i-1] + abs(c[j][2] - c[k][2]);
            //index = k;
          }
        }
        dp[j][i] = max;
      }
    }
  }

  /*cout << endl;
  for(int i = 0; i < n; i++){
    for(int j = 0; j < d+1; j++){
      cout << dp[i][j] << " ";
    }
    cout << endl;
  }*/
  int max = -1;
  for(int j = 0; j < n; j++){
    if(max < dp[j][d]) max = dp[j][d];
  }
  cout << max << endl;
}

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<iostream>
#include<algorithm>
#include<climits>
#include<cmath>
#include<cstdio>
#include<cstdlib>
#include<ctime>
#include<string>
#include<cstring>
#include<vector>
#include<stack>
#include<queue>
#include<set>
#include<bitset>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> i_i;

#define PI 3.141592653589793238462643383279
#define mod 1000000007LL
#define rep(i, n) for(i = 0;i < n;++i)
#define rep1(i, n) for(i = 1;i < n;++i)
#define per(i, n) for(i = n - 1;i > -1;--i)
#define int(x) int x; scanf("%d",&x)
#define int2(x, y) int x, y; scanf("%d%d",&x, &y)
#define int3(x, y, z) int x, y, z; scanf("%d%d%d",&x, &y, &z)
#define int4(v, x, y, z) int v, x, y, z; scanf("%d%d%d%d", &v, &x, &y, &z)
#define int5(v, w, x, y, z) int v, w, x, y, z; scanf("%d%d%d%d%d", &v, &w, &x, &y, &z)
#define ll2(x, y) ll x, y; cin >> x >> y;
#define scn(n, a) rep(i, n)cin >> a[i]
#define sc2n(n, a, b) rep(i, n)cin >> a[i] >> b[i]
#define pri(x) cout << (x) << "\n"
#define pri2(x, y) cout << (x) << " " << (y) << "\n"
#define pri3(x, y, z) cout << (x) << " " << (y) << " " << (z) << "\n"
#define pb push_back
#define mp make_pair
#define all(a) (a).begin(),(a).end()
#define endl "\n"
#define kabe puts("---------------------------")
#define kara puts("")
#define debug(x) cout << " --- " << (x) << "\n"
#define debug2(x, y) cout << " --- " << (x) << " " << (y) << "\n"
#define debug3(x, y, z) cout << " --- " << (x) << " " << (y) << " " << (z) << "\n"
#define X first
#define Y second
#define eps 0.0001
#define prid(x) printf("%.15lf\n", x)

struct wear{
  int a, b, c;
  void nxt(void){
    scanf("%d %d %d", &a, &b, &c);
    return;
  }
};

int t[200];
wear w[200];
int dp[200][101] = {0};

signed main(void){
  int i, j;
  for(int testcase = 0;testcase >= 0;testcase++){
    int2(d, n);
    scn(d, t);

    rep(i, d)rep(j, 101)dp[i][j] = -1;
    rep(i, n){
      w[i].nxt();
      if(w[i].a <= t[0] && t[0] <= w[i].b)dp[0][w[i].c] = 0;
    }

    rep(i, d - 1)rep(j, 101)if(dp[i][j] != -1){
      int k;
      rep(k, n)if(w[k].a <= t[i + 1] && t[i + 1] <= w[k].b)
      dp[i + 1][w[k].c] = max(dp[i + 1][w[k].c], dp[i][j] + abs(w[k].c - j));
    }
    int res = 0;
    rep(i, 101)res = max(res, dp[d - 1][i]);
    pri(res);

/*/

//*/ break;
  }
  return 0;
}

C++: #include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue>
using namespace std;
typedef pair<int,int> P;
#define make_pair mp
#define first f
#define second s
int dp[205][105];
int cloth[205][3];
int temp[205],n,d;
int main(){
memset(dp,-1,sizeof(dp));
scanf("%d %d",&d,&n);
for(int i=1;i<=d;i++){
	scanf("%d",&temp[i]);
}
for(int i=1;i<=n;i++){
	scanf("%d %d %d",&cloth[i][0],&cloth[i][1],&cloth[i][2]);
}
for(int i=1;i<=n;i++){
	if(cloth[i][0]<=temp[1] && temp[1]<=cloth[i][1]){
		dp[1][cloth[i][2]]=0;
	}
}
for(int i=2;i<=d;i++){
	for(int j=1;j<=n;j++){
		if(cloth[j][0]<=temp[i] && temp[i]<=cloth[j][1]){
			for(int h=0;h<=100;h++){
				if(dp[i-1][h]!=-1){
					dp[i][cloth[j][2]]=max(dp[i][cloth[j][2]],dp[i-1][h]+max(cloth[j][2],h)-min(h,cloth[j][2]));
				}
			}
		}
	}
}
int ans=0;
for(int i=0;i<=100;i++){
	ans=max(ans,dp[d][i]);
}
printf("%d\r\n",ans);
return 0;
}