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];
int ans=0;
clothes T;
LL cal(int day,int pp){
   LL res=0;
   if(day==N+1){
        return 0;
   }
   else if(mem[day][pp]!=-1){
        //cout<<"hoge"<<endl;
        return mem[day][pp];
   }
   else{
        FOR(i,1,D){
            if(Day[day]>=T.A[i]&&Day[day]<=T.B[i]){
                if(day==1){
                    res=cal(day+1,T.C[i]);
                    mem[day][pp]=res;
                }
                else {
                    res=max(cal(day+1,T.C[i])+abs(pp-T.C[i]),res);
                    mem[day][pp]=res;
                }
            }
        }
   }
   return mem[day][pp]=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<cstdio>
#include<algorithm>
using namespace std;
int dp[200][200],a[200],b[200],c[200],mx,t[200];
int main(void)
{
	int d,n,i,j,k,x;
	scanf("%d %d",&d,&n);
	for(i=0;i<d;i++){
		scanf("%d",&t[i]);
	}
	for(i=0;i<d;i++){
		for(j=0;j<n;j++){
			dp[i][j]=-1;
		}
	}
	for(i=0;i<n;i++){
		scanf("%d %d %d",&a[i],&b[i],&c[i]);
		if(a[i]<=t[0]&&t[0]<=b[i]){
			dp[0][i]=0;
		}
	}
	for(i=0;i<d-1;i++){
		for(j=0;j<n;j++){
			if(dp[i][j]!=-1){
				for(k=0;k<n;k++){
					if(a[k]<=t[i+1]&&t[i+1]<=b[k]){
						x=c[j]-c[k];
						if(x<0)	x=-x;
						dp[i+1][k]=max(dp[i+1][k],dp[i][j]+x);
					}
				}
			}
		}
	}
	mx=0;
	for(i=0;i<n;i++){
		mx=max(mx,dp[d-1][i]);
	}
	printf("%d\n",mx);
	return 0;
}

C++: #include<iostream>
#include<string>
#include<cmath>
#include<queue>
#include<map>
#include<set>
#include<list>
#include<iomanip>
#include<vector>
#include<functional>
#include<algorithm>
#include<cstdio>
using namespace std;
struct P {
	int min, max, hade;
};
P b[200];
int d, n,t[200];
vector<int>a[200];
int dp[200][200];
int solve(int i, int mae) {
	if (i == d) return 0;
	int sum = 0;
	for (int j = 0; j < a[i].size(); j++) {
		int res = mae - b[a[i][j]].hade;
		res = max(res, 0 - res);
		if (dp[i + 1][a[i][j]] != -1) {
			sum = max(sum, res + dp[i + 1][a[i][j]]);
		}
		else {
			dp[i + 1][a[i][j]] = solve(i + 1, b[a[i][j]].hade);
			sum = max(sum, dp[i + 1][a[i][j]] + res);
		}
	}
	return sum;
}
int main() {
	cin >> d >> n;
	for (int i = 0; i < d; i++) {
		cin >> t[i];
	}
	for (int i = 0; i < n; i++) {
		cin >> b[i].min >> b[i].max >> b[i].hade;
	}
	for (int i = 0; i < d; i++) {
		for (int j = 0; j < n; j++) {
			if (b[j].min <= t[i] && b[j].max >= t[i]) {
				a[i].push_back(j);
			}
		}
	}
	for (int i = 0; i <= d; i++) {
		for (int j = 0; j < n; j++) {
			dp[i][j] = -1;
		}
	}
	int sum = 0;
	for (int i = 0; i < a[0].size(); i++) {
		if (dp[0][a[0][i]] != -1) {
			sum = max(sum, a[0][i] - dp[1][a[0][i]]);
		}
		else {
			sum = max(sum, dp[1][a[0][i]] = solve(1, b[a[0][i]].hade));
		}
	}
	cout << sum << endl;
}

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

int main()
{
	int d, n, m = 0;
	int t[200];

	cin >> d >> n;

	for (int i = 0; i < d; i++)
		cin >> t[i];

	vector<vector<int> > cx(d);

	for (int i = 0; i < n; i++) {
		int a, b, c;
		cin >> a >> b >> c;

		for (int j = 0; j < d; j++) {
			if (t[j] >= a && t[j] <= b)
				cx[j].push_back(c);
		}
	}

	for (int i = 0; i < d; i++)
		sort(cx[i].begin(), cx[i].end());

	int mx = abs(cx[0].back() - cx[1].front()),
		mn = abs(cx[0].front() - cx[1].back());

	for (int i = 2; i < d; i++) {
		int a = mx, b = mn;
		mx = max(a + abs(cx[i - 1].front() - cx[i].front()), b + abs(cx[i - 1].back() - cx[i].front()));
		mn = max(a + abs(cx[i - 1].front() - cx[i].back()), b + abs(cx[i - 1].back() - cx[i].back()));
	}

	cout << max(mx, mn) << endl;

	return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
 
int main() {
  int D,N;
  cin >> D >> N;
  
  vector<int> T(D);
  for (int i = 0; i < D; i++) cin >> T.at(i);
  
  vector<vector<int>> ABC(N, vector<int>(3));
  for (int i = 0; i < N; i++) cin >> ABC.at(i).at(0)
                                   >> ABC.at(i).at(1)
                                   >> ABC.at(i).at(2);
  
  vector<vector<vector<int>>> dp( D , 
          vector<vector<int>>   ( N ,
                  vector<int>   ( N, -1)));
  
  for (int i = 0; i < N; i++) {
    if (ABC.at(i).at(0) <= T.at(0) && T.at(0) <= ABC.at(i).at(1)) {
      for (int j = 0; j < N; j++) {
        dp.at(0).at(i).at(j) = 0;
      }
    }
  }

  for (int i = 1; i < D; i++) {
    for (int j = 0; j < N; j++) {
      
      if (ABC.at(j).at(0) <= T.at(i) && T.at(i) <= ABC.at(j).at(1)) {

        for (int k = 0; k < N; k++) {
          dp.at(i).at(j).at(k) = 0;
        
          for (int l = 0; l < N; l++) {
            if (dp.at(i-1).at(k).at(l) == -1) continue;
            dp.at(i).at(j).at(k) = max(dp.at(i).at(j).at(k),
                                       dp.at(i-1).at(k).at(l) + abs(ABC.at(k).at(2) - ABC.at(j).at(2)));
          }
        }
        
      }
      
    }
  }
  
  int ans = 0;
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < N; j++) {
      ans = max(ans, dp.at(D-1).at(i).at(j));
    }
  }
  
  cout << ans << endl;

  return 0;
}

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

