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<cstdio>
#include<cstdlib>
#include<algorithm>
#include<iostream>
#include<cmath>
#include<string>
#include<cstring>
#include<vector>
#include<queue>
#include<deque>
using namespace std;
int main(){
	int D,N;
	cin >> D >> N;
	int day[220];
	int clothes[212][212];
	memset(clothes,-1,sizeof(clothes));
	for(int i = 0; i < D; i++){
		scanf("%d",&day[i]);
	}
	for(int i = 0; i < N; i++){
		int a,b,c;
		scanf("%d%d%d",&a,&b,&c);
		for(int j = 0; j < D; j++){
			if(a <= day[j] && day[j] <= b)clothes[j+1][i+1] = c;
		}
	}
	int dp[212][212];
	memset(dp,-1,sizeof(dp));
	N++;D++;
	for(int j = 1; j < N; j++){
		if(clothes[1][j]!=-1)dp[1][j] = 0;
	}
	for(int i = 2; i < D; i++){
		for(int j = 1; j < N; j++){
			if(clothes[i][j]!=-1){
				for(int k = 1; k < N; k++){
					if(dp[i-1][k]!=-1){
						dp[i][j] = max(dp[i][j],dp[i-1][k]+abs(clothes[i-1][k]-clothes[i][j]));
					}
				}
			}
		}
	}
	int ans = -1000;
	for(int i = 0; i < N; i++){
		ans = max(ans,dp[D-1][i]);
	}
	printf("%d\n",ans);
	return 0;
}

C++: #include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <functional>
#include <numeric>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <utility>

using namespace std;

#define FOR(i,a,b) for(long long i=(a);i<(b);i++)
#define REP(i,N) for(long long i=0;i<(N);i++)
#define ALL(s) (s).begin(),(s).end()

#define PI acos(-1.0)
#define INF 1000000007

typedef long long ll; 
typedef pair<ll, ll> PL;

ll n, d, dp1, dp2, p, q;
ll t[1000], a[1000], b[1000], c[1000], MAX[1000], MIN[1000];

int main(){
	cin >> d >> n;
	REP(i, d){
		cin >> t[i];
	}
	REP(i, n){
		cin >> a[i] >> b[i] >> c[i];
	}
	REP(i, d){
		ll ma = -1, mi = 10000;
		REP(j, n){
			if (t[i] >= a[j] && t[i] <= b[j]){
				ma = max(c[j], ma);
				mi = min(c[j], mi);
			}
		}
		MAX[i] = ma;
		MIN[i] = mi;
	}
	REP(i, d){
		cout << MAX[i] << " " << MIN[i] << endl;
	}
	dp1 = 0;
	dp2 = 0;
	REP(i, d - 1){
		p = dp1; q = dp2;
		dp1 = max(p + abs(MAX[i + 1] - MAX[i]), q + abs(MIN[i + 1] - MAX[i]));
		dp2 = max(q + abs(MAX[i + 1] - MIN[i]), p + abs(MIN[i + 1] - MIN[i]));
	}
	cout << max(dp1,dp2) << endl;
}

C++: #include <bits/stdc++.h>
using namespace std;

int d, n, t[200], a[200], b[200], c[200];
int dp[210][210];

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

	for (int i = 1; i < d; i++) {
		for (int j = 0; j < n; j++) {
			if (t[i - 1] < a[j] || b[j] < t[i - 1]) continue;
			for (int k = 0; k < n; k++) {
				if (t[i] < a[k] || b[k] < t[i]) continue;
				dp[i][k] = max(dp[i][k], dp[i - 1][j] + abs(c[j] - c[k]));
			}
		}
	}

	int ans = 0;
	for (int i = 0; i < n; i++) ans = max(ans, dp[d - 1][i]);
	cout << ans << endl;
}

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

#define REP(i,n) for(int i=0;i<(int)(n);i++)

using namespace std;
int dp[210][101];

int main() {
  REP(i,210)REP(j,101)dp[i][j]=-1000000000;
  int d,n;
  cin>>d>>n;
  vector<int> t(d);
  REP(i,d)cin>>t[i];
  vector<tuple<int,int,int>> cl;
  REP(i,n){
    int a,b,c;
    cin>>a>>b>>c;
    cl.emplace_back(a,b,c);
  }
  REP(i,d){
    REP(j,n){
      int a,b,c;
      tie(a,b,c)=cl[j];
      if(t[i]<a||t[i]>b) continue;
      REP(k,101){
        if (i) dp[i+1][c] = max(dp[i+1][c], dp[i][k] + abs(c-k));
        else dp[i+1][c] = 0;
      }
    }
  }
  int mx = 0;
  REP(i,101)mx=max(mx,dp[d][i]);
  cout<<mx<<endl;
  return 0;
}

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

int wear[200][3], day[200];
int mem[200][200];
int D, N;
int dp(int d, int b)
{
  if(d == D)
    return 0;
  if(mem[d][b] != -1)
    return mem[d][b];
  int ans = 0;
  for(int i = 0; i < N; i++)
  {
    if(wear[i][0] <= day[d] && wear[i][1] >= day[d])
      ans = max(ans, dp(d + 1, i) + abs(wear[i][2] - wear[b][2]));
  }
  mem[d][b] = ans;
  return ans;
}

int main()
{
  for(int i = 0; i < 200; i++)
    for(int j = 0; j < 200; j++)
      mem[i][j] = -1;

  cin >> D >> N;
  for(int i = 0; i < D; i++)
    cin >> day[i];
  for(int i = 0; i < N; i++)
    cin >> wear[i][0] >> wear[i][1] >> wear[i][2];
  int ans = 0;
  for(int i = 0; i < N; i++)
  {
    if(wear[i][0] <= day[0] && wear[i][1] >= day[0])
      ans = max(ans, dp(1, i));
  }
  cout << ans << endl;
}

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

int N,D;
LL res=-INF;
LL Day[150];
LL mem[114][514][810];
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]!=-1)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;
    fill(mem[0][0],mem[114][0],-1);
    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<iostream>
#include<camth>

using namespace std;

int d,n;
int tenki[202];
int saiko[101],saitei[101];
int hade[101];
int dp[201][101];

int hot(int d,int f){
	if(dp[d][f]!=0)return dp[d][f];
	if(d==0){
		cout<<d<<"a "<<f<<" "<<endl;
		return dp[d][f]=0;
	}
	int ans=0;
	for(int i=1;i<=n;i++){
		if(saitei[i]<=tenki[d]&&tenki[d]<=saiko[i]){
			ans=max(ans,hot(d-1,i)+abs(hade[i]-hade[f]));
		}
		cout<<d<<"asi "<<f<<" "<<ans<<endl;
	}
	cout<<d<<"as "<<f<<" "<<ans<<endl;
	return dp[d][f]=ans;
}

int main(){
	cin>>d>>n;
	for(int i=1;i<=d;i++){
		cin>>tenki[i];
	}
	for(int i=1;i<=n;i++){
		cin>>saitei[i]>>saiko[i]>>hade[i];
	}
	int ans=0;
	for(int i=1;i<=n;i++){
		if(saitei[i]<=tenki[d-1]&&tenki[d-1]<=saiko[i]){
			ans=max(ans,hot(d-1,i));
		}
	}
	cout<<ans<<endl;
	return 0;
}