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>
using namespace std;

#define llong long long 
typedef struct data{
	int low;
	int high;
	int eva;
}data;
int D, N;
int temp[201];
data kind[200];
int score[201][200][200];

int solve(int d, int k, int yk, int e){
	if(!(d < 2)) e += fabs(kind[yk].eva - kind[k].eva);
	//cout << d << " " << k << " " << e << "   " << score[d][yk][k] << endl;
	if(e != -1 && e < score[d][yk][k]) return score[d][yk][k];

	if(d == D) return score[d][yk][k] = max(e, score[d][yk][k]);
	
	if(d == 1) e = 0;
	int ret = -1;
	for(int i = 0; i < N; i++){
		data use = kind[i];
		if(use.low > temp[d + 1] || use.high < temp[d + 1]) continue;
		ret = max(ret, solve(d + 1, i, k, e));
	}

	return score[d][yk][k] = max(score[d][yk][k], ret);
}

int main(){
	fill_n(temp, 201, -1);
	fill_n(**score, 201 * 200 + 200, 0);
	cin >> D >> N;
	for(int i = 1; i <= D; i++){
		cin >> temp[i];
	}
	for(int i = 0; i < N; i++){
		cin >> kind[i].low >> kind[i].high >> kind[i].eva;
	}

	cout << solve(0, 0, 0, -1) << endl;
	return 0;
}

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

int d,n,t[201],data[201][2],dp[201][2];

int main(){
	std::cin>>d>>n;
	for(int i=0;i<d;i++)std::cin>>t[i],data[i][1]=1000000;
	for(int i=0;i<n;i++){
		int a,b,c;
		std::cin>>a>>b>>c;
		for(int j=0;j<d;j++){
			if(a<=t[j]&&t[j]<=b)data[j][0]=std::max(data[j][0],c),data[j][1]=std::min(data[j][1],c);
		}
	}
	for(int i=0;i<d-1;i++){
		for(int j=0;j<2;j++){
			dp[i][j]=std::max(dp[i-1][0],dp[i-1][1])+std::max(std::abs(data[i][j]-data[i+1][0]),std::abs(data[i][j]-data[i+1][1]));
		}
	}
	std::cout<<std::max(dp[d-2][0],dp[d-2][0])<<std::endl;
	return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;
int a[1999],b[1999],c[1999],D,N,t[1999],da[1999];
int main(){
    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];
    }
    int huku,ka=0;
    for(int i=0;i<D;i++){
         huku=0;
         int kari=0;
         if(ka==0){kari=98798798;}
        for(int j=0;j<N;j++){
            if(a[j]<=t[i]&&t[i]<=b[j]&&ka==1)kari=max(kari,c[j]);
            if(a[j]<=t[i]&&t[i]<=b[j]&&ka==0)kari=min(kari,c[j]);
        }
        da[i]=kari;
        huku+=kari;
        if(ka==0){ka=1;}else
        if(ka==1){ka=0;}
    }
    int ze=0;
    for(int h=1;h<D;h++){
        int hk=0;
        hk=da[h]-da[h-1];
        if(hk<0)hk=hk*-1;
        ze+=hk;
    }
    cout<<ze<<endl;




}

C++: 
#include<iostream>

using namespace std;

int calc_abso(int a,int b){
    int abso;
    if(a>=b){
        abso=a-b;
    }else if(a<b){
        abso=b-a;
    }
    return abso;
}

struct WEAR{
    int min;        //適す最高気温(下限)
    int max;        //適す最高気温(上限)
    int looks;      //派手さ
};

int dp[256][256]={{0}};    //[日数][服の種]    //値は派手さの絶対値の合計（一番大きいものを最終的に取るように）


