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;
typedef long long ll;
typedef pair<int, int> P;
const ll MOD = 1000000007;
const int IINF = INT_MAX;
const ll LLINF = LLONG_MAX;
const int MAX_N = int(2e4 + 5);
#define REP(i, n) for (int i = 0; i < n; i++)

int n, m, c[16], x[MAX_N], dp[MAX_N][256];  // iまでみて信号をjにしたときの最小

int main() {
    while(cin >> n >> m, n){
        REP(i,m) cin >> c[i];
        REP(i,n) cin >> x[i];

        REP(i,n+1)fill(dp[i],dp[i]+256,IINF/2);
        dp[0][128] = 0;

        REP(i,n){
            REP(j,256){
                REP(k,m){
                    int y = max(0,min(255,j+c[k]));
                    dp[i+1][y] = min(dp[i+1][y], dp[i][j]+int(pow(x[i]-y,2)));
                }
            }
        }
        int ans = IINF;
        REP(i,256) ans = min(ans, dp[n][i]);
        cout << ans << endl;
    }
    return 0;
}

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

#define INF (1 << 29)

#define MAX_N 20000
#define MAX_M 16

using namespace std;

int N, M, C[MAX_M], X, dp[MAX_N + 1][256];

int main()
{
	while (true)
	{
		for (int i = 0; i < MAX_N + 1; i++)
		{
			for (int j = 0; j < 256; j++)
			{
				dp[i][j] = INF;
			}
		}

		dp[0][128] = 0;

		cin >> N >> M;

		if (N == 0 && M == 0) { break; }

		for (int i = 0; i < M; i++) { cin >> C[i]; }

		for (int i = 0; i < N; i++)
		{
			cin >> X;

			for (int j = 0; j < 256; j++)
			{
				for (int k = 0; k < M; k++)
				{
					int ptr = max(j + C[k], 0);

					ptr = min(ptr, 255);

					int sabun = (X - ptr) * (X - ptr);

					dp[i + 1][ptr] = min(dp[i + 1][ptr], dp[i][j] + sabun);
				}
			}
		}

		cout << *min_element(dp[N], dp[N] + 256) << endl;
	}

	return 0;
}

C++: #define _USE_MATH_DEFINES
#include <bits/stdc++.h>
// #define USE_INT_LONG_LONG
#ifdef USE_INT_LONG_LONG
#define int long long
    #define inf (1ll<<60)
#else
    #define inf (1<<30)
#endif
#define rep(i,n) for(int i=0;i<n;i++)
#define Rep(i,a,b) for(int i=a;i<b;i++)
#define REP(i,a,b) for(int i=a;i<=b;i++)
#define rev(i,n) for(int i=(n)-1;i>=0;i--)
#define vi vector<int>
#define vvi vector<vi>
#define pb push_back
#define pi pair<int,int>
#define vp vector<pair<int,int>>
#define mp make_pair
#define all(v) (v).begin(),(v).end()
#define fi first
#define se second
#define MEMSET(a) memset(a,0,sizeof(a))
#define Yes(f) cout<<(f?"Yes":"No")<<endl
#define yes(f) cout<<(f?"yes":"no")<<endl
#define YES(f) cout<<(f?"YES":"NO")<<endl
#define SORT(v) sort(all(v))
#define RSORT(v) sort(all(v), greater<int>())

using namespace std;

const int mod=1e9+7;
const string sp=" ";

void run();

void init() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout<<fixed<<setprecision(12);
}

signed main(){
    init();
    run();
    return 0;
}

void run(){
    int n,m;
    cin>>n>>m;
    if(!n)return;
    vi c(m);
    rep(i,m)cin>>c[i];
    vi x(n);
    rep(i,n)cin>>x[i];
    vvi dp(n+1,vi(256));
    rep(i,n+1)rep(j,256)dp[i][j]=inf;
    dp[0][128]=0;
    REP(i,1,n){
        rep(j,256){
            if(dp[i-1][j]==inf)continue;
            rep(k,m){
                int t=min(255,max(0,j+c[k]));
                dp[i][t]=min(dp[i][t],dp[i-1][j]+(x[i-1]-t)*(x[i-1]-t));
            }
        }
    }
    int ans=inf;
    rep(i,256)ans=min(ans,dp[n][i]);
    cout<<ans<<endl;
    run();
}

C++: #include <cstdio>
#include <cstring>
#include <cassert>
#include <functional>
#include <algorithm>
#include <iostream>
#include <iomanip>
#include <vector>
#include <map>
#include <iterator>
#include <queue>
#include <utility>

using namespace std;

int N, M;
#define MN 20000
#define MM 16
int C[MN];
int X[MM];
bool input() {
    cin >> N >> M;
    if (N == 0 && M == 0) return false;
    for (int i = 0; i < M; i++) cin >> X[i];
    for (int i = 0; i < N; i++) cin >> C[i];
    return true;
}

#define INF (1<<28)
#define SQ(x) ((x) * (x))

int F(int x) {
    if (x < 0) return 0;
    if (x > 255) return 255;
    return x;
}

int Cache[MN][256];
int dfs(int index, int x) {
    if (index == N) return 0;
    if (Cache[index][x] >= 0) return Cache[index][x];
    int Ret = INF;
    for (int i = 0; i < M; i++) {
        Ret = min(Ret, dfs(index + 1, F(x + X[i])) + SQ(C[index] - x));
    }
    return Cache[index][x] = Ret;
}

void solve() {
    memset(Cache, -1, sizeof(Cache));
    int Ans = INF;
    for (int i = 0; i < M; i++) {
        Ans = min(Ans, dfs(0, F(128 + X[i])));
    }
    cout << Ans << endl;
}

int main() {
    while (input()) solve();
    return 0;
}