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 <fstream>
#include <math.h>

using namespace std;
int T[200], A[200], B[200], C[200], D, N;
int Cs[200][200], CsNumber[200], valueCs[200][200];

inline void readinput(){
//	ifstream fin("input_2012_4.dat");
	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];
}

inline void output_2d(int X[200][200]){
	for(int i=0; i<D; i++){
		for(int j=0; j<N; j++){
			cout << X[i][j];
			if(j != N-1) cout << " ";
			else cout << endl;
		}
	}
}

inline void putCs(){
	int idx;
	for(int i=0; i<D; i++){
		idx = 0;
		for(int j=0; j<N; j++){
			if(A[j]<=T[i] && T[i]<=B[j]){
				Cs[i][idx] = C[j];
				idx += 1;
			}
		CsNumber[i] = idx;
		}
	}
}

inline int solver(){
	int max, maxidx, buf;
	
	//initialize
	for(int i=0; i<200; i++){
		for(int j=0; j<200; j++) valueCs[i][j] = 0;
	}
	
	for(int i=D-1; i>0; i--){	// D-1 to 1
		for(int j1=0; j1<CsNumber[i-1]; j1++){
			max = 0;
			for(int j2=0; j2<CsNumber[i]; j2++){
				buf = fabs(Cs[i-1][j1] - Cs[i][j2]) + valueCs[i][j2];
				if(max < buf){
					max = buf;
					maxidx = j2;
				}
			}
			valueCs[i-1][j1] += max;
		}
	}
	max = 0;
	for (int j=0; j<CsNumber[0]; j++){
		buf = valueCs[0][j];
		if(max < buf) max = buf;
	}
	return max;
}

int main(){
	readinput();
	putCs();
	cout << "answer: " << solver();
//	cout << "--- Cs ---" << endl;
//	output_2d(Cs);
//	cout << "--- valueCs ---" << endl;
//	output_2d(valueCs);
	return 0;
}

C++: #include<iostream>
#include<algorithm>
#include<functional>
#include<iomanip>
#include<stdio.h>
#include<stdlib.h>
using namespace std;
int main(){
	int D,N;
	cin>>D>>N;
	int day[220];
	for(int i=0;i<D;i++){
		cin>>day[i];
	}
	int jimi[220]={},hade[220]={};
	for(int i=0;i<N;i++){
		int a,b,c;
		cin>>a>>b>>c;
		for(int j=a;j<=b;j++){
			if(jimi[j]==0){
				jimi[j]=c;
			}
			jimi[j]=min(jimi[j],c);
			hade[j]=max(hade[j],c);
		}
	}
	int ans1=0,ans2=0;
	for(int i=0;i<D-1;i++){
		if(i%2==0){
			ans1+=abs(jimi[day[i]]-hade[day[i+1]]);
			ans2+=abs(hade[day[i]]-jimi[day[i+1]]);
		}
		else{
			ans1+=abs(hade[day[i]]-jimi[day[i+1]]);
			ans2+=abs(jimi[day[i]]-hade[day[i+1]]);
		}
	}
	int ans=max(ans1,ans2);
	cout<<ans<<endl;
	return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef pair<ll,ll> mp;
#define inf 1e9


int main(){
	int d,n;
	cin>>d>>n;
	vector<int> tmp(d);
	for(int i=0;i<d;i++)cin>>tmp[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(a<=tmp[j] && tmp[j]<=b) cx[j].push_back(c);
		}
	}
	for(int i=0;i<d;i++) sort(cx[i].begin(),cx[i].end());
	int l=cx[0][0],r=cx[0][cx[0].size()-1];
	int ls=0,rs=0;
	for(int i=1;i<d;i++){
		if(i%2==0){
			
			rs += abs(cx[i][cx[i].size()-1] -cx[i-1][0]);
			ls += abs(cx[i][0] - cx[i-1][cx[i-1].size()-1]);
		}else{
			ls += abs(cx[i][cx[i].size()-1] -cx[i-1][0]);
			rs += abs(cx[i][0] - cx[i-1][cx[i-1].size()-1]);

		}
	}
	cout<<max(ls,rs)<<endl;


	return 0;
}

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

typedef struct{
	int a; //下限
	int b; //上限
	int c; //派手さ
}clothes;

int d, n;
int t[200];
clothes p[200];

long dp[202][201];

