Go: package main

import (
	"bufio"
	_ "container/heap"
	"fmt"
	"os"
	_ "sort"
	"strconv"
)

// I/O
type Scanner struct {
	sc *bufio.Scanner
}

func NewScanner() *Scanner {
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)
	sc.Buffer(make([]byte, 1024), int(1e+9))
	return &Scanner{sc}
}

func (s *Scanner) nextStr() string {
	s.sc.Scan()
	return s.sc.Text()
}

func (s *Scanner) nextInt() int {
	i, e := strconv.Atoi(s.nextStr())
	if e != nil {
		panic(e)
	}
	return i
}

func (s *Scanner) nextFloat() float64 {
	f, e := strconv.ParseFloat(s.nextStr(), 64)
	if e != nil {
		panic(e)
	}
	return f
}

func (s *Scanner) nextRuneSlice() []rune {
	return []rune(s.nextStr())
}

func (s *Scanner) nextIntSlice(n int) []int {
	res := make([]int, n)
	for i := 0; i < n; i++ {
		res[i] = s.nextInt()
	}
	return res
}

func (s *Scanner) nextFloatSlice(n int) []float64 {
	res := make([]float64, n)
	for i := 0; i < n; i++ {
		res[i] = s.nextFloat()
	}
	return res
}

// Arithmetic
func max(nums ...int) int {
	m := nums[0]
	for _, i := range nums {
		if m < i {
			m = i
		}
	}
	return m
}

func min(nums ...int) int {
	m := nums[0]
	for _, i := range nums {
		if m > i {
			m = i
		}
	}
	return m
}

func abs(x int) int {
	if x > 0 {
		return x
	}
	return -x
}

func pow(x, y int) int {
	res := 1
	for i := 0; i < y; i++ {
		res *= x
	}
	return res
}

func ceil(a, b int) int {
	if a%b == 0 {
		return a / b
	} else {
		return a/b + 1
	}
}

func gcd(a, b int) int {
	if b == 0 {
		return a
	}
	return gcd(b, a%b)
}

func lcm(a, b int) int {
	return a / gcd(a, b) * b
}

// Sort
type RuneSlice []rune

func (a RuneSlice) Len() int           { return len(a) }
func (a RuneSlice) Less(i, j int) bool { return a[i] < a[j] }
func (a RuneSlice) Swap(i, j int)      { a[i], a[j] = a[j], a[i] }

// Main
const MOD = int(1e+9) + 7
const INF = 1 << 60

func main() {
	sc := NewScanner()
	wtr := bufio.NewWriter(os.Stdout)
	defer wtr.Flush()
	for true {
		N, M := sc.nextInt(), sc.nextInt()
		if N == 0 && M == 0 {
			break
		}
		C := sc.nextIntSlice(M)
		X := sc.nextIntSlice(N)
		dp := make([][256]int, N+1)
		for i := 0; i < N+1; i++ {
			for j := 0; j < 256; j++ {
				dp[i][j] = INF
			}
		}
		dp[0][128] = 0
		for i := 0; i < N; i++ {
			for j := 0; j < 256; j++ {
				if dp[i][j] == INF {
					continue
				}
				for k := 0; k < M; k++ {
					nex := j + C[k]
					if nex < 0 {
						nex = 0
					}
					if nex > 255 {
						nex = 255
					}
					dp[i+1][nex] = min(dp[i+1][nex], dp[i][j]+pow(X[i]-nex, 2))
				}
			}
		}
		ans := INF
		for i := 0; i < 256; i++ {
			ans = min(ans, dp[N][i])
		}
		fmt.Fprintln(wtr, ans)
	}
}

C++: #include<iostream>
#include<string.h>
#include<vector>
using namespace std;

int table[256],ttable[256];
const int inf = 100000000;

int main() {

  int n,m,t,next;
  while(1) {
    vector<int> C;
    cin>>n>>m;
    if(!n && !m) break;

    for(int l=0; l<256; l++)
      table[l] = -1;
    table[128] = 0;

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

    for(int i=0; i<n; i++) {
      for(int l=0; l<256; l++)
	ttable[l] = inf;
      cin>>t;

      for(int j=0; j<256; j++) {
	if(table[j] != -1) {
	  for(int k=0; k<C.size(); k++) {
	    next = j+C[k];
	    if(next < 0) next = 0;
	    if(next > 255) next = 255;
	    ttable[next] = min(ttable[next], table[j]+(t-next)*(t-next));
	  }
	}
      }

      for(int l=0; l<256; l++) {
	table[l] = -1;
	if(ttable[l] < inf) table[l] = ttable[l];
      }
    }

    int ans = inf;
    for(int i=0; i<256; i++)
      if( table[i] != -1 && table[i] < ans ) {
	ans = table[i];
	//cout<<i<<endl;
      }
    cout<<ans<<endl;
  }
}

