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 <vector>
#include <algorithm>
#include <cmath>
#include <cstring>

const int INF = 33554432;

struct W
{
        int min, max;
        int c;
};

std::vector< W > ws;
std::vector< int > ts;
long dp[ 300 ][ 300 ];
int n;
int m;

long solve( int d, int before )
{
        if ( d == n )
        {
                return 0;
        }

        if ( dp[ d ][ before ] != -1 )
        {
                return dp[ d ][ before ];
        }


        long res = -INF;
        for ( int i = 0; i < m; ++i )
        {
                if ( ws[ i ].min <= ts[ d ] && ts[ d ] <= ws[ i ].max )
                {
                        if ( d == 0 )
                        {
                                res = std::max( res, solve( d + 1, i ) );
                        }
                        else {
                                res = std::max( res, solve( d + 1, i ) + std::abs( ws[ before ].c - ws[ i ].c ) );
                        }
                }
        }

        return dp[ d ][ before ] = res;
}

int main()
{
        memset( dp, -1, sizeof( dp ) );

        std::cin >> n >> m;
        ts.resize( n );
        ws.resize( m );
        for ( int i = 0; i < n; ++i )
        {
                std::cin >> ts[ i ];
        }
        for ( int i = 0; i < m; ++i )
        {
                std::cin >> ws[ i ].min >> ws[ i ].max >> ws[ i ].c;
        }

        
        std::cout << solve( 0, 0 ) << std::endl;
        
}

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



using namespace std;


int main (void)
{
    int P,wa[100], A, n, m, a[100], b[100], c[100], d[100],k[100][100],K[100][100];
    cin >> n >> m;

    for (int i = 0; i < n; i++)
    {
        cin >> a[i];
    }
    for (int j = 0; j < m; j++)
    {
        cin >> b[j] >> c[j] >> d[j];
    }

    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < m; j++)
        {
            if (b[j] < a[i] && a[i] < c[j])
            {
                k[i][j] = 0;
                k[i][j] = d[j];
            }
            else
            {
                k[i][j] = 0;
            }
        }
    }

    for (int j = 0; j < m; j++)
    {
        for (int i = 0; i < n - 1; i++)
        {
            K[i][j] = 0;
            K[i][j] = k[i][j] - k[i + 1][j];

            if (K[i][j] < 0)
            {
                P = 0;
                P = K[i][j];
                K[i][j] = 0;
                K[i][j] = P - P - P;
            }
        }
    }

    A = 0;
    for (int j = 0; j < m; j++)
    {
        wa[j] = 0;

        for (int i = 0; i < n - 1; i++)
        {
            wa[j] += K[i][j];
        }

        if (A < wa[j])
        {
            A = 0;
            A = wa[j];
        }
    }

    cout << A << endl;
    return 0;
}

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[MAX_N],B[MAX_N],C[MAX_N];
};

int N,D;
LL res=-INF;
LL Day[MAX_N];
LL mem[250][110][110];
clothes T;

LL cal(LL day,LL p,LL pp,LL sum){
    if(res<sum+abs(pp-p))res=sum+abs(pp-p);
    if(mem[day][p][pp]!=0)return mem[day][p][pp];
    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 mem[day][p][pp]=res;
}


int main(){
    cin>>N>>D;
    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<bits/stdc++.h>
using namespace std;

int ze(int a){
if(a<0)return -a;
return a;
}
int main(){
int a,b;
cin>>a>>b;
vector<int>c(a);
for(int d=0;d<a;d++)cin>>c[d];
vector<vector<int>>e(b,vector<int>(3));
for(int f=0;f<b;f++)cin>>e[f][0]>>e[f][1]>>e[f][2];
vector<vector<int>>dp(a+1,vector<int>(b));
for(int h=1;h<a;h++){
	for(int i=0;i<b;i++){
		if(c[h]>=e[i][0]&&c[h]<=e[i][1]){
			int s=0;
		for(int j=0;j<b;j++){
			if(c[h-1]>=e[j][0]&&c[h-1]<=e[j][1])
			s=max(s,dp[h][j]+ze(e[i][2]-e[j][2]));
		}
		dp[h+1][i]=s;
		}
	}
}
int x=0;
for(int y:dp[a]){
	if(x<y)x=y;
}
cout<<x<<endl;
}

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[201][201];
	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;
}