constexpr int INF = 1e9;

int main() {
    int D, N; cin >> D >> N;
    vector<int> T(D), A(N), B(N), C(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];
    }

    vector<vector<int>> dp(D + 1, vector<int>(N, -INF));
    for(int i = 0; i < D; ++i) {
        for(int j = 0; j < N; ++j) {
            if(A[j] <= T[i] && T[i] <= B[j]) {
                if(i == 0) {
                    dp[i + 1][j] = 0;
                } else {
                    for(int k = 0; k < N; ++k) {
                        dp[i + 1][j] = max(dp[i + 1][j], dp[i][k] + abs(C[j] - C[k]));
                    }
                }
            }
        }
    }

    cout << *max_element(begin(dp[D]), end(dp[D])) << endl;
}

C++: #include<stdio.h>
int main(void)
{
	int a,b,c=0,d=0,i,j,k;
	scanf("%d %d",&a,&b);
	int x[a],a1[200],b1[200],c1[200],y[200][200];
	for(i=0;i<a;i++) scanf("%d",&x[i]);
	for(i=0;i<a;i++){
		for(j=0;j<b;j++){
			y[i][j]=-1;
		}
	}
	for(i=0;i<b;i++){
		scanf("%d %d %d",&a1[i],&b1[i],&c1[i]);
		if(a1[i]<=x[0]&&x[0]<=b1[i]){
			y[0][i]=0;
		}
	}
	for(i=0;i<a;i++){
		for(j=0;j<b;j++){
			if(y[i][j]!=-1){
				for(k=0;k<b;k++){
					if(a1[k]<=x[i+1]&&x[i+1]<=b1[k]){
						c=c1[j]-c1[k];
						if(c<0) c=-c;
						if(y[i+1][k]>=y[i][j]+c){
							y[i+1][k]=y[i+1][k];
						}
						else y[i+1][k]=y[i][j]+c;
					}
				}
			}
		}
	}
	for(i=0;i<b;i++){
		if(d<=y[a-1][i]){
			d=y[a-1][i];
		}
	}
	printf("%d\n",d);
	return 0;
}