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 rep(i,n) for(int i = 0; i < (int)(n); i++)

int dp[20001][256];
const int inf = 1e9;

int main(){
    int n, m;
    while (cin >> n >> m && n){
        vector<int> c(m);
        rep(i,m) cin >> c[i];
        rep(i,n+1) rep(j,256) dp[i][j] = inf;
        dp[0][128] = 0;
        rep(i,n){
            int x; cin >> x;
            rep(j,256){
                rep(k,m){
                    int y = j + c[k];
                    if(y > 255) y = 255;
                    if(y < 0) y = 0;
                    int d = x - y;
                    dp[i+1][y] = min(dp[i+1][y], dp[i][j]+d*d);
                }
            }
        }
        cout << *min_element(dp[n], dp[n+1]) << endl;
    }
}

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

const int MAXN = 20001;
const int MAXM = 16;
const int MAXX = 256;
const int INF = 1<<28;

int N, M;
int C[MAXM], x[MAXN];
int dp[2][MAXX];

int main() {
  while(cin >> N >> M && (N|M)) {
    for(int i = 0; i < M; ++i) cin >> C[i];
    for(int i = 0; i < N; ++i) cin >> x[i];
    fill(dp[0], dp[2], INF);
    dp[0][128] = 0;
    for(int i = 0; i < N; ++i) {
      const int now = i & 1, next = 1 - now;
      for(int j = 0; j < MAXX; ++j) dp[next][j] = INF;
      for(int j = 0; j < MAXX; ++j) {
        for(int k = 0; k < M; ++k) {
          int nj = max(0, min(MAXX - 1, j + C[k]));
          dp[next][nj] = min(dp[next][nj],
                             dp[now][j] + (x[i] - nj) * (x[i] - nj));
        }
      }
    }
    cout << *min_element(dp[N&1], dp[N&1] + MAXX) << endl;
  }
  return 0;
}

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

int main(){
    while(1){
        int n, m; cin >> n >> m;
        if(!n) break;
        vector<int> c(m);
        vector<int> 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(256, vector<int> (n + 1, INF));
        for(int i = 0; i <= 255; i++) dp[i][n] = 0;
        for(int j = n - 1; j >= 0; j--){
            for(int i = 0; i <= 255; i++){
                for(int k = 0; k < m; k++){
                    int t = i + c[k];
                    if(t < 0) t = 0;
                    if(t > 255) t = 255;
                    dp[i][j] = min(dp[i][j], (t - x[j]) * (t - x[j]) + dp[t][j + 1]);
                }
            }
        }

        cout << dp[128][0] << endl;
    }
    return 0;
}

C++: #include<bits/stdc++.h>
#define LL long long
#define REP(i,n) for(int i=0;i<(n);++i)
#define REPA(i,n) for(int i=1;i<(n);++i)
#define PII pair<int,int>
#define PLI pair<long long, int>
#define PLL pair<long long, long long>
#define MOD ((int)1e9 + 7)
#define INF ((int)2e9)
#define INFLL ((LL)1e18)
#define ALL(x) (x).begin(),(x).end()
#define CTOI(x) (x - 'A') 
#define ctoi(x) (x - 'a')
#define BIT(x) (1 << (x))
using namespace std;

LL modinv(LL a){
    LL b = MOD, u = 1, v = 0;
    while(b){
        LL t = a / b;
        a -= t * b;swap(a, b);
        u -= t * v;swap(u, v);
    }
    u%=MOD;
    if(u < 0) u += MOD;
    return u;
}

LL exp(LL a, LL b){
    LL res = 1;
    LL sum = a;
    while(b){
        if(b&1)res = (res * sum) % MOD;
        sum = (sum * sum) % MOD;
        b>>=1;
    }
    return res;
}

int count(int x){
    int res = 0;
    while(x){
        res += x & 1;
        x>>=1;
    }
    return res;
}

vector<int> getsosu(int N){
    vector<int> res;
    vector<int> check(N+1, true);
    for(int i=2;i<=N;++i){
        if(check[i]){
            res.push_back(i);
            for(int j=i*2;j<=N;j+=i){
                check[j]=false;
            }
        }
    }
    return res;
}

vector<int>y;
vector<int> C;
int N, M;

int main(){
    while(cin >> N >> M && N){
        C = vector<int>(M);
        REP(i, M)cin>>C[i];
        y = vector<int>(N);
        REP(i, N)cin>>y[i];
        vector<vector<int> > dp(N+1, vector<int>(256, INF));
        dp[0][128] = 0;
        REP(i, N){
            REP(j, 256){
                REP(k, M){
                    int d = max(0, min(255, j + C[k]));
                    dp[i+1][d] = min(dp[i+1][d], (y[i]-d) * (y[i]-d) + dp[i][j]);
                }
            }
        }
        int ans = INF;
        REP(i, 256){
            ans = min(ans, dp.back()[i]);
        }
        cout << ans << endl;
    }
    return 0;
}

C++: #include<iostream>
#include<cstdio>
#include<vector>
using namespace std;

#define REP(i,n) for(int i=0;i<(int)(n);++i)

int main() {
  for(int N, M; cin >> N >> M, (N || M); ) {
    int C[M]; REP(i, M) scanf("%d", &C[i]);
    int dp[2][256] = {};

    const int inf = 987654321;
    REP(i, 256) dp[0][i] = inf;
    dp[0][128] = 0;
    for(int n = 1; n <= N; n++) {
      int x; scanf("%d", &x);
      int cur = n & 1;
      int prev = 1 - cur;
      REP(i, 256) dp[cur][i] = inf;
      REP(i, 256) {
        REP(m, M) {
          int j = i + C[m];
          if( j < 0 ) j = 0;
          if( j > 255 ) j = 255;
          dp[cur][j] = min(dp[cur][j], dp[prev][i] + (x - j) * (x - j));
        }
      }
    }
    int best = inf;
    REP(i, 256) best = min(best, dp[N & 1][i] );
    cout << best << endl;
  }
  return 0;
}