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<bits/stdc++.h>
#define FOR(i,a,b) for(int i=a;i<=b;++i)
#define LL long long
static const LL INF = 1<<24;
static const int MAX_N = 300;
using namespace std;

struct clothes{
    LL A[MAX_N],B[MAX_N],C[MAX_N];
};

LL mem[MAX_N][MAX_N];
int N,D;
int Day[MAX_N];
clothes T;

LL cal(int day,int pp){
   LL res=-INF;
   // cout<<day-1<<" "<<res<<endl;
   if(day==N+1){
        return 0;
   }
   else if(mem[day][pp]!=-1){
        return res = mem[day][pp];
   }
   else{
        FOR(i,1,D){
            if(Day[day]>=T.A[i]&&Day[day]<=T.B[i]){
                if(day==1)mem[day][pp]=res=cal(day+1,T.C[i]);
                else {
                    mem[day][pp]=res=max(cal(day+1,T.C[i])+abs(pp-T.C[i]),res);
                }
            }
        }
   }
   return res;
}

int main(){
    //cout<<-INF<<endl;
    cin>>N>>D;
    fill(mem[0],mem[MAX_N],-1);
    FOR(i,1,N){
        cin>>Day[i];
    }
    FOR(i,1,D){
        cin>>T.A[i]>>T.B[i]>>T.C[i];
    }
    cout<<cal(1,0)<<endl;
}

C++: #include <bits/stdc++.h>
using namespace std;
#define MAX_D (200)
#define MAX_N (200)

int d, n;
int t[MAX_D];
int a[MAX_N], b[MAX_N], c[MAX_N];

int dp[MAX_D + 1][MAX_N];
// dp[ ?????\????????§??????????????? ][ ?????????????????? ] = ????????§????´??????????????¨?

int search(int i, int last)
{
	int &res = dp[i][last];
	
	if (res != -1) return (res);
	
	if (i == d) return (res = 0);
	
	for (int j = 0; j < n; j++){
		if (t[i] < a[j] || b[j] < t[i]) continue;
		int sub = abs(c[last] - c[j]);
		res = max(res, sub + search(i + 1, j));
	}
	
	return res;
}

int main()
{
	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]);
	}
	
	memset(dp, -1, sizeof(dp));
	
	int ans = 0;
	for (int i = 0; i < n; i++){
		if (t[0] < a[i] || b[i] < t[0]) continue;
		ans = max(ans, search(1, i));
	}
	
	printf("%d\n", ans);
	
	return 0;
}

C++: #include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <cmath>
using namespace std;

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

struct Dress{
  int A, B, C;
};

const int MAX = 210;

bool canWear(int temp, Dress d){
  return (d.A <= temp && temp <= d.B);
}

int main(){
  int D, N;
  while(cin >> D >> N){
    vector<Dress> dress(D);    
    vector<int> temp(N);
    rep(i, D) cin >> temp[i];
    rep(i, N) cin >> dress[i].A >> dress[i].B >> dress[i].C;
    
    int dp[MAX][MAX];
    memset(dp, -1, sizeof(dp));
    rep(i, N) if(canWear(temp[0], dress[i])) dp[0][i] = 0;
    
    REP(i, 1, D){
      REP(j, 0, N){
	REP(k, 0, N){
	  if(dp[i-1][j] == -1) continue;
	  
	  if(canWear(temp[i], dress[k])) dp[i][k] = max(dp[i][k], dp[i-1][j] + abs(dress[j].C - dress[k].C));
	}
      }
    }
    
    int ans = -1;
    rep(i, N) ans = max(ans, dp[D-1][i]);
    cout << ans << endl;
  }
  return 0;
}

C++: #include<iostream>
using namespace std;
const int INF=(1<<21);
int main(){

  typedef struct{
    int a;
    int b;
    int c;
  }wear;
  wear st[200];

  int d,n;
  int t[200];
  cin>> d>> n;
  for(int i=0;i<d;i++){
    cin>> t[i];
  }

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

  int dp[d][n];

  for(int i=0;i<d;i++){
    for(int j=0;j<n;j++){
      if(i==0 && st[j].a<=t[0] && st[j].b>=t[0]) dp[i][j]=0;
      else dp[i][j]=-INF;
    }
  }

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

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

  cout<< m<< endl;
}

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

 int master(int data){
 if(data>=0)
	 return data;
 return -data;
 }
