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 <iomanip>
#include <vector>
#include <algorithm>
using namespace std;

int main() {
  int d, n;
  cin >> d >> n;
  vector<int> t(d);
  vector<vector<int>> abc(n, vector<int>(3));
  int i, j, k;
  for (i = 0; i < d; i++) {
    cin >> t[i];
  }
  for (i = 0; i < n; i++) {
    for (j = 0; j < 3; j++) {
      cin >> abc[i][j];
    }
  }

  int dp[d][n];
  for (i = 0; i < d; i++) {
    for (j = 0; j < n; j++) {
      // cout << abc[j][i] << ' ' << abc[j][2] << ' ' << t[i] << endl;
      dp[i][j] = (abc[j][0] <= t[i] && t[i] <= abc[j][1] ? 0 : -1);
    }
    // cout << endl;
  }
  for (i = 1; i < d; i++) {
    for (j = 0; j < n; j++) {
      if (dp[i][j] != -1) {
        for (k = 0; k < n; k++) {
          if (dp[i - 1][k] != -1) {
            dp[i][j] = max(dp[i][j], dp[i - 1][k] + abs(abc[j][2] - abc[k][2]));
          }
        }
      }
    }
  }
  int result = 0;
  for (i = 0; i < n; i++) {
    result = max(result, dp[d - 1][i]);
  }
  cout << result << endl;

  // cout << endl;
  // for (i = 0; i < d; i++) {
  //   for (j = 0; j < n; j++) {
  //     cout << setw(3) << dp[i][j] << ' ';
  //   }
  //   cout << endl;
  // }
  return 0;
}

C++: #include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <map>
#include <sstream>
#include <functional>
#include <numeric>
#include <cmath>
#include <cstring>
#include <queue>

using namespace std;

const int INF = 1 << 29;
typedef pair<int, int> P;

int d,n;
typedef tuple<int, int, int> C;//温度~温度,派手

int dp[201][201] = {0};

int main(void)
{
	while(cin >> d >> n && n && d)
	{
		memset(dp, 0, sizeof(dp));
		vector<C> cs(n);
		vector<int> atm(d);
		for (int i = 0; i < d; i++)
		{
			cin >> atm[i];
		}
		for (int i = 0; i < n; i++)
		{
			int a, b, c;
			cin >> a >> b >> c;
			cs[i] = make_tuple(a, b, c);
		}

		int ans = 0;
		for (int i = 1; i < d; i++)
		{
			for (int j = 0; j < n; j++)
			{
				int a = get<0>(cs[j]), b = get<1>(cs[j]), c = get<2>(cs[j]);
				if(atm[i] < a || atm[i] > b) continue;
				for (int k = 0; k < n; k++)
				{
					int pa = get<0>(cs[k]), pb=get<1>(cs[k]), pc=get<2>(cs[k]);
					if(atm[i-1] < pa || atm[i-1] > pb) continue;

					 dp[i][j] = max(dp[i][j], abs(c - pc) + dp[i-1][k]);
					 ans = max(dp[i][j],ans);
				}
			}
		}
		cout << ans << endl;
	}
	return 0;
}

C++: #include<iostream>
#include<vector>
#include<algorithm>
#include<cmath>

using namespace std;

int main(void){

  int d,n,t,a,b,c,dp[202][202];
  vector<int>T,A,B,C;

  cin >> d >> n;
    T.clear();
    A.clear();
    B.clear();
    C.clear();

    for(int i=0;i<d;i++){
      cin >> t;
      T.push_back(t);
    }

    for(int i=0;i<n;i++){
      cin >> a >> b >> c;
      A.push_back(a);
      B.push_back(b);
      C.push_back(c);
    }

    for(int i=0;i<202;i++)
      for(int j=0;j<202;j++)
	dp[i][j]=-1;

    for(int i=0;i<n;i++){
      for(int j=0;j<n;j++){
       
	  if(A[i]<=T[0] && T[0]<=B[i] && A[j]<=T[1] && T[1]<=B[j])
	    dp[0][j]=max(dp[0][j],abs(C[i]-C[j]));
	}
    }

    for(int i=1;i<=d;i++){
      for(int j=0;j<n;j++){
	for(int k=0;k<n;k++){
	    if(A[k]<=T[i+1] && T[i+1]<=B[k] && dp[i-1][j]>=0)
	      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-2][i]);
    }
    cout << ans << endl;

  return 0;
}

