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>
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[201],Ubox[201],Sbox[201],Hbox[201];
	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;
}

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

int main() {
	int da, n,p=0,q=0;
	cin >> da >> n;
	int hot[210] = {0};
	int wear[3][210];
	int d[210][210] = {0};
	int kiroku[2][210] = {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 <string>
#include <vector>
#include <list>
#include <map>
#include <cmath>

using namespace std;

int main()
{
    int d, n;
    int dp[200][200]={0};
    int temp[200];
    int cloth[200][3];
    int m;
    list<int> suit;
    cin >> d >> n;
    for(int i=0;i<d;++i) cin >> temp[i];
    for(int i=0;i<n;++i) cin >> cloth[i][0] >> cloth[i][1] >> cloth[i][2];
    for(int i=0;i<n;++i)
    {
        if(!(cloth[i][0] <= temp[0] && temp[0] <= cloth[i][1])) dp[0][i] = -400000;
    }
    for(int i=1;i<d;++i)
    {
        for(int j=0;j<n;++j)
        {
            m=0;
            for(int k=0;k<n;++k)
            {
                if(cloth[j][0] <= temp[i] && temp[i] <= cloth[j][1])
                {
                    m = max(m, dp[i-1][k] + abs(cloth[k][2]-cloth[j][2]));
                }
            }
            dp[i][j] = m;
        }
    }
    m=0;
    for(int i=0;i<n;++i) m = max(m,dp[d-1][i]);
    cout << m << endl;
}

C++: 
#include<stdio.h>
#include<math.h>
#include<algorithm>
#include<cctype>
#include<string.h>
#include <stdlib.h>

#define PI 3.1415926
int max(int n, int m);

int main() {
	int D, N, T[400], A[400], B[400], C[400], X[20][400] = { 0 };
	int F[10][400] = { 0 };
	int f[10][400];
	int ans = 0;
	scanf("%d %d", &D, &N);
	for (int i = 1; i <= D; i++) {
		scanf("%d", &T[i]);
	}
	for (int i = 1; i <= N; i++) {
		scanf("%d %d %d", &A[i], &B[i], &C[i]);
	}
	for (int i = 1; i <= D+100; i++) {
		X[1][i] = 100;
	}
	for (int i = 1; i <= D; i++) {
		for (int j = 1; j <= N; j++) {
			if (A[j] <= T[i] && B[j] >= T[i] && C[j] > X[0][i]) {
				X[0][i] = C[j];
			}
			if (A[j] <= T[i] && B[j] >= T[i] && C[j] < X[1][i]) {
				X[1][i] = C[j];
			}
		}
		//printf("%d %d %d\n", i, X[0][i], X[1][i]);
	}
	f[0][1] = 0;
	f[1][1] = 1;
	for (int i = 2; i <= D; i++) {
		if (F[0][i-1]+abs(X[0][i] - X[0][i - 1]) > F[1][i-1]+abs(X[0][i] - X[1][i - 1])) {
			F[0][i] = F[0][i - 1] + abs(X[0][i] - X[0][i - 1]);
		}
		else {
			F[0][i] = F[1][i - 1] + abs(X[0][i] - X[1][i - 1]);
		}
		if (F[0][i-1]+abs(X[1][i] - X[0][i - 1]) >F[1][i-1]+ abs(X[1][i] - X[1][i - 1])) {
			F[1][i] = F[0][i - 1] + abs(X[1][i] - X[0][i - 1]);
		}
		else {
			F[1][i] = F[1][i - 1] + abs(X[1][i] - X[1][i - 1]);
		}
		//printf("%d %d\n", F[0][i], F[1][i]);
	}

	printf("%d\n",max(F[0][D],F[1][D]));
	return 0;
}

int max(int n, int m) {
	if (n >= m) {
		return n;
	}
	else {
		return m;
	}
}

C++: #include<bits/stdc++.h>


using namespace std;

int dp[200][101];
int d, n, days[200], ans = 0;
struct cloth{
  int a, b, c;
};
cloth clothes[200];

int dfs(int day, int point){
  if(day == d)
    return 0;
  if(~dp[day][point])
    return dp[day][point];
  int res = 0;
  for(int i = 0; i < n; i++){
    if(clothes[i].a <= days[day] && days[day] <= clothes[i].b){
      res = max(res, abs(point - clothes[i].c)+ dfs(day + 1, clothes[i].c));
    }
  }
  return dp[day][point] = res;
}

int main(){
  cin.tie(0);
  ios::sync_with_stdio(0);
  cin >> d >> n;
  for(int i = 0; i < d; i++){
    cin >> days[i];
  }
  for(int i = 0; i < n; i++){
    cin >> clothes[i].a >> clothes[i].b >> clothes[i].c;
  }
  memset(dp, -1, sizeof(dp));
  for(int i = 0; i < n; i++){
    if(clothes[i].a <= days[0] && days[0] <= clothes[i].b){
      ans = max(ans, dfs(1, clothes[i].c));
    }
  }
  cout << ans << "\n";
}

C++: #include <iostream>
using namespace std;
int tem[201],G[201][3],D,N,DP[201][201]={0};
	
int main() {
	cin >> D >> N ;
	for (int i=1;i<=D;i++){
		cin>>tem[i];
	}
	for(int i=0;i<N;i++){
		for(int j=0;j<3;j++){
			cin >> G[i][j];

		}
		if(!(G[i][0]<=tem[1] && tem[1]<=G[i][1]))
			DP[i][1]=-9999;
	}

	for(int i=2;i<=D;i++){
		for(int j=0;j<N;j++){
			if(G[j][0]<=tem[i] && tem[i]<=G[j][1]){
			for(int k=0;k<N;k++){
			DP[j][i]=max(DP[j][i],DP[k][i-1]+abs(G[k][2]-G[j][2]));
			}
			}
		}
	}
	int maxer=-1;
	for (int i=0;i<N;i++){
		maxer=max(maxer,DP[i][D]);
	}
	cout << maxer << endl; 
}