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 <cstdio>
#include <algorithm>
using namespace std;
int main(){
    int D,N,ts[210],mins[210],maxs[210];
    scanf("%d%d",&D,&N);
    for(int i=0;i<D;i++){
        scanf("%d",&ts[i]);
        mins[i]=200;
        maxs[i]=-1;
    }
    for(int i=0;i<N;i++){
        int a,b,c;
        scanf("%d%d%d",&a,&b,&c);
        for(int j=0;j<D;j++){
            if(a<=ts[j] && ts[j]<=b){
                mins[j]=min(mins[j],c);
                maxs[j]=max(maxs[j],c);
            }
        }
    }
    int dp[210][2];
    dp[0][0]=dp[0][1]=0;
    for(int i=1;i<D;i++){
        dp[i][0]=max(dp[i-1][0]+abs(mins[i-1]-mins[i]),dp[i-1][1]+abs(maxs[i-1]-mins[i]));
        dp[i][1]=max(dp[i-1][0]+abs(mins[i-1]-maxs[i]),dp[i-1][1]+abs(maxs[i-1]-maxs[i]));
    }
    printf("%d\n",max(dp[D-1][0],dp[D-1][1]));
}

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

using namespace std;

#define MAX_D 210
#define MAX_N 210

int D, N;
int T[201];
int A[201], B[MAX_N], C[MAX_N];
bool suit[MAX_D][MAX_N];
int dp[MAX_D][MAX_N];

int main() {
    int i, j, k;
    
    scanf("%d%d", &D, &N);
    for (i = 0; i < D; ++i) {
        scanf("%d", &T[i]);
    }
    for (j = 0; j < N; ++j) {
        scanf("%d%d%d", &A[j], &B[j], &C[j]);
    }
    
    for (i = 0; i < D; ++i) for (j = 0; j < N; ++j) {
        suit[i][j] = (A[j] <= T[i] && T[i] <= B[j]);
    }
    
    for (i = 0; i < D; ++i) for (j = 0; j < N; ++j) {
        //	-1 : impossible
        dp[i][j] = -1;
    }
    for (j = 0; j < N; ++j) if (suit[0][j]) {
        dp[0][j] = 0;
    }
    for (i = 0; i < D - 1; ++i) for (j = 0; j < N; ++j) if (dp[i][j] >= 0) {
        for (k = 0; k < N; ++k) if (suit[i + 1][k]) {
            if (dp[i + 1][k] < dp[i][j] + abs(C[j] - C[k])) {
                dp[i + 1][k] = dp[i][j] + abs(C[j] - C[k]);
            }
        }
    }
    
    int ans = -1;
    for (j = 0; j < N; ++j) {
        if (ans < dp[D - 1][j]) {
            ans = dp[D - 1][j];
        }
    }
    printf("%d\n", ans);
    
    return 0;
}

C++: #include <vector>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <algorithm>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <string>
#include <cstring>
#include <complex>
#include <ctime>
#include <cstdlib>

using namespace std;

inline int to_int(string s) {int v; istringstream sin(s); sin >> v; return v;}
template<class T> inline string to_str(T x) {ostringstream sout; sout << x; return sout.str();}

typedef long long ll;

int main()
{
	int d, n, T[64], low[256], hi[256], M[256];
	int dp[256][256]; //dp[i][j] : i日目に服jを着た時の派手さの最大値

	cin >> d >> n;

	for(int i = 0; i < d; i++)
	{
		cin >> T[i];
	}
	for(int i = 0; i < n; i++)
	{
		cin >> low[i] >> hi[i] >> M[i];
	}	

	for(int i = 0; i < 201; i++)
	{
		for(int j = 0; j < 201; j++)
		{
			dp[i][j] = 0;
		}
	}

	int res = 0;

	for(int i = 1; i < d; i++)
	{
		for(int j = 0; j < n; j++)
		{
			if(T[i] >= low[j] && T[i] <= hi[j])
			{
				for(int k = 0; k < n; k++)
				{
					if(low[k] <= T[i-1] && hi[k] >= T[i-1])
					{
						dp[i][j] = max(dp[i][j], abs(M[j]-M[k])+ dp[i-1][k]);
						res = max(res, dp[i][j]);
					}
				}
			}
		}
	}

	cout << res << endl;
}

C++: 
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <string>
#include <cstring>
#include <deque>
#include <list>
#include <queue>
#include <stack>
#include <vector>
#include <utility>
#include <algorithm>
#include <map>
#include <set>
#include <complex>
#include <cmath>
#include <limits>
#include <cfloat>
#include <climits>
#include <ctime>
#include <cassert>
#include <numeric>
#include <fstream>
#include <functional>
#include <bitset>

using namespace std;
#define int long long int
const int INF = 1001001001001001LL;

int di[] = {-1, 0, 1, 0, -1, 1, 1, -1};
int dj[] = {0, 1, 0, -1, 1, 1, -1, -1};

signed main(){
 
    while(1){
        int n; cin >> n;
        
        vector<pair<int, int> > ans(n);
        for(int i = 0; i < n; i++){
            int bangou; cin >> bangou;
            ans[i].second = bangou;
            int score = 0;
            int pre = 0; // 1 := スペア 2 := ストライク
            for(int j = 0; j < 9; j++){
                int val; cin >> val;
                if(val == 10){
                    if(pre > 0) score += val;
                    pre = 2;
                    score += val;
                    
                    continue;           
                }else{
                    if(pre > 0) score += val;
                    score += val;
                }

                int val2; cin >> val2;
                if(val + val2 == 10){
                    if(pre >= 2) score += val2;
                    pre = 1;
                    score += val2;
                }else{
                    if(pre >= 2) score += val2;
                    score += val2;
                }
            }

            int val; cin >> val;
            if(pre > 0) score += val;
            score += val;
            int val2; cin >> val2;
            if(pre >= 2) score += val2;
            score += val2;
            if(val + val2 == 10){
                int val3; cin >> val3;
                score += val3;
            }
            
            ans[i].first = -score;

        }

    
        sort(ans.begin(), ans.end());
        for(int i = 0; i < n; i++) cout << ans[i].first << " " << ans[i].second << endl;
    }

    return 0;
}

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

int temp[201];
int a[201], b[201], c[201];

int dp[201][201];

int main() {
  int D,N;
  cin>>D>>N;
  for (int i=0; i<D; ++i) cin>>temp[i];
  for (int i=0; i<N; ++i) cin>>a[i]>>b[i]>>c[i];

  for (int i=0; i<D; ++i)
    for (int j=0; j<201; ++j)
      dp[i][j] = 1<<29;

  for (int i=0; i<N; ++i) {
    if (temp[0] <= b[i] && a[i] <= temp[0])
      dp[0][c[i]] = 0;
  }

  for (int i=1; i<D; ++i) {
    for (int j=0; j<N; ++j) {
      if (temp[i] <= b[j] && a[j] <= temp[i]) {
        for (int k=0; k<201; ++k) {
          if (dp[i-1][k] == 1<<29) continue;
          if (dp[i][c[j]] == 1<<29)  dp[i][c[j]] = dp[i-1][k]+abs(k-c[j]);
          dp[i][c[j]] = max(dp[i-1][k]+abs(k-c[j]), dp[i][c[j]]);
        }
      }
    }
  }

  int ans = 0;
  for (int i=0; i<201; ++i) {
    if (dp[D-1][i] == 1<<29) continue;
    ans = max(ans, dp[D-1][i]);
  }
  cout<<ans<<endl;
}