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 ASC(vec) vec.begin(), vec.end()		// 昇順ソート 例：sort(ASC(vec));
#define DESC(vec) vec.rbegin(), vec.rend()	// 降順ソート 例:sort(DESC(vec));
#define rep(i, n) for(int i = 0; i < (n); i++)
#define Rep(i, n) for(int i = 1; i < n; i++)

const int mod = 1000000007;
const int inf = (int)1e9 + 7;
const long long INF = 1LL << 60;

using ii = pair<int, int>;
using ll = long long;
using vi = vector<int>;
using vd = vector<double>;
using vb = vector<bool>;
using vl = vector<ll>;
using vs = vector<string>;
using vvi = vector<vector<int>>;
using vvl = vector<vector<ll>>;
using vvb = vector<vector<bool>>;
using vii = vector<pair<int, int>>;

template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }

ll GCD(ll a, ll b) { return b ? GCD(b, a % b) : a; }
ll LCM(ll a, ll b) { return (a * b) / GCD(a, b); }
int round_int(int a, int b) { return (a + (b - 1)) / b; }

// 素因数分解; 返り値は(素数, 乗数)のvector配列
vector<pair<ll, int>> factorize(ll n) {
	vector<pair<ll, int>> res;
	for (ll i = 2; i * i <= n; ++i) {
		if (n % i) continue;
		res.emplace_back(i, 0);
		while (n % i == 0) {
			n /= i;
			res.back().second++;
		}
	}
	if (n != 1) res.emplace_back(n, 1);
	return res;
}

constexpr array<int, 9> dx = { 0, 1, 0, -1, -1, 1, 1, -1, 0 };
constexpr array<int, 9> dy = { 1, 0, -1, 0, 1, 1, -1, -1, 0 };
//constexpr auto MAX = 100000;

// ──────────────────────────────────────────────────────────────────

int dp[20001][256];

int main() {
	vi ans;

	while (true) {

		int n, m;
		cin >> n >> m;

		if (!n && !m) break;

		vi c(m), x(n);
		rep(i, m) cin >> c[i];
		rep(i, n) cin >> x[i];

		memset(dp, inf, sizeof(dp));
		dp[0][128] = 0;

		Rep(i, n + 1) {
			rep(j, 256) {
				rep(k, m) {
					if (dp[i][j] == inf) continue;

					int a = j + c[k];

					if (a < 0) a = 0;
					else if (a > 255) a = 255;

					int b = (x[i - 1] - a) * (x[i - 1] - a);

					chmin(dp[i][a], dp[i - 1][j] + b);
				}
			}
		}

		ans.push_back(*min_element(dp[n], dp[n] + 256));
	}
	rep(i, ans.size()) cout << ans[i] << endl;
}

C++: #include<iostream>
#include<vector>
#include<algorithm>
#define INF 100000000
using namespace std;
int main(){
	int n,m,x,dp[20][260],cb[20];//now,now
	int temp;
	while(cin>>n>>m,n||m){
		vector<int> input;
		for(int i=0;i<m;i++)
			cin>>cb[i];
		for(int i=0;i<n;i++){
			cin>>x;
			input.push_back(x);
		}
		for(int i=0;i<=n;i++)
			fill(dp[i],dp[i]+256,INF);
		dp[0][128]=0;
		for(int i=0;i<n;i++)
		for(int j=0;j<=255;j++){
				if(dp[i][j]!=INF){
					for(int k=0;k<m;k++){
						temp=j+cb[k];
						if(temp>255)
						temp=255;
						else if(temp<0)
						temp=0;
						dp[i+1][temp]=min(dp[i+1][temp],dp[i][j]+(temp-input[i])*(temp-input[i]));
					}
				}
		}
		int ans=INF;
		for(int i=0;i<=255;i++){
		if(ans>dp[n][i])
		ans=dp[n][i];
		}
		cout<<ans<<endl;
	}
	return 0;
}

C++: #include<bits/stdc++.h>
#define enld '\n'
#define rep(i,n) for(int i=0; i<(n); i++)
using namespace std;
using ll = long long;
using P = pair<int,int>;
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#pragma GCC optimize("Ofast")
constexpr ll INF = 1e18;
constexpr int inf = 1e9;
constexpr double INFD = 1e100;
constexpr ll mod = 1000000007;
constexpr ll mod2 = 998244353;
const double PI = 3.1415926535897932384626433832795028841971;
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
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; }
// ios::sync_with_stdio(false);
// cin.tie(nullptr);
// ---------------------------------------------------------------------------


int main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int N,M;
    cin >> N >> M;
    while(N != 0){
        vector<ll> C(M);
        for(int i=0; i<M; i++){
            cin >> C[i];
        }
        vector<ll> X(N);
        for(int i=0; i<N; i++){
            cin >> X[i];
        }
        vector<vector<ll>> dp(N+1, vector<ll> (256,INF));
        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++){
                    if(C[k] < 0){
                        chmin(dp[i+1][max(0LL,C[k]+j)],dp[i][j]+(max(0LL,C[k]+j)-X[i])*(max(0LL,C[k]+j)-X[i]));
                    }else{
                        chmin(dp[i+1][min(255LL,C[k]+j)],dp[i][j]+(min(255LL,C[k]+j)-X[i])*(min(255LL,C[k]+j)-X[i]));
                    }
                }
            }
        }
        ll ans = INF;
        for(int i=0; i<=255; i++){
            if(dp[N][i] == INF) continue;
            chmin(ans,dp[N][i]);
        }
        cout << ans << enld;
        cin >> N >> M;
    }


    return 0;
}