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 <cstdlib>
#include <cmath>
#include <climits>
#include <cfloat>
#include <map>
//#include <utility>
#include <set>
#include <iostream>
//#include <memory>
#include <string>
#include <vector>
#include <algorithm>
//#include <functional>
#include <sstream>
//#include <deque>
#include <complex>
#include <stack>
#include <queue>
#include <cstdio>
//#include <cctype>
#include <cstring>
//#include <ctime>
#include <iterator>
#include <bitset>
//#include <numeric>
//#include <list>
//#include <iomanip>

#if __cplusplus >= 201103L
#include <array>
#include <tuple>
#include <initializer_list>
#include <unordered_set>
#include <unordered_map>
#include <forward_list>

#define cauto const auto&
#else

#endif

using namespace std;
#define int long long

namespace{
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;

typedef vector<int> vint;
typedef vector<vector<int> > vvint;
typedef vector<long long> vll, vLL;
typedef vector<vector<long long> > vvll, vvLL;

#define VV(T) vector<vector< T > >

template <class T>
void initvv(vector<vector<T> > &v, int a, int b, const T &t = T()){
    v.assign(a, vector<T>(b, t));
}

template <class F, class T>
void convert(const F &f, T &t){
    stringstream ss;
    ss << f;
    ss >> t;
}


#define reep(i,a,b) for(int i=(a);i<(b);++i)
#define rep(i,n) reep((i),0,(n))
#define ALL(v) (v).begin(),(v).end()
#define PB push_back
#define F first
#define S second
#define mkp make_pair
#define RALL(v) (v).rbegin(),(v).rend()


#define MOD 1000000007LL
#define EPS 1e-8
static const ll INF=LLONG_MAX;
/*
2
8
0
325125
65026
*/
vint dp(256,0);
void mainmain(){
	int n,m;
	while(cin>>n>>m,n||m){
		vint c(m),x(n);
		rep(i,m) cin>>c[i];
		rep(i,n) cin>>x[i];
		rep(i,256) dp[i]=INF;
		dp[128]=0;
		rep(i,n){
			vint tmp(256,INF);
			rep(j,m){
				rep(k,256){
					int t=k+c[j];
					if(t<0) tmp[0]=min(tmp[0],dp[k]);
					else if(t>255) tmp[255]=min(tmp[255],dp[k]);
					else tmp[t]=min(tmp[t],dp[k]);
				}
			}
			rep(j,256){
				if(tmp[j]==INF) continue;
				tmp[j]+=(j-x[i])*(j-x[i]);
			}
			dp=tmp;
			// rep(i,256) cout<<i<<" "<<dp[i]<<endl;
			// cout<<endl;
		}
		int ans=INF;
		rep(i,256){
			ans=min(ans,dp[i]);
		}
		cout<<ans<<endl;
	}
}



}
main() try{
    mainmain();
}
catch(...){
}

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

int foo(int huga){
	if(huga <= 0) return 0;
	else if(huga >= 255) return 255;
	return huga;
}

int main(){
  int N, M, C[16], x, y = 128, i, j, k, l, mi;
  	while(1){
		int  temp[20004][256] = {}, temp2[2][256] = {};
		temp2[0][128] = 1;
	    cin>>N>>M;
	if(N == 0 && M ==0)break;
    	for(i = 0; i < M; i++){
			cin>>C[i];
			temp2[1][foo(C[i] + 128)] = 1;
		}	
		for(i = 1; i <= N; i++){
			cin>>x;
			for(j = 0; j < 256; j++){
				if(!temp2[1][j])continue;
				mi = 2147483647;
				for(k = 0; k < M; k++){
					if(foo(j - C[k]) == 0){
						for(l = 255 - C[k]; l <= 255; l++){
							if(temp2[0][l]){
								mi = min(temp[i-1][l], mi);
							}
						}
					} else if (foo(j - C[k]) == 255){
						for(l = 0; l <= - C[k]; l++){
							if(temp2[0][l]){mi = min(temp[i-1][l], mi);}
						}
					}else {
						if(!temp2[0][foo(j - C[k])]){continue;}
						mi = min(temp[i-1][foo(j - C[k])], mi);
					}
				}
				temp[i][j] = mi + (x - j) * (x - j);
			}
			for(j = 0; j < 256; j++){
				temp2[0][j] = temp2[1][j];
				temp2[1][j] = 0;
			}
			for(j = 0; j < 256; j++){
				if(!temp2[0][j])continue;
				 for(k = 0; k < M; k++){
					temp2[1][foo(C[k] + j)] = 1;
				}
			}
		}
		mi = 2147483647;
		for(i = 0; i < 256; i++){
			if(!temp2[0][i])continue;
			mi = min(mi, temp[N][i]);
		}
		cout<<mi<<endl;
	}
  return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;
const long long int INF = 1e9;

int dp[20010][260];
int c[20], x[20010];

int main() {
    int n, m;

    while (true)
    {
        cin >> n >> m;
        if(n + 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 < 257; j++)
            {
                dp[i][j] = INF;
            }
        }
        dp[0][128] = 0;

        for (int i = 0; i < n; i++)
        {
            for (int j = 0; j < 256; j++)
            {
                for (int k = 0; k < m; k++)
                {
                    int ne = j + c[k];
                    ne = max(ne, 0);
                    ne = min(ne, 255);
                    dp[i+1][ne] = min(dp[i+1][ne], dp[i][j] + (x[i] - ne) * (x[i] - ne));
                }
            }
        }

        int ans = INF;
        for (int i = 0; i < 256; i++)
        {
            ans = min(ans, dp[n][i]);
        }

        cout << ans << endl;
    }

}