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 <bits/stdc++.h>
using namespace std;

#define INF 1e+9
    int dp[700][20100];
int main(){

  while(1){
    int N,M;
    cin >> N >> M;
    if(!N and !M) break;

    vector<int> c(M);
    for(int i=0; i<M; i++)
      cin >> c[i];
  
    vector<int> x(N+1);
    x[0] = 128;
    for(int i=1; i<=N; i++)
      cin >> x[i];

    // DP

    for(int i=0; i<600; i++)
      for(int j=0; j<20100; j++)
	dp[i][j] = INF;

    dp[128][0] = 0;
    
    for(int i=0; i<N; i++){
      for(int j=0; j<=255; j++){
	if(dp[j][i] != INF){
	  for(int dy : c){
	    int ny = j+dy;
	    if(ny < 0) ny = 0;
	    if(ny > 255) ny = 255;
	    dp[ny][i+1] = min(dp[ny][i+1],dp[j][i] + (x[i+1]-ny)*(x[i+1]-ny));
	  }
	}
      }
    }
  
    int ans = INF;
    for(int j=0; j<=255; j++){
      ans = min(dp[j][N],ans);
    }
    cout << ans << endl;

  }


}

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

int	main()
{
	int	N, M;
	while (cin >> N >> M) {
		if (N == 0 && M == 0) break;
		vector<vector<int>>	cost(N + 1, vector<int>(256, -1));
		cost[0][128] = 0;
		vector<int>	C_vec(M), x_vec(N);
		for (int i = 0; i < M; ++i)
			cin >> C_vec.at(i);
		for (int i = 0; i < N; ++i)
			cin >> x_vec.at(i);
		int	min_cost = -1;
		for (int i = 0; i < N; ++i) {
			for (int j = 0; j < 256; ++j) {
				if (cost[i][j] == -1) continue;
				for (int k = 0; k < M; ++k) {
					int	l = j + C_vec.at(k);
					if (l < 0) l = 0;
					if (l > 255) l = 255;
					int	cost_l = (l - x_vec.at(i)) * (l - x_vec.at(i));
					cost_l += cost[i][j];
					if (cost[i + 1][l] == -1 || cost[i + 1][l] > cost_l) {
						cost[i + 1][l] = cost_l;
						if (i + 1 == N) {
							if (min_cost == -1 || min_cost > cost_l)
								min_cost = cost_l;
						}
					}
				}
			}
		}
		cout << min_cost << endl;
	}
}

C++: #include <bits/stdc++.h>
using ll = long long;
using namespace std;
#define rep(i,n) for(int i=0, i##_len=(int)(n); i<i##_len; i++)
#define reps(i,n) for(int i=1 , i##_len=(int)(n);i<=i##_len;i++)
#define rrep(i,n) for(int i=((int)(n)-1);i>=0;i--)
#define rreps(i,n) for(int i=((int)(n));i>0;i--)
#define all(x) (x).begin(), (x).end()
#define F first
#define S second
#define mp make_pair
#define pb push_back
#define solve(a) ((a)?"Yes":"No")
typedef vector<long long> V;
typedef vector<V> VV;
typedef pair<long long , long long> P;
typedef vector<P> VP;
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1;} return 0;}
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1;} return 0;}
const long long INFLL = 1LL<<60;
const int INF = 1<<30;
const double PI=acos(-1);
int fash(int a){
	return(max(0,min(255,a)));
}
ll twox(ll a){
	return a*a;
}
int main(){
	int n,m;
	VV dp(20010,V(260));
	vector<int> c(20);
	vector<int> x(20010);
	ll ans=INFLL;
while(1){
	cin >>n>>m;
	if(n==0 and m==0)return 0;
	ans=INFLL;
	rep(i,20010){
		rep(j,260){
			dp[i][j]=INFLL;
		}
	}
	rep(i,m){
		cin>>c[i];
	}
	rep(i,n){
		cin>>x[i];
	}
	dp[0][128]=0LL;
	rep(i,n){
		rep(j,256){
			rep(at,m){
				chmin(dp[i + 1][fash(j + c[at])], dp[i][j] + twox(fash(j + c[at])-x[i]));
			}
		}
	}
	rep(i,256){
		chmin(ans,dp[n][i]);
	}
	cout<<ans<<endl;
	}
}

C++: #include <iostream>

using namespace std;

long long int ans;

void function(int y,int index,int *c,int *x,int n,int m,int now){

	//cout<<y<<" "<<index<<" "<<n<<" "<<m<<" "<<now<<endl;

	if(now>=ans){
		return;
	}
	if(index>=n){
		if(ans>=now){
			ans = now;
		}
		return;
	}
	for(int i=0;i<m;i++){
		function(y+c[i],index+1,c,x,n,m,now+(y+c[i]-x[index])*(y+c[i]-x[index]));
	}
}

int main(){

	int n,m;
	ans = 1000000000000000;

	while(cin>>n>>m){
		ans = 1000000000000000;
		if(n==0 && m==0){
			break;
		}
		int *c;
		int *x;
		c = new int[m];
		x = new int[n];

		for(int i=0;i<m;i++){
			cin>>c[i];
		}
		for(int i=0;i<n;i++){
			cin>>x[i];
		}
		function(128,0,c,x,n,m,0);
		cout<<ans<<endl;

	}

	return 0;

}

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

using namespace std;

#define MAX_M 16
#define MAX_N 20010
#define INF 1e8

int N, M, C[MAX_M], x[MAX_N];
int dp[MAX_N][256];

int main(){
  while(cin >> N >> M , (N | M)){
    for(int i = 0 ; i < M ; i++){
      cin >> C[i];
    }
 
    for(int i = 1 ; i <= N ; i++){
      cin >> x[i];
    }
    int ans = INF;
    fill(dp[0], dp[MAX_N], INF);
    dp[0][128] = 0;
    for(int i = 1 ; i <= N ; i++){
      for(int j = 0 ; j <= 255 ; j++){
        for(int k = 0 ; k < M ; k++){
          int t = j + C[k];
          if(t < 0) t = 0;
          if(t > 255) t = 255;
          dp[i][t] = min(dp[i][t], dp[i-1][j] + (t-x[i])*(t-x[i]));
        }
      }
    }
    for(int i = 0 ; i <= 255 ; i++){
      ans = min(ans, dp[N][i]);
    } 
    cout << ans << endl;
  }

  return 0;
}