int main(){
    int D,N,T[200],A[200][3];
    int dp[200][101];
    cin >> N >> D ;
    for(int i=0;i<N;i++){
        cin >>T[i];
    }
    for(int i=0;i<D;i++){
        for(int j=0;j<3;j++){
            cin >>A[i][j];
        }
    }
    for(int i=0;i<N;i++){
        for(int j=0;j<D;j++){
            dp[i][j]=-9999;
        }
    }
    for(int l=0;l<D;l++){
        if(T[0]>A[l][0] && T[0]<A[l][1] ){
            dp[ 0 ][ A[l][2] ]=0;
             
 
        }
    }
    for(int i=1;i<N;i++){
        for(int j=0;j<D;j++){
            if(T[i]>=A[j][0] && T[i]<=A[j][1] ){
                for(int l=0;l<=100;l++){
                    if(dp[i-1 ][ l ]>=0)
                        dp[ i ][ A[j][2] ]=max(dp[ i ][ A[j][2] ],dp[ i-1 ][ l ]+master(l-A[j][2]));//テッツセツεッツスツ、テッツセツづッツスツサテッツセツづァツ板佚ッツスツ」テッツセツ?テッツスツセテッツセツεッツスツ」テッツセツ?テッツスツァテッツセツεッツスツ」テッツセツ?テッツスツョテッツセツεッツスツヲテッツセツづヲツ??テッツスツゥテッツセツづッツスツォテッツセツづ、ツスツ堙ッツスツ」テッツセツ??テッツスツ、テッツセツづッツスツサテッツセツづァツ板佚ッツスツ・テッツセツづ・ツエツ「?
                     
 
                }
            }
        }
    }
    int answer=0;
    for(int j=0;j<101;j++)
    {
        answer=max(answer,dp[ N-1 ][j]);
    }
    cout << answer << endl;
    return 0;
}

C++: //AOJ 0579: Hot days
#include<iostream>
#include<algorithm>
#include<queue>
using namespace std;
typedef pair<int, int> P;
const int INF = 10000000;
int n, d, ans, c[201][61], save[3], dt[201], plan[200][2], s[200][101];
void solve(){
	for (int i = 1; i < d; i++){
		if (plan[i][0] == plan[i][1]){
			s[i][plan[i][0]] = max(s[i - 1][plan[i - 1][0]] + abs(plan[i][0] - plan[i - 1][0]), s[i - 1][plan[i - 1][1]] + abs(plan[i][0] - plan[i - 1][1]));
		}

		else{
			s[i][plan[i][0]] = max(s[i - 1][plan[i - 1][0]] + abs(plan[i][0] - plan[i - 1][0]), s[i - 1][plan[i - 1][1]] + abs(plan[i][0] - plan[i - 1][1]));
			s[i][plan[i][1]] = max(s[i - 1][plan[i - 1][0]] + abs(plan[i][1] - plan[i - 1][0]), s[i - 1][plan[i - 1][1]] + abs(plan[i][1] - plan[i - 1][1]));
		}
	}
	ans = max(s[d - 1][plan[d - 1][0]], s[d - 1][plan[d - 1][1]]);
}
int main(){
	cin >> d >> n;
	for (int i = 0; i < d; i++){
		cin >> dt[i];
	}
	for (int i = 0; i < n; i++){
		for (int j = 0; j<61; j++){
			c[i][j] = -1;
		}
	}
	for (int i = 0; i < n; i++){
		cin >> save[0] >> save[1] >> save[2];
		for (int j = save[0]; j <= save[1]; j++){
			c[i][j] = save[2];
		}
	}
	for (int i = 0; i<d; i++){
		plan[i][0] = INF;
	}
	for (int i = 0; i < d; i++){
		for (int j = 0; j < n; j++){
			if (c[j][dt[i]] != -1){
				plan[i][0] = min(c[j][dt[i]], plan[i][0]);
				plan[i][1] = max(c[j][dt[i]], plan[i][1]);
			}
		}
	}
	solve();
	cout << ans << endl;
	return 0;
}