int main (void){
    int D,N;
    WEAR status[256];
    int tempe[256];


    for(int i=0;i<D;i++){
        cin >> tempe[i];
    }
    for(int i=0;i<N;i++){
        cin >> status[i].min >> status[i].max >> status[i].looks;
    }

    for(int i=1;i<D;i++){
        for(int j=0;j<N;j++){
            if((status[j].min<tempe[j])&&(status[j].max>tempe[j])){      //この服が今日適しているか？
                int MAX_tortal=-1;    //前日の服と差の最大値
                int MAX_num;
                for(int k=0;k<N;k++){   //前日の服
                        int THIS_tortal=calc_abso(status[N].looks , status[k].looks)+dp[i][k];
                        if(THIS_tortal>MAX_tortal){
                            MAX_tortal=THIS_tortal;
                            MAX_num=k;
                        }

                }
                dp[i][j]+=MAX_tortal;
            }
        }
    }

    int LAST_MAX_tortal=-1;
    for(int i=0;i<N;i++){
        if(dp[D-1][i]>LAST_MAX_tortal){
            LAST_MAX_tortal=dp[D-1][i];
        }
    }

    cout << LAST_MAX_tortal << endl;


    return 0;
}

C++: #include<bits/stdc++.h>
#define FOR(i,a,b) for(int i=a;i<=b;i++)
static const int INF = 8931919114514810;
static const int MAX_N = 300;
using namespace std;

int A[300],B[300],C[300];
int mem[MAX_N][MAX_N];
int N,D;
int Day[MAX_N];
int cal(int day,int pp){
   int res=0;
   if(mem[day][pp]!=-1){
        return mem[day][pp];
   }
   if(day==N+1){
        return res=0;
   }
   if(day<=1){
       FOR(i,1,D){
            if(Day[day]>=A[i]&&Day[day]<=B[i]){
            res=max(cal(day+1,C[i]),res);
         }
      }
   }
   else{
        FOR(i,1,D){
            if(Day[day]>=A[i]&&Day[day]<=B[i]){
                int temp=abs(pp-C[i]);
                res=max(cal(day+1,C[i])+temp,res);
            }
        }
   }
   return mem[day][pp]=res;
}

int main(){
    cin>>N>>D;
    memset(mem,-1,sizeof(mem));
    FOR(i,1,N){
        cin>>Day[i];
    }
    FOR(i,1,D){
        cin>>A[i]>>B[i]>>C[i];
    }
    cout<<cal(1,0)<<endl;
    return 0;
}

C++: #include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <iostream>
using namespace std;
int main()
{
	int d,n;
	cin >> d >> n;
	int dp[205][105];
	for(int i=0;i<205;i++) for(int j=0;j<105;j++) dp[i][j]=-1e7;
	int temp[205];
	int a[205],b[205],c[205];
	for(int i=1;i<=d;i++)
	{
		cin >> temp[i];
	}
	for(int i=1;i<=n;i++)
	{
		cin >> a[i]>> b[i] >> c[i];
		if(a[i] <= temp[1] && temp[1] <= b[i])
		{
			dp[1][c[i]]=max(dp[1][c[i]],0);
		}
	}
	for(int i=2;i<=d;i++)
	{
		for(int j=1;j<=n;j++)
		{
			if(a[j] <= temp[i] && temp[i] <= b[j])
			{
				for(int k=0;k<=100;k++) dp[i][c[j]]=max(dp[i][c[j]],dp[i-1][k]+abs(k-c[j]));
			}
		}
	}
	int val=-1e7;
	for(int k=0;k<=100;k++) val=max(val,dp[d][k]);
	cout << val << endl;
}

C++: #include <stdio.h>
#include <algorithm>

int D, N, T[212], A[212], B[212], C[212];
int dp[212][212], ans;

int main(void) {
	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]); }

	for (int i = 0; i < D; ++i) {
		for (int j = 0; j < N; ++j) {
			dp[i][j] = -1;
		}
	}
	for (int i = 0; i < N; ++i) { dp[0][i] = 0; }
	for (int i = 0; i < D; ++i) {
		for (int j = 0; j < N; ++j) {
			if (dp[i][j] >= 0) {
				for (int k = 0; k < N; ++k) {
					if (A[k] <= T[i] && T[i] <= B[k]) {
						dp[i + 1][k] = std::max(dp[i + 1][k], dp[i][j] + abs(C[j] - C[k]));
					}
				}
			}
		}
	}

	ans = -1;
	for (int i = 0; i < D; ++i) {
		ans = std::max(ans, dp[D - 1][i]);
	}
	printf("%d\n", ans);
	return 0;
}