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>

using namespace std;

const int MAX_N = 20000;
const int MAX_M = 16;
const int y0 = 128;
int N, M;
int C[MAX_M + 1], x[MAX_N + 1];
int ans;

void Rec(int index, int sum, int y)
{
    if (index == N) {
        ans = min(ans, sum);
        return ;
    }

    for (int i = 0; i < M; ++i) {
        int y_n = y + C[i];
        if (y_n < 0)
            y_n = 0;
        else if (y_n > 255)
            y_n = 255;

        int sum_n = sum + (x[index] - y_n) * (x[index] - y_n);
        if (sum_n < ans)
            Rec(index + 1, sum_n, y_n);
    }
}

int main()
{
    while (cin >> N >> M, N || M) {
        // Input
        for (int i = 0; i < M; ++i)
            cin >> C[i];
        for (int i = 0; i < N; ++i)
            cin >> x[i];

        int y = 128;
        ans = 0;
        for (int i = 0; i < N; ++i) {
            y += C[M - 1];
            if (y < 0)
                y = 0;
            else if (y > 255)
                y = 255;
            ans += (x[i] - y) * (x[i] - y);
        }

        Rec(0, 0, y0);
        cout << ans << endl;
    }

    return 0;
}

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

#define int long long
#define double long double

#define REP(i, b) for(int i = 0; i < (b); i++)
#define REPS(i, b) for(int i = 1; i <= (b); i++)
#define ALL(v) (v).begin(), (v).end()

#define fi first
#define se second
#define pb push_back
#define mp make_pair

using namespace std;

using pi = pair<int, int>;
using vi = vector<int>;
using vd = vector<double>;
using vs = vector<string>;
using vb = vector<bool>;
using vpi = vector<pi>;
using vvi = vector<vi>;
using vvb = vector<vb>;

const int INF = 1e16;
const int MOD = 1e9+7;

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

signed main()
{
    cin.tie(nullptr);
    ios_base::sync_with_stdio(false);
    cout << fixed << setprecision(10);

    while(true)
    {
        int N, M; cin >> N >> M;
        if(N == 0 && M == 0) break;
        vi C(M); REP(i, M) cin >> C[i];
        vi x(N); REP(i, N) cin >> x[i];
        vvi dp(N+1, vi(256, INF)); // dp[i+1][j] := i番目の複合信号がjのときの最小値
        REP(i, M) dp[0][128] = 0;
        REP(i, N)
        {
            REP(j, 256)
            {
                if(dp[i][j] != INF)
                {
                    REP(k, M)
                    {
                        int t;
                        if(j+C[k] > 255) t = 255;
                        else if(j+C[k] < 0) t = 0;
                        else t = j+C[k];
                        chmin(dp[i+1][t], dp[i][j]+(t-x[i])*(t-x[i]));
                    }
                }
            }
        }
        cout << *min_element(ALL(dp[N])) << endl;
    }
}

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, z;
  	while(1){
		int  temp[20003][256] = {}, temp3[3][256] = {}, count0 = 0, count1 = 0, count2 = 0;
		temp[0][128] = 0;
		for(i = 0; i < 3; i++)for(j = 0; j < 256; j++) temp3[i][j] = -1;
		temp3[0][count0++] = 128;
	    cin>>N>>M;
	if(N == 0 && M ==0)break;
    	for(i = 0; i < M; i++){
			cin>>C[i];
		}
		for(i = 1; i <= N; i++){
			cin>>x;
			for(j = 0; j < 256; j++)temp[i][j] = -1;
			for(j = 0; j < count0; j++){
				for(k = 0; k < M; k++){
					z = foo(C[k] + temp3[0][j]);
					if(temp[i][z] == -1){
						temp[i][z] = temp[i - 1][temp3[0][j]] + (x - z) * (x - z);
						temp3[1][count1] = z;
						count1++;
					} else {
						temp[i][z] = min(temp[i][z], temp[i - 1][temp3[0][j]] + (x - z) * (x - z));
					}
				}
			}
			for(j = 0; j < count1; j++){
				temp3[0][j] = temp3[1][j];
			}
			count0 = count1;
			count1 = 0;
		}
		
		mi = temp[N][temp3[0][0]];
		for(j = 1; j < count0; j++){
			mi = min(temp[N][temp3[0][j]], mi);
		}
		
		cout<<mi<<endl;
			
		
	}
  return 0;
}

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

#define INF_LL (int64)1e18
#define INF (int32)1e9
#define REP(i, n) for(int i = 0;i < (n);i++)
#define FOR(i, a, b) for(int i = (a);i < (b);i++)
#define all(x) x.begin(),x.end()
#define fs first
#define sc second

using int32 = int_fast32_t;
using uint32 = uint_fast32_t;
using int64 = int_fast64_t;
using uint64 = uint_fast64_t;
using PII = pair<int32, int32>;
using PLL = pair<int64, int64>;

const double eps = 1e-6;

template<typename A, typename B>inline void chmin(A &a, B b){if(a > b) a = b;}
template<typename A, typename B>inline void chmax(A &a, B b){if(a < b) a = b;}

const int64 mod = 1e9+7;

int main(void){
	int32 N, M;
	while(cin >> N >> M && N+M){
		vector<int64> C(M), x(N);
		REP(i, M) cin >> C[i];
		REP(i, N) cin >> x[i];
		vector<int64> dp(300, INF_LL);
		dp[128] = 0;
		REP(i, N){
			vector<int64> dp2(300, INF_LL);
			REP(j, 256){
				REP(k, M){
					int64 nxt = min((int64)255, max((int64)0, j+C[k]));
					chmin(dp2[nxt], dp[j]+(nxt-x[i])*(nxt-x[i]));
				}
			}
			swap(dp, dp2);
		}
		cout << *min_element(all(dp)) << endl;
	}
}