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 <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 = 220;

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;
    
    for(int i = 1 ; i < D ; i++){
      for(int j = 0 ; j < N ; j++){
	for(int k = 0 ; k < N ; k++){
	  if(dp[i-1][j] == -1) continue;
	  if(!canWear(temp[i], dress[k])) continue;
	  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<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());
	/*for(int i=0;i<d;i++){
		cout<<"day "<<i<<endl;
		for(int j=0;j<cx[i].size();j++){
			cout<<cx[i][j]<<' ';
		}
		cout<<endl;
	}*/
	int l=cx[0][0],r=cx[0][cx[0].size()-1];
	int ls=0,rs=0;
	vector<vector<int> > dp(d,vector<int>(2,0) );
	for(int i=1;i<d;i++){
		dp[i][0] = max(dp[i-1][0]+abs(cx[i][0]-cx[i-1][0]),dp[i-1][1]+abs(cx[i][0]-cx[i-1][cx[i-1].size()-1]) ); 
		dp[i][1] = max(dp[i-1][0]+abs(cx[i][cx[i].size()-1]-cx[i-1][0]),dp[i-1][1]+abs(cx[i][cx[i].size()-1]-cx[i-1][cx[i-1].size()-1]) ); 
	}
	//cout<<ls<<' '<<rs<<endl;
	cout<<max(dp[d-1][0],dp[d-1][1])<<endl;


	return 0;
}

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

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

const int DMAX = 300, NMAX = 300;
const int INF = 1e4;
int d, n;
int T[DMAX], A[NMAX], B[NMAX], C[NMAX];
int black[NMAX], red[NMAX];

int diff(int x, int y) {
  return (x > y ? x - y:y - x);
}

int solve() {
  for (int day = 0; day < d; day++) {
    black[day] = INF, red[day] = -1;
  }

  for (int day = 0; day < d; day++) {
    for (int cloth = 0; cloth < n; cloth++) {
      if (A[cloth] <= T[day] and T[day] <= B[cloth]) {
        black[day] = min(black[day], C[cloth]);
        red[day] = max(red[day], C[cloth]);
      }
    }
  }

  int oans = 0, yesterday = red[0];
  for (int day = 1; day < d; day++) {
    if (diff(red[day], yesterday) > diff(black[day], yesterday)) {
      oans += diff(red[day], yesterday);
      yesterday = red[day];
    } else {
      oans += diff(black[day], yesterday);
      yesterday = black[day];
    }
  }
  
  int eans = 0; yesterday = black[0];
  for (int day = 1; day < d; day++) {
    if (diff(red[day], yesterday) > diff(black[day], yesterday)) {
      eans += diff(red[day], yesterday);
      yesterday = red[day];
    } else {
      eans += diff(black[day], yesterday);
      yesterday = black[day];
    }
  }

  return max(oans, eans);
}

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

  cout << solve() << endl;
}

C++: #include <bits/stdc++.h>
typedef long long LL;
#define SORT(c) sort((c).begin(),(c).end())

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

using namespace std;
int main(void)
{
  int d,n;
  cin >> d >> n;
  vector<int> hot;
  hot.resize(d);
  REP(i,d) cin >> hot[i];
  vector<pair<pair<int,int>,int> > cloth;
  // cold,hot , color
  cloth.resize(n);
  REP(i,n) cin >> cloth[i].first.first >> cloth[i].first.second >> cloth[i].second;
  vector<vector<int> > dp;
  // dp[day][lastwear] = max score
  dp.resize(d+1);
  REP(i,d) dp[i].assign(n,-1048576);
  REP(j,n) if(cloth[j].first.first <= hot[0] && hot[0] <= cloth[j].first.second) dp[0][j]=0;
  FOR(i,1,d)
    REP(j,n)
    if(cloth[j].first.first <= hot[i] && hot[i] <= cloth[j].first.second)
	REP(k,n) dp[i][j]=max(dp[i][j],dp[i-1][k]+abs(cloth[j].second-cloth[k].second));
  int answer=0;
  REP(j,n) answer=max(answer,dp[d-1][j]);
  cout << answer << endl;
  return 0;
}

C++: #include <cstdio>
#include <algorithm>

using namespace std;

#define INF (1<<28)
#define MD 200
#define MN 200
int D, N;
int T[MD+1];
int CA[MN+1], CB[MN+1], CC[MN+1];

int solve() {
    int dp[D][N];
    for (int i = 0; i < D; i++) {
        for (int j = 0; j < N; j++) {
            if (CA[j] <= T[i] && T[i] <= CB[j]) {
                dp[i][j] = 0;
            } else {
                dp[i][j] = -1;
            }
        }
    }
    for (int i = 1; i < D; i++) {
        for (int j = 0; j < N; j++) {
            if (dp[i][j] < 0) continue;
            for (int k = 0; k < N; k++) {
                if (dp[i-1][k] < 0) continue;
                dp[i][j] = max(dp[i][j], dp[i-1][k] + abs(CC[j] - CC[k]));
            }
        }
    }
    int ret = 0;
    for (int i = 0; i < D; i++) {
        for (int j = 0; j < N; j++) {
            ret = max(ret, dp[i][j]);
        }
    }
    //for (int i = 0; i < D; i++) {
    //    for (int j = 0; j < N; j++) {
    //        printf("%4d", dp[i][j]);
    //    }
    //    printf("\n");
    //}
    return 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", &CA[i], &CB[i], &CC[i]);
    printf("%d\n", solve());
    return 0;
}