C++: #include<stdio.h>
#include<stdlib.h>
int h[201][201];
int main(void)
{
    int n,m,i,j,x[200],a[200],b[200],c[200],l,s,w,f;
	scanf("%d %d",&n,&m);
	for(i=0;i<n;i++){
	    scanf("%d",&x[i]);
	}
	for(i=0;i<m;i++){
	    scanf("%d %d %d",&a[i],&b[i],&c[i]);
	}
	for(i=0;i<n;i++){
	    for(j=0;j<m;j++){
		    h[i][j]=-1;
		}
	}
	for(i=0;i<n;i++){
	    for(j=0;j<m;j++){
		    if(x[i]>=a[j]&&x[i]<=b[j]){
			    h[i][j]=c[j];
			}
		}
	}
	w=0;
	for(i=0;i<n-1;i++){
	    s=0;
		for(j=0;j<m;j++){
		    for(l=0;l<m;l++){
			    f=abs(h[i][j]-h[i+1][l]);
				if(h[i][j]>-1&&h[i+1][l]>-1&&s<f){
				    s=f;
				}
			}
		}
		w+=s;
	}
	printf("%d\n",w);
	return 0;
}

C++: #include <bits/stdc++.h>
#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define RFOR(i,a,b) for(int i=(b) - 1;i>=(a);i--)
#define REP(i,n) for(int i=0;i<(n);i++)
#define RREP(i,n) for(int i=n-1;i>=0;i--)

#define PB push_back
#define INF INT_MAX/3
#define ALL(a) (a).begin(),(a).end()
#define RALL(a) (a).rbegin(),(a).rend()
#define CLR(a) memset(a,0,sizeof(a))

typedef long long int ll;

using namespace std;

int dp[201][201] = {0};

int main(){
    int d,n;
    memset(dp,0,sizeof(dp));
    cin >> d >> n;
    vector<int> t(d);
    vector<int> a(n);
    vector<int> b(n);
    vector<int> c(n);
    REP(i,d){
	cin >> t[i];
    }
    REP(i,n){
	cin >> a[i] >> b[i] >> c[i];
    }

    FOR(i,2,d+2){
	REP(j,n){
	    REP(k,n){
		int tt = t[i-1];
		int pt = t[i-2];
		if(a[k] <= tt && tt <= b[k] && a[j] <= pt && pt <= b[j]){
		    dp[i][k] = max(dp[i][k],dp[i-1][j] + abs(c[j]-c[k]) );
		}
	    }
	}
    }
    int ans = 0;
    REP(i,n) {
	ans = max(ans,dp[d][i]);
    }
    cout << ans << endl;

    return 0;
}

C++: #include<stdio.h>
#include<stdlib.h>
#include<algorithm>
using namespace std;
int s[200][200],r[200][200];
int main(void)
{
	int n,i,j,m,t[200],a[200],b[200],c[200],k[200],h,l,z,x;
	scanf("%d %d",&n,&m);
	for(i=0;i<n;i++) scanf("%d",&t[i]);
	for(i=0;i<m;i++) scanf("%d %d %d",&a[i],&b[i],&c[i]);
	for(i=0;i<n;i++){
		k[i]=0;
		for(j=0;j<m;j++){
			if(a[j]<=t[i]&&b[j]>=t[i]){
				r[i][k[i]]=c[j];
				k[i]++;
			}
		}
	}
	for(i=0;i<n;i++){
		for(j=0;j<m;j++){
			s[i][j]=0;
		}
	}
	for(i=1;i<n;i++){
		for(j=0;j<k[i];j++){
			for(l=0;l<k[i-1];l++){
				if(s[i][j]<abs(r[i][j]-r[i-1][l])+s[i-1][l]){
					s[i][j]=abs(r[i][j]-r[i-1][l])+s[i-1][l];
				}
			}
		}
	}
	z=0;
	for(i=0;i<k[n-1];i++){
		if(z<s[n-1][i]) z=s[n-1][i];
	}
	printf("%d\n",z);
	return 0;
}