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 <climits>
#include <algorithm>
#define MAX_N 20001
#define MAX_M 16
#define MAX_X 256
#define INIT 128
using namespace std;

long long dp[MAX_N][MAX_X];
int c[MAX_M];
int x[MAX_N];
int n, m;

void solve() {
  dp[0][128] = 0;
  for (int i = 0; i + 1 <= n; i++) {
    for (int j = 0; j < MAX_X; j++) {
      if (dp[i][j] == LONG_MAX) continue;

      for (int k = 0; k < m; k++) {
	int t = j + c[k];
	if (t < 0) t = 0;
	if (t > 255) t = 255;
	int s = (t - x[i + 1]) * (t - x[i + 1]);
	dp[i + 1][t] = min(dp[i][j] + s, dp[i + 1][t]);
      }
    }
  }
  long long ans = LONG_MAX;
  for (int i = 0; i < MAX_X; i++)
    ans = min(ans, dp[n][i]);
  cout << ans << endl;
}

void init() {
  for (int i = 0; i < MAX_N; i++)
    for (int j = 0; j < MAX_X; j++)
      dp[i][j] = LONG_MAX;

  for (int i = 0; i < MAX_M; i++)
    c[i] = 0;
  for (int i = 0; i < MAX_N; i++)
    x[i] = 0;
}

int main() {
  while (true) {
    cin >> n >> m;
    if (n == 0 && m == 0) return 0;

    init();
    for (int i = 0; i < m; i++)
      cin >> c[i];
    for (int i = 1; i <= n; i++)
      cin >> x[i];

    solve();
  }
}

C++: #include <bits/stdc++.h>
 
using namespace std;
 
#define INF 1001000100010001000
#define MOD 1000000007
#define EPS 1e-10
#define int long long
#define rep(i, N) for (int i = 0; i < N; i++)
#define Rep(i, N) for (int i = 1; i < N; i++)
#define For(i, a, b) for (int i = (a); i < (b); i++)
#define pb push_back
#define mp make_pair
#define i_i pair<int, int>
#define vi vector<int>
#define vvi vector<vi >
#define vb vector<bool>
#define vvb vector<vb >
#define vp vector< i_i >
#define all(a) (a).begin(), (a).end()
#define Int(x) int x; scanf("%lld", &x);
 
//int dxy[5] = {0, 1, 0, -1, 0};
// assign
 
signed main()
{
    Int(n);
    Int(m);

    while (m) {
        vi code(m);
        rep(i, m) {
            cin >> code[i];
        }
        vi x(n);
        rep(i, n) {
            cin >> x[i];
        }
        vvi dp(n, vi(256, INF));
        int st = 128;
        rep(i, m) {
            int tmp = st + code[i];
            tmp = (tmp < 0) ? 0 : tmp;
            tmp = (tmp > 255) ? 255 : tmp;
            dp[0][tmp] = min(dp[0][tmp], (x[0] - tmp) * (x[0] - tmp));
        }
        rep(i, n-1) {
            rep(j, 256) {
                if (dp[i][j] != INF) {
                    st = j;
                } else {
                    continue;
                }
                rep(k, m) {
                    int tmp = st + code[k];
                    tmp = (tmp < 0) ? 0 : tmp;
                    tmp = (tmp > 255) ? 255 : tmp;
                    dp[i+1][tmp] = min(dp[i+1][tmp], 
                            dp[i][j] + (x[i+1] - tmp) * (x[i+1] - tmp));
                }
            }
        }
        int ret = INF;
        rep(i, 256) {
            ret = min(ret, dp[n-1][i]);
        }
        cout << ret << endl;
        cin >> n >> m;
    }

    return 0;
}

C++: #include <iostream>

using namespace std;

long long int ans;

int min(int a,int b){
	if(a>=b)return b;
	else return a;
}

int main(){

	int n,m;
	long long int dp[256];
	long long int nextDp[256];
	int next;

	while(cin>>n>>m){
		ans = 1000000000000;
		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];
		}
		for(int i=0;i<256;i++){
			dp[i]=ans;
			nextDp[i]=ans;
		}

		for(int i=0;i<m;i++){
			next = 128+c[i];
			if(next>=255)next=255;
			if(next<=0)next = 0;
			dp[next] = (x[0]-next)*(x[0]-next);
		}
		/*
		for(int i=0;i<256;i++){
			cout<<dp[0][i]<<" ";
		}
		cout<<endl;
		*/
		for(int i=1;i<n;i++){
			for(int j=0;j<256;j++){
				for(int k=0;k<m;k++){
					next = j+c[k];
					if(next>=255)next=255;
					if(next<=0)next = 0;
					//dp[next] = min(dp[next],dp[j]+(x[i]-next)*(x[i]-next));
					nextDp[next] = min(nextDp[next],dp[j]+(x[i]-next)*(x[i]-next));
				}
			}
			for(int j=0;j<256;j++){
				dp[j]=nextDp[j];
				nextDp[j]=ans;
			}
		}
		for(int i=0;i<256;i++){
			ans = min(ans,dp[i]);
		}


		cout<<ans<<endl;

	}

	return 0;

}

C++: #include<iostream>
using namespace std;
#define rep(i, n) for ( int i = 0; i < n; i++ )
#define NMAX 20001
#define MMAX 16
#define INFTY (1<<30)
int N, M, C[MMAX], X[NMAX];
long long T[256][NMAX];

long long compute(){
    rep(i, 256) rep(j, N+1) T[i][j] = INFTY;
    T[128][0] = 0;
    rep(j, N){
	rep(i,256){
	    if ( T[i][j] == INFTY ) continue;
	    rep(k, M){
		int p = i + C[k];
		if ( p > 255 ) p = 255;
		if ( p < 0 ) p = 0;
		T[p][j+1] = min(T[p][j+1], T[i][j] + (X[j+1]-p)*(X[j+1]-p));
	    }
	}
    }
    long long minv = INFTY;
    rep(i, 256) minv = min(minv, T[i][N]);
    return minv;
}

main(){
    while(cin >> N >> M && N ){
	rep(i, M) cin >> C[i];
	rep(i, N) cin >> X[i+1];
	cout << compute() << endl;
    }
}