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>
#define rep(i,n) for(int i = 0;i<n;i++)
using namespace std;
typedef pair<int,int> P;
typedef long long ll;
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 int INF = 1001001001;

int main() {
    while(true) {
        int n,m;
        cin >> n >> m;
        if(n==0 && m == 0) break;
        vector<int>c(m),x(n);
        rep(i,m) cin >> c[i];
        rep(i,n) 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<=255;j++) {
                for(int k = 0;k<m;k++) {
                    int d = c[k] + j;
                    if(d<0) d = 0;
                    if(d > 255) d=255;
                    chmin(dp[i+1][d], dp[i][j]+(d-x[i])*(d-x[i]));
                }
            }
        }
        int ans = INF;
        for(int i = 0;i<=255;i++) {
            chmin(ans,dp[n][i]);
        }
        cout << ans << endl;
    }
}

C++: #include <algorithm>
#include <iostream>
#include <math.h>
#include <stdio.h>
#include <string>
#include <sstream>
#include <vector>
#include <set>
#include <stack>
#include <map>
#include <cmath>
#include <queue>
#define rep(i, n) for(int i = 0; i < (int)(n); i++)
using namespace std;
typedef unsigned long ll;
#define MAX_N 20001
#define MAX_X 256
#define INF 2000000000

int dp[MAX_N][MAX_X];

void init(){
	rep(i,MAX_N){
		rep(j,MAX_X) dp[i][j] = INF;
	}
}


int main()
{
	int N,M;
	cin>>N>>M;
	while( !(N == 0 && M == 0)){
		init();
		int *codeBook = new int[M];
		int *x = new int[N];
		rep(i,M) cin>>codeBook[i];
		rep(i,N) cin>>x[i];
		dp[0][128] = 0;
		for(int i = 0; i < N; i++){
			for(int j = 0; j <= 255; j++){
				if( dp[i][j] == INF) continue;
				for(int k = 0; k < M; k++){
					int next = j + codeBook[k];
					if(next <= 0){
						next = 0;
					}else if(next >= 255){
						next = 255;
					}
					dp[i+1][next] = min(dp[i+1][next], dp[i][j] + (next-x[i])*(next-x[i]));
				}
			}
		}
		int ans = INF;
		rep(i,256){
			ans = min(ans,dp[N][i]);
		}
		cout<<ans<<endl;
		cin>>N>>M;
	}
	return 0;
}

C++: #include <bits/stdc++.h>

using namespace std;
using LL = long long;
using P = pair<int, int>;
using Tapris = tuple<int, int, int>;

#define REP(i, n) for(LL i = 0; i < n; ++i)
#define FOR(i, a, n) for(LL i = a; i < n; ++i)
#define pb(a) push_back(a)
#define all(x) (x).begin(),(x).end()

const int INF = (int)1e9;
const LL INFL = (LL)1e15;
const int MOD = 1e9 + 7;

int dy[]={0, 0, 1, -1, 0};
int dx[]={1, -1, 0, 0, 0};

//#define int long long

/*************** using variables ***************/
int n, m;
vector<int> c, x;
LL dp[20005][550];
/**********************************************/

signed main(){
    while(cin >> n >> m, n != 0 || m != 0){
        c.resize(m);
        x.resize(n);
        REP(i, m) cin >> c[i];
        REP(i, n) cin >> x[i];

        REP(i, 20005) REP(j, 550) dp[i][j] = INF;
        dp[0][128 + 255] = 0;
        FOR(i, 1, n+1){
            for(int j = 0; j <= 255; j++){
                bool flag = false;
                if(j == 0){
                    REP(k, m){
                        for(int z = 0 - c[k]; z >= 0; z--){
                            if(dp[i-1][z + 255] != INF){
                                dp[i][0 + 255] = min(dp[i][0 + 255], dp[i-1][z + 255] + (LL)pow(abs(x[i-1] - (0)), 2));
                            }
                        }
                    }
                }else if(j == 255){
                    REP(k, m){
                        for(int z = 255 - c[k]; z <= 255; z++){
                            if(dp[i-1][z + 255] != INF){
                                dp[i][255 + 255] = min(dp[i][255 + 255], dp[i-1][z + 255] + (LL)pow(abs(x[i-1] - (255)), 2));
                            }
                        }
                    }
                }else{
                    REP(k, m) flag |= dp[i-1][j-c[k] + 255] != INF;
                    if(flag){
                        REP(k, m){
                            if(j-c[k] >= 0 && j-c[k] <= 255 && dp[i-1][j-c[k] + 255] != INF){
                                dp[i][j + 255] = min(dp[i][j + 255], dp[i-1][j-c[k] + 255] + (LL)pow(abs(x[i-1] - j), 2));
                                //cout << i << " " << dp[i][j + 255] << " " << x[i-1] << " " << j-c[k] << endl;
                            }
                        }
                    }
                }
            }
        }

        LL ans = INF;
        for(int i = 0; i <= 255; i++) ans = min(ans, dp[n][i + 255]);
        cout << ans << endl;
    }

}