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> pii;
typedef pair<ll, ll> pll;
const int INF = 1e9;
const ll LINF = 1e18;
template<class S,class T> ostream& operator << (ostream& out,const pair<S,T>& o){ out << "(" << o.first << "," << o.second << ")"; return out; }
template<class T> ostream& operator << (ostream& out,const vector<T> V){ for(int i = 0; i < V.size(); i++){ out << V[i]; if(i!=V.size()-1) out << " ";} return out; }
template<class T> ostream& operator << (ostream& out,const vector<vector<T> > Mat){ for(int i = 0; i < Mat.size(); i++) { if(i != 0) out << endl; out << Mat[i];} return out; }
template<class S,class T> ostream& operator << (ostream& out,const map<S,T> mp){ out << "{ "; for(auto it = mp.begin(); it != mp.end(); it++){ out << it->first << ":" << it->second; if(mp.size()-1 != distance(mp.begin(),it)) out << ", "; } out << " }"; return out; }

/*
 <url:http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=2199&lang=jp>
 問題文============================================================
 =================================================================
 解説=============================================================
 ================================================================
 */

int N,M;
ll solve(){
    vector<ll> C(M),x(N);
    for(auto& in:C) cin >> in;
    for(auto& in:x) cin >> in;
    vector<vector<ll>> dp(N+1,vector<ll>(256,LINF));
    dp[0][128] = 0;
    for(int i = 0; i < N;i++){
        for(int j = 0; j < 256;j++){
            if(dp[i][j] == LINF) continue;
            for(int k = 0; k < M;k++){
                ll y = j + C[k];
                y = max(0LL,y); y = min(255LL,y);
                dp[i+1][y] = min(dp[i+1][y],dp[i][j] + (ll)pow(y-x[i],2));
            }
        }
    }
    return *min_element(dp[N].begin(),dp[N].end());
}
int main(void) {
    cin.tie(0); ios_base::sync_with_stdio(false);
    while(cin >> N >> M,N|M){
        cout << solve() << endl;
    }
    return 0;
}

C++: #include <cstdio>
#include <iostream>
#include <sstream>
#include <iomanip>
#include <algorithm>
#include <cmath>
#include <string>
#include <vector>
#include <list>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <bitset>
#include <numeric>
#include <climits>
#include <cfloat>
using namespace std;

const int INF = INT_MAX / 4 * 3;

int main()
{
    for(;;){
        int n, m;
        cin >> n >> m;
        if(n == 0)
            return 0;

        vector<int> c(m), x(n);
        for(int i=0; i<m; ++i)
            cin >> c[i];
        for(int i=0; i<n; ++i)
            cin >> x[i];

        vector<int> dp(256, INF);
        dp[128] = 0;
        for(int i=0; i<n; ++i){
            vector<int> next(256, INF);
            for(int j=0; j<256; ++j){
                for(int k=0; k<m; ++k){
                    int a = max(0, min(255, j + c[k]));
                    next[a] = min(next[a], dp[j] + (x[i] - a) * (x[i] - a));
                }
            }
            dp.swap(next);
        }

        cout << *min_element(dp.begin(), dp.end()) << endl;
    }
}

C++: #include <iostream>
#include <vector>
#include <algorithm>
#define INF (1<<30)
using namespace std;

int pow(int a){
    return a*a;
}

int main(){
    int N,M;
    vector< vector<int> > dp(1<<8,vector<int>(20001,INF));
    vector<int> C(1<<4,0),X(20001,0);
    cin >> N >> M;
    while(N!=0 && M!=0){
        dp.at(128).at(0)=0;
        for(int i=0;i<M;i++) cin >> C.at(i);
        for(int i=1;i<=N;i++) cin >> X.at(i);
        for(int j=0;j<N;j++){
            for(int l=0;l<M;l++){
                int k=C.at(l);
                for(int i=0;i<256;i++){
                    int next=min(i+k,255);
                    next=max(next,0);
                    int cur = dp.at(next).at(j+1);
                    dp.at(next).at(j+1)
                    =min(cur,dp.at(i).at(j)+pow(X.at(j+1)-next));
                    //cout << i << " " << next<< " " << X.at(j+1) << endl;
                }
            }
        }
        int ans=INF;
        for(int i=0;i<256;i++){
            ans=min(ans,dp.at(i).at(N));
        }
        cout << ans << endl;
        /*
        cout << "aaaaaaaaaaaa" << endl;
        for(int j=0;j<=N;j++){
            for(int i=0;i<256;i++){
                cout << dp.at(i).at(j) << " ";
            }
            cout << endl;
        }
        cout << "bbbbbbbbbbbbb" << endl;
        */
        cin >> N >> M;
        for(int i=0;i<M;i++) C.at(i)=0;
        for(int i=1;i<=N;i++) X.at(i)=0;
        for(int i=0;i<256;i++){
            for(int j=0;j<=N;j++){
                dp.at(i).at(j)=INF;
            }
        }
    }
}