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 <iostream>
#include <cstdio>
#include <cmath>
#include <vector>
#include <map>
#include <stack>
#include <queue>
#include <algorithm>
#include <set>

#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define REP(i,j) FOR(i,0,j)

const int INF = 1 << 24;

typedef unsigned long long ull;

int main(){
    int N, D;// D: days
    std::cin >> D >> N;

    int T[210], A[210], B[210], C[210];
    REP(i, D){
        std::cin >> T[i];
    }

    REP(i, N){
        std::cin >> A[i] >> B[i] >> C[i]; // <=, value
    }

    int dp[210][210];
    REP(i, D){
        REP(i, N){
            dp[D][N] = 0;
        }
    }

    REP(i, N){
        if(A[i] <= T[0] && T[0] <= B[i]){
            dp[0][i] = 0;
        }else{
            dp[0][i] = -INF;
        }
    }

    FOR(i, 1, D){
        REP(j, N){
            if(A[j] <= T[i] && T[i] <= B[j]){
                REP(k, N){
                    dp[i][j] = std::max(dp[i][j], dp[i-1][k] + std::abs(C[k] - C[j]));
                }
            }else{
                dp[i][j] = -INF;
            }
        }
    }

    // REP(i, D){
    //     REP(j, N){
    //         printf("%d ", dp[i][j]);
    //     }
    //     puts("");
    // }
    
    int res = 0;
    REP(i, N){
        res = std::max(res, dp[D-1][i]);
    }

    std::cout << res << std::endl;
}

C++: #include<iostream>
#include<string>
#include<cstdio>
#include<vector>
using namespace std;
int main(){
	int d, n;
	cin >> d >> n;
	int h[205];
	for (int i = 1; i <= d; i++){
		cin >> h[i];
	}
	int s[205][4];
	for (int i = 0; i < n; i++){
		int aa, ab, ac;
		cin >> aa >> ab >> ac;
		s[i][1] = aa;
		s[i][2] = ab;
		s[i][0] = ac;
		
	}
	int dp[205][205] = { 0 };
	for (int i = 0; i < n; i++){
		if (s[i][1] <= h[1] && s[i][2] >= h[1]){
			for (int j = 0; j < n; j++){
				if (s[j][1] <= h[2] && s[j][2] >= h[2]){
					int a;
					if (s[i][0]>s[j][0]){
						a = s[i][0] - s[j][0];
					}
					else{
						a = s[j][0] - s[i][0];
					}
					if (dp[2][j] < a){
						dp[2][j] = a;
					}
				}

			}
		}
		
	}
	

	for (int i = 3; i <=d; i++){
		for (int j = 0; j < n; j++){
			if (s[j][1] <= h[i] && h[i] <= s[j][2]){
				int max = 0;
				for (int l = 0; l < n; l++){
					if (dp[i - 1][l] != 0){
						int ba;
						if (s[j][0]>s[l][0]){
							ba = dp[i - 1][l] + s[j][0] - s[l][0];
						}
						else{
							ba = dp[i - 1][l] + s[l][0] - s[j][0];
						}
						if (ba > max || max == 0){
							max = ba;
						}
					}

				}
				dp[i][j] = max;
			}
			
		}
		
	}
	int max2 = 0;
	for (int i = 0; i < n; i++){
		if (max2 < dp[d][i] || max2 == 0){
			max2 = dp[d][i];
		}
	}
	cout << max2 << endl;
	return 0;
}

C++: #include<stdio.h>
#include<stdlib.h>
int s[200][200],r[200][200],t[200],a[200],b[200],c[200],d[200];
int main(void)
{
	int m,n,i,k,h,x;
	scanf("%d %d",&n,&m);
	for(i=0;i<n;i++) scanf("%d",&t[i]);
	for(i=0;i<m;i++) scanf("%d %d %d",&a[i],&b[i],&c[i]);
	for(i=0;i<n;i++){
		for(k=0;k<m;k++) s[i][k]=0;
	}
	for(i=0;i<n;i++){
		d[i]=0;
		for(k=0;k<m;k++){
			if(a[k]<=t[i]&&t[i]<=b[k]){
				r[i][d[i]]=c[k];
				d[i]++;
			}
			
		}
	}
	for(i=1;i<n;i++){
		for(k=0;k<d[i];k++){
			for(h=0;h<d[i-1];h++){

				if(s[i][k]<abs(r[i][k]-r[i-1][h])+s[i-1][h]){
					s[i][k]=abs(r[i][k]-r[i-1][h])+s[i-1][h];
				}			
			}
		}
	}
	x=0;
	for(i=0;i<d[n-1];i++){
		if(x<s[n-1][i]) x=s[n-1][i];
	}
	printf("%d\n",x);
	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 res=0;
int cal(int day,int pp){
   if(day==N+1){
        return res=0;
   }
   if(mem[day][pp]!=-1){
        return mem[day][pp];
   }
   if(day<=1){
       FOR(i,1,D){
            if(Day[day]>=A[i]&&Day[day]<=B[i]){
            res=cal(day+1,C[i]);
         }
      }
   }
   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<bits/stdc++.h>
using namespace std;

int main()
{
    int d,n;
    cin >> d >> n;
    int t[d];
    for(int i=0;i<d;i++)
        cin >> t[i];
    int ma[n],mi[n],a[n];
    for(int i=0;i<n;i++)
        cin >> mi[i] >> ma[i] >> a[i];
    int b[d][n]={},dp[d][n]={};
    for(int i=0;i<d;i++)
        for(int j=0;j<n;j++)
            if(ma[j]>=t[i]&&mi[j]<=t[i])
                b[i][j]=1;//1..ok
    for(int i=0;i<d-1;i++)
        for(int j=0;j<n;j++){
            if(b[i+1][j]==1){
                int m=0;
                for(int k=0;k<n;k++){
                    if(b[i][k]==1){
                        m=max(m,abs(a[j]-a[k])+dp[i][k]);
                    }
                }
                dp[i+1][j]=m;
            }
        }
    int ans=0;
    for(int i=0;i<n;i++)
        ans=max(ans,dp[d-1][i]);
    cout << ans << endl;
    return 0;
}