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 1e9
#define ll long long
#define ull unsigned long long
#define M 1000000007
#define P pair<int,int>
#define PLL pair<ll,ll>
#define FOR(i,m,n) for(int i=m;i<n;i++)
#define RFOR(i,m,n) for(int i=m;i>=n;i--)
#define rep(i,n) FOR(i,0,n)
#define rrep(i,n) RFOR(i,n,0)
#define all(a) a.begin(),a.end()
const int vx[4] = {0,1,0,-1};
const int vy[4] = {1,0,-1,0};
#define PI 3.14159265

int dp[21000][300];

void f(int n,int m){

  FOR(i,1,n+1){
    rep(j,300){
      dp[i][j] = inf;
    }
  }
  int c[1000];
  rep(i,m){
    cin>>c[i];
  }

  FOR(i,1,n+1){
    int x;
    cin>>x;
    rep(j,256){
      rep(k,m){
        int a = j+c[k];
        a = min(255,a);
        a = max(0,a);
        int d = (a-x)*(a-x);
        dp[i][a] = min(dp[i][a],dp[i-1][j]+d);
      }
    }
  }
  int ans = inf;
  rep(i,256){
    ans = min(ans,dp[n][i]);
  }

  cout<<ans<<endl;

}

int main(){
  while(1){
    int n,m;
    cin>>n>>m;
    if(n==0&&m==0) break;
    f(n,m);
  }

  return 0;
}

C++: #include <sstream>
#include <string>
#include <vector>
#include <map>
#include <algorithm>
#include <iostream>
#include <utility>
#include <set>
#include <cctype>
#include <queue>
#include <stack>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <functional>

using namespace std;

#define INF 1000000000
#define EPS 1e-9
#define PI (atan(1.0) * 4.0)

typedef pair<int, int> P;
typedef long long ll;

#define MAX_M 16
#define MAX_N 20000

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

ll dp[MAX_N+1][256];

vector<int> ans_list;

int main(){

	while (true){

		cin >> N >> M;

		if (N == 0 && M == 0)
			break;

		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 + 1; i++){
			for (int j = 0; j <= 255; j++){
				dp[i][j] = INF;
			}
		}

		dp[0][128] = 0;

		for (int i = 0; i < N; i++){
			for (int j = 0; j <= 255; j++){
				for (int m = 0; m < M; m++){
					if (j + C[m] < 0){
						dp[i + 1][0] = min(dp[i + 1][0], dp[i][j] + (0 - x[i])*(0 - x[i]));
					}
					else if (j + C[m] > 255){
						dp[i + 1][255] = min(dp[i + 1][255], dp[i][j] + (255 - x[i])*(255 - x[i]));
					}
					else
						dp[i + 1][j + C[m]] = min(dp[i + 1][j + C[m]], dp[i][j] + (j + C[m] - x[i])*(j + C[m] - x[i]));
				}
			}
		}

		ll ans = INF;
		for (int i = 0; i <= 255; i++){
			ans = min(ans, dp[N][i]);
		}

		ans_list.push_back(ans);

	}

	for (int i = 0; i < ans_list.size(); i++){
		cout << ans_list[i] << endl;
	}

	return 0;

}

C++: #include<iostream>
#include<vector>
#include<utility>
#include<cmath>

#define INF (1 << 29)

using namespace std;

int fix(int num)
{
    return min(255, max( num, 0) );
}

int main()
{
    int N, M;
    while(cin >> N >> M, (N))
    {

        vector<int> in_C(M);
        for(auto& _c : in_C)
        {
            cin >> _c;
        }

        vector<int> in_X(N);
        for(auto& _x : in_X)
        {
            cin >> _x;
        }

        int dp[2][256];
        for(int i = 0; i < 256; i++)
        {
            dp[0][i] = dp[1][i] = INF;
        }

        for(int i = 0; i < M; i++)
        {
            int sub = fix(128 + in_C[i]);
            dp[0][sub] = pow(in_X[0] - sub, 2.0);
        }

        for(int n = 1; n < N; n++)
        {
            for(int i = 0; i < 256; i++)
            {
                if(dp[0][i] == INF)continue;
                for(int j = 0; j < M; j++)
                {
                    int sub = fix(i + in_C[j]);
                    int nextScore = dp[0][i] + pow( in_X[n] - sub, 2.0 );
                    dp[1][sub] = min(dp[1][sub], nextScore);
                    /*
                    cout << sub << endl;
                    cout << dp[0][sub] << endl;
                    cout << pow( in_X[n] - sub, 2.0 ) << endl;
                    cout << endl;
                    //*/
                }
            }
            for(int i = 0; i < 256; i++)
            {
                dp[0][i] = dp[1][i];
                dp[1][i] = INF;
            }
        }

        int ans = INF;
        for(int i = 0; i < 256; i++)
        {
            ans = min(ans, dp[0][i]);
            //cout << i << " : "<< ans << endl;
        }
        cout << ans << endl;

    }



    return 0;
}

C++: #include<deque>
#include<list>
#include<map>
#include<queue>
#include<set>
#include<stack>
#include<vector>
#include<algorithm>
#include<string>
#include<iostream>
#include<sstream>
#include<cmath>
#include<cstdio>
#include<cstring>
using namespace std;
int a[16],b[20000],c[20001][256],n,m;
int fi(int p,int q){
  int i;
  if(c[p][q]>=0)
    return c[p][q];
  int mn=1<<30;
  for(i=0;i<m;++i){
    int r=min(max(0,q+a[i]),255),s=(b[p]-r)*(b[p]-r);
    if(0){
    }else if(mn>fi(p+1,r)+s){
      mn=c[p+1][r]+s;
    }else if(mn<c[p+1][r]+s){
      break;
    }
  }
  return c[p][q]=mn;
}
int main(){
  int i;
  //int n,m;
  while(cin>>n>>m,n||m){
    //int a[16],b[20000];
    for(i=0;i<m;++i)
      cin>>a[i];
    sort(a,a+m);
    for(i=0;i<n;++i)
      cin>>b[i];
    memset(c,-1,n*256*sizeof(int));
    memset(c[n],0,256*sizeof(int));
    cout<<fi(0,128)<<endl;
  }
  return 0;
}