long rec(int x, int y){ //x日目,前の日にきてた服y
	if(dp[x][y]>=0) return dp[x][y];
	
	long ret=0;
	if(x==0){
		for(int i=0; i<n; ++i){
			if(p[i].a<=t[x] && t[x]<=p[i].b){
				int tmp=rec(x+1, i);	
				if(ret<tmp) ret=tmp;
			}
		}	
	}
	else if(x==d) return 0;	
	else{
		int mm=0;
		for(int i=0; i<n; ++i){
			if(p[i].a<=t[x] && t[x]<=p[i].b){
				int cc=p[i].c-p[y].c;
				if(cc<0) cc=-cc;
				
				int tmp=rec(x+1, i)+cc;	
				if(mm<tmp) mm=tmp;
			}
		}
		ret+=mm;	
	}
	
	return dp[x][y]=ret;
}

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", &p[i].a, &p[i].b, &p[i].c);
	
	memset(dp, -1, sizeof(dp));
	printf("%ld\n", rec(0,0));	
}

C++: #include<bits/stdc++.h>
using namespace std;
int n,m,a[201],b[3][201];
int mem[200][1000][1000];
int hot(int day,double sum,int bef);

int main(){

  cin >> n >> m;

  memset(mem,-1,sizeof(mem));

  for(int i=0;i<n;i++) cin >> a[i];
  for(int i=0;i<m;i++) cin >> b[0][i] >> b[1][i] >> b[2][i];

  cout << hot(0,0,0) << endl;
  
  return(0);
}

int hot(int day,double sum,int bef){
  
  int ret = 0;
  
  if(day == n) return ret = max(ret,(int)sum);
  if(mem[day][(int)sum][bef] != -1) return mem[day][(int)sum][bef];
  if(day < n){
    
    for(int j=0;j<m;j++){
      if(a[day]>=b[0][j]&&a[day]<=b[1][j]){
	if(day == 0) ret = max(hot(day+1,sum,b[2][j]),ret);
	else ret = max(hot(day+1,sum+fabs( (double)bef-b[2][j]),b[2][j]),ret);
      }
    }
    
  }
  
  return mem[day][(int)sum][bef] = ret;

}

C++: #include <iostream>
#include <fstream>
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <iomanip>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <sstream>
#include <string>
#include <vector>
using namespace std;

#ifdef __GNUC__
#define foreach(it,X) for(__typeof((X).begin()) it=(X).begin();it!=(X).end();it++)
#endif

#ifdef MSC_VER
#pragma comment(linker, "/STACK:400000000")
#define foreach(it,X) for(it=X.begin();it!=X.end();it++)
#endif

#define EPS 1e-9
#define INF 1070000000LL
#define MOD 1000000007LL
#define fir first
#define ite iterator
#define mp make_pair
#define rep(i,n) rep2(i,0,n)
#define rep2(i,m,n) for(int i=m;i<(n);i++)
#define pb push_back
#define sec second
#define sz(x) ((int)x.size())
#define vc vector

typedef istringstream iss;
typedef long long ll;
typedef pair<int,int> pi;
typedef stringstream sst;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<string> vs;

int D,N,T[222],A[222],B[222],C[222];
int dp[222][111];

int f(int cur,int pre){
	int& res=dp[cur][pre];
	if(res!=-1)return res;
	if(cur==D)return res=0;
	res=0;
	rep(i,N)if(A[i]<=T[cur] && T[cur]<=B[i]){
		res=max(res,cur==0 ? f(cur+1,C[i]) : abs(pre-C[i])+f(cur+1,C[i]));
	}
	return res;
}

int main(){
	ios_base::sync_with_stdio(0);
	cin>>D>>N;
	rep(i,D)cin>>T[i];
	rep(i,N)cin>>A[i]>>B[i]>>C[i];
	memset(dp,-1,sizeof(dp));
	cout<<f(0,0)<<endl;
}

C++: #include<iostream>
#include<algorithm>
using namespace std;
int dp[110][110],l[110],r[110],p[110],n,m,a[110];
int main(){
	cin>>n>>m;for(int i=0;i<n;i++)cin>>a[i];
	for(int i=0;i<m;i++)cin>>l[i]>>r[i]>>p[i];
	for(int i=0;i<12100;i++)dp[i/110][i%110]=-1000000000;
	for(int i=0;i<m;i++){
		if(l[i]<=a[0] && a[i]<=r[0]){
			dp[0][p[i]]=max(dp[0][p[i]],0);
		}
	}
	for(int i=1;i<n;i++){
		for(int j=0;j<=100;j++){
			for(int k=0;k<m;k++){
				if(l[k]<=a[i] && a[i]<=r[k]){
					dp[i][p[k]]=max(dp[i][p[k]],dp[i-1][j]+abs(j-p[k]));
				}
			}
		}
	}
	int maxn=0;for(int i=0;i<=100;i++)maxn=max(maxn,dp[n-1][i]);
	cout<<maxn<<endl;
	return 0;
}