C++: #include <iostream>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;
//方針：DP
int min(int p, int q){
	if (p < q) return p;
	return q;
}
int rss(int p, int q){
	return (p - q)*(p - q);
}
int limit(int i) {
	if (i > 255) return 255;
	if (i < 0) return 0;
	return i;
}
int main(void){
	//FILE* fp_in = freopen("data.txt", "r", stdin);
 
	vector< vector< int > > dp;
	vector<int> dpp;
	for (int i = 0; i < 10000; i++)
		dpp.push_back(1 << 30);
	for (int i = 0; i < 256; i++)
		dp.push_back(dpp);
 
	while (1){
	int N, M;
	vector<int> c, x;
	cin >> N >> M;
	if (N == 0 && M == 0) break;
	for (int i = 0; i < M; i++) {
		int tmp;
		cin >> tmp;
		c.push_back(tmp);
	}
  for (int i = 0; i < N; i++) {
   int tmp;
   cin >> tmp;
   x.push_back(tmp);
  }
  for (int i = 0; i < 256; i++)
   for (int j = 0; j < 10000; j++)
    dp[i][j] = 1 << 30;
  
  for (int i = 0; i < 256; i++)
   dp[i][0] = 0;
  for (int i = 0; i < M; i++) {
   dp[limit(128 + c[i])][1] = rss(limit(128+c[i]), x[0]);
  }
  for (int i = 2; i <= N; i++) {
   for (int j = 0; j < M; j++) {
    for (int k = 0; k < 256; k++) {
     dp[k][i] = min(dp[limit(k-c[j])][i-1] + rss(k,x[i-1]), dp[k][i]);
	 if(k == 255 && c[j] > 0 ) dp[k][i] = min(dp[limit(k)][i-1] + rss(k,x[i-1]), dp[k][i]);
	 if(k == 0 && c[j] < 0 ) dp[k][i] = min(dp[limit(k)][i-1] + rss(k,x[i-1]), dp[k][i]);
    }
   }
  }
  int ans = 1 << 30;
  for (int i = 0; i < 256; i++) {
   if (dp[i][N] < ans) ans = dp[i][N];
  }
   

  cout << ans << endl;
 }
 
// while (1){}
 return 0;
}

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

#define max(a,b) ((a)>(b)?(a):(b))
#define min(a,b) ((a)<(b)?(a):(b))
#define abs(a) max((a),-(a))
#define rep(i,n) for(int i=0;i<(int)(n);i++)
#define repe(i,n) rep(i,(n)+1)
#define per(i,n) for(int i=(int)(n)-1;i>=0;i--)
#define pere(i,n) rep(i,(n)+1)
#define all(x) (x).begin(),(x).end()
#define SP <<" "<<
#define RET return 0
#define MOD 1000000007
#define INF 1000000000

typedef long long LL;
typedef long double LD;

int main(){
  while(1){
    int n,m;
    cin >> n >> m;
    if(n==0) return 0;
    vector<int> c(m),x(n);
    for(int i=0;i<m;i++) cin >> c[i];
    for(int i=0;i<n;i++) cin >> x[i];
    vector<vector<int>> dp(n+1,vector<int>(256,INF));
    dp[0][128]=0;
    for(int i=0;i<n;i++){
      for(int j=0;j<256;j++){
        for(int k=0;k<m;k++){
          dp[i+1][min(max(j+c[k],0),255)]=min(dp[i+1][min(max(j+c[k],0),255)],dp[i][j]+(x[i]-min(max(j+c[k],0),255))*(x[i]-min(max(j+c[k],0),255)));
        }
      }
    }
    int ans=INF;
    for(int i=0;i<256;i++) ans=min(ans,dp[n][i]);
    cout << ans << endl;
  }
  return 0;
}

C++: #include <iostream>
using namespace std; // 575
const int INF = 1073741823; // 10^30-1、使わない辺とかに用いる

int square(int N) { // 2乗を返すだけ
	return N * N;
}

int main() {
	int N, M;
	while (true) {
		cin >> N >> M;
		if (N == 0) break;
		int dp[20001][256]; // dp[i][j]は第i番目まで見てy[i]=jになってる時の最小誤差
		for (int i = 0;i < 20001;++ i) for (int j = 0;j < 256;++ j) dp[i][j] = INF;
		dp[0][128] = 0;
		int C[16], x[20000];
		for (int i = 0;i < M;++ i) cin >> C[i];
		for (int i = 0;i < N;++ i) cin >> x[i];
		
		for (int i = 0;i < N;++ i) {
			for (int j = 0;j < 256;++ j) {
				for (int k = 0;k < M;++ k) {
					int next = max(0, min(255, j + C[k])); // 次にC[k]を選んだ時の遷移先
					dp[i + 1][next] = min(dp[i + 1][next], dp[i][j] + square(x[i] - next));
				}
			}
		}
		int ans = dp[N][0]; // 最悪ケース: 誤差255が全部=1300500500、intに入る
		for (int j = 1;j < 256;++ j) ans = min(ans, dp[N][j]);
		cout << ans << endl;
	}
	return 0;
}