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;
int D, N, T[200], A[200], B[200], C[200];
int dp[300][300];

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];

  fill_n(*dp, 300 * 300, -1);
  for(int i = 0; i < N; i++) {
    if(B[i] >= T[0] && T[0] >= A[i]) dp[0][i] = 0;
  }

  for(int i = 1; i < D; i++) {
    for(int j = 0; j < N; j++) {
      if(~dp[i - 1][j]) {
        for(int k = 0; k < N; k++) {
          if(B[k] >= T[i] && T[i] >= A[k]) {
            dp[i][k] = max(dp[i][k], dp[i - 1][j] + max(C[j] - C[k], C[k] - C[j])); 
          }
        }
      }
    }
  }
  cout << *max_element(dp[0], dp[300]) << endl;
}

C++: 
#include<iostream>

using namespace std;

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

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].mix >> 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[])){      //この服が今日適しているか？
                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_abso=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 <iostream>
#include <vector>
#include <algorithm>

using namespace std;

struct Cloth {
    int lb;
    int ub;
    int hade;
    Cloth(int ub, int lb, int hade) :
        ub(ub), lb(lb), hade(hade) {}
};

int main(){
    int d,n;
    cin >> d >> n;
    vector<int> temp(d);
    vector<Cloth> cloth(n, Cloth(0,0,0));
    for(int i=0;i<d;++i) cin >> temp[i];
    for(int i=0;i<n;++i) {
        int a, b, c;
        cin >> a >> b >> c;
        cloth[i].ub = b;
        cloth[i].lb = a;
        cloth[i].hade = c;
    }

    int p1 = 0, p2 = 0;
    int b1 = 0, b2 = 0;
    for(int i=0;i<d;++i) {
        int M = -1000000, m = 1000000;
        int t = temp[i];
        for(int j = 0; j<n; ++j) {
            if( cloth[j].lb <= t && t <= cloth[j].ub ) {
                int h = cloth[j].hade;
                M = max(h,M); m = min(h,m);
            }
        }

        if(i == 0) {
            b1 = M;
            b2 = m;
        } else {
            if( i % 2 == 1) {
                p1 += abs(m - b1);
                p2 += abs(M - b2);
                b1 = m; b2 = M;
            } else {
                p1 += abs(M - b1);
                p2 += abs(m - b2);
                b1 = M; b2 = m;
            }
        }
    }
    cout << max(p1,p2) << 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[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;
int i;

LL cal(LL day,LL p,LL pp,LL sum){
    //cout<<sum+abs(pp-p)<<" "<<res<<endl;
     if(res<sum+abs(pp-p)&&day==N+1){
                mem[day][p][pp]=res=sum+abs(pp-p);
                return 0;
    }
    if(mem[day][p][pp]!=0){
        res = mem[day][p][pp];
        //cout<<"1"<<endl;
    }
    else{
        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,0);
        }
        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,0);
            }
        }
        //cout<<"2"<<endl;
    }
    return 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 a,b,c,d,n,memo[500][500],a1,a2,m1,m2;
vector <pair<int,int> > updown;
vector <int> day;
vector <pair<int,int> > huku;
int solve(int dp,int one,int sum);


int main(){

    cin >> d >> n;
    for(int i=0;i<d;i++){
        cin >> a;
        day.push_back(a);
    }
    for(int i=0;i<n;i++){
        cin >> a >> b >> c;
        updown.push_back(make_pair(a,b));
        huku.push_back(make_pair(c,i));
    }

    std::sort(huku.begin(),huku.end());

    for(int i=0;i<n;i++){
            if(updown[huku[i].second].second>=day[0]&&day[0]>=updown[huku[i].second].first){
                m1=solve(1,huku[i].first,0);
                break;
            }
        }
    for(int i=n-1;i>-1;i--){
            if(updown[huku[i].second].second>=day[0]&&day[0]>=updown[huku[i].second].first){
                m2=solve(1,huku[i].first,0);
                break;
            }
        }


    cout << max(m1,m2) << endl;

return 0;
}


int solve (int dp,int one,int sum){

    //cout << "#######" << endl;

    int s1=0,s2=0;
    if(dp==d)return sum;
    else {
        if(memo[dp][one]!=0&&memo[dp][one]==sum)return memo[dp][one];

        for(int i=0;i<n;i++){
            if(updown[huku[i].second].second>=day[dp]&&day[dp]>=updown[huku[i].second].first){
                int sa=max(one,huku[i].first)-min(one,huku[i].first);
                s1=solve(dp+1,huku[i].first,sum+sa);
                break;
            }
        }
        for(int i=n-1;i>-1;i--){
            if(updown[huku[i].second].second>=day[dp]&&day[dp]>=updown[huku[i].second].first){
                int sa=max(one,huku[i].first)-min(one,huku[i].first);
                s2=solve(dp+1,huku[i].first,sum+sa);
                break;
            }
        }
        return memo[dp][one]=max(s1,s2);
    }

}