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 <string>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <map>
#include <set>
#include <algorithm>
#include <ctime>
#include <queue>
#include <stack>
#include <functional>
#include <bitset>
#include <assert.h>
using namespace std;
typedef long long ll;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<char> vc;
typedef vector<string> vs;
typedef vector<bool> vb;
typedef vector<double> vd;
typedef pair<ll,ll> P;
typedef vector<P> vpl;
typedef tuple<ll,ll,ll> tapu;
#define rep(i,n) for(ll i=0; i<(n); i++)
#define REP(i,a,b) for(int i=(a); i<(b); i++)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
const int inf = 1<<30;
const ll linf = 1LL<<62;
const int MAX = 510000;
ll dy[8] = {0,1,0,-1,1,-1,1,-1};
ll dx[8] = {1,0,-1,0,1,-1,-1,1};
const double pi = acos(-1);
const double eps = 1e-7;
template<typename T1,typename T2> inline bool chmin(T1 &a,T2 b){
	if(a>b){
		a = b; return true;
	}
	else return false;
}
template<typename T1,typename T2> inline bool chmax(T1 &a,T2 b){
	if(a<b){
		a = b; return true;
	}
	else return false;
}
template<typename T> inline void print(T &a){
    rep(i,a.size()) cout << a[i] << " ";
    cout << "\n";
}
template<typename T1,typename T2> inline void print2(T1 a, T2 b){cout << a << " " << b << "\n";}
template<typename T1,typename T2,typename T3> inline void print3(T1 a, T2 b, T3 c){
	cout << a << " " << b << " " << c << "\n";
}
//const int mod = 1e9 + 7;
const int mod = 998244353;

int main(){
	while(1){
		ll m,n; cin >> m >> n;
		if(n+m == 0) break;
		vl c(m),x(n);
		rep(i,n) cin >> x[i];
		rep(i,m) cin >> c[i];
		vvl dp(m+1,vl(300,linf));
		dp[0][128] = 0;
		rep(i,m){
			rep(j,256){
				if(dp[i][j] == linf) continue;
				rep(k,n){
					ll nxt = min(255LL, max(0LL, j+x[k]));
					chmin(dp[i+1][nxt], dp[i][j] + (nxt-c[i]) * (nxt-c[i]));
				}
			}
		}
		ll ans = linf;
		rep(i,256) chmin(ans, dp[m][i]);
		cout << ans << "\n";
	}
}

C++: #include<bits/stdc++.h>
using namespace std;
static const int MAX_N = 20000;
static const int MAX_M = 16;
static const int MAX_Y = 256;
static const int INF = 1 << 30;

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

int memo[MAX_Y][MAX_N + 1];

int main(){
	for(;;){
		scanf("%d %d", &N, &M);
		if(N == 0 && M == 0) break;
		for(int i = 0; i < M; i++) scanf("%d", &C[i]);
		for(int i = 0; i < N; i++) scanf("%d", &x[i]);
		fill(memo[0], memo[MAX_Y], INF);
		memo[128][0] = 0;
		for(int i = 0; i < N; i++){
			for(int j = 0; j < MAX_Y; j++){
				if(memo[j][i] == INF) continue;
				for(int k = 0; k < M; k++){
					int num = min(255, max(0, j + C[k]));
					memo[num][i + 1] = min(memo[num][i + 1], memo[j][i] + (num - x[i]) * (num - x[i]));
				}
			}
		}
		int res = INF;
		for(int i = 0; i < MAX_Y; i++) res = min(res, memo[i][N]);
		printf("%d\n", res);
	}
	return 0;
}

C++: #line 1 "../lib/template.hpp"


#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < n; i++)
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define ALL(v) (v).begin(), (v).end()
#define coutd(n) cout << fixed << setprecision(n)
#define ll long long int
#define vl vector<ll>
#define vi vector<int>

using namespace std;

#line 2 "tmp.cpp"

int main() {
  while (true) {
    int n, m;
    cin >> n >> m;
    if (n == 0 && m == 0)
      break;
    vector<vl> dp(n + 1, vl(256, 1e12));
    vl c(m);
    REP(i, m) { cin >> c[i]; }
    vl x(n);
    REP(i, n) { cin >> x[i]; }

    REP(j, 256) { dp[0][j] = 0LL; }
    FOR(i, 1, n + 1) {
      REP(j, 256) {
        REP(k, m) {
          ll ny = min(255LL, max(0LL, (i == 1 ? 128 + c[k] : j + c[k])));
          dp[i][ny] = min(dp[i][ny], dp[i - 1][j] + (ny - x[i - 1]) * (ny - x[i - 1]));
        }
      }
    }
    ll ans = 1e12;
    REP(j, 256) { ans = min(ans, dp[n][j]); }
    cout << ans << endl;
  }
  return 0;
}

C++: #include <iostream>
#include <vector>
#include <algorithm>

#define IF 1040401
#define lengthof(x) (sizeof(x) / sizeof(*(x)))

using namespace std;

int main(int argc, char const *argv[])
{
	int n,m;
	int dp[1<<8][2];
	int min_;

	while(1){
		cin>>n>>m;
		if(n+m==0) break;

		vector<int> cb(m);
		vector<int> x(n);
		for(int i1=0;i1<m;i1++){
			cin>>cb[i1];
		}

		for(int i1=0;i1<n;i1++){
			cin>>x[i1];
		}

		fill((int*)dp,(int *)(dp+lengthof(dp)),IF);

		dp[128][0]=0;
		for(int i1=0;i1<n;i1++){
			for(int i2=0;i2<(1<<8);i2++){
				if(dp[i2][i1%2]!=IF){

					for(int i3=0;i3<m;i3++){
						int temp=i2+cb[i3];
						if(temp<0) temp=0;
						if(temp>255) temp=255;
						dp[temp][(i1+1)%2]=min(dp[temp][(i1+1)%2],dp[i2][i1%2]+(temp-x[i1])*(temp-x[i1]));
					}
					dp[i2][i1%2]=IF;
				}

			}
		}

		min_=IF;
		for(int i1=0;i1<(1<<8);i1++){
			min_=min(min_,dp[i1][n%2]);
		}

		cout<<min_<<endl;
	}
	return 0;
}