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<vector>
#include<utility>
#include<cmath>

#define INF (1 << 29)

using namespace std;

struct DP{
    int first, second;
};

int fix(int num)
{
    return min(255, max( num, 0) );
}

int main()
{
    int N, M;
    while(cin >> N >> M, (N))
    {

        vector<int> in_C(M);
        for(auto& _c : in_C)
        {
            cin >> _c;
        }

        vector<int> in_X(N);
        for(auto& _x : in_X)
        {
            cin >> _x;
        }

        DP dp[2][16];
        //*
        for(int i = 0; i < M; i++)
        {
            int score, nextX;
            nextX = fix(128 + in_C[i]);
            score = pow( abs(in_X[0] - nextX), 2.0);

            dp[0][i] = DP{ score, nextX };
            //cout << (int)dp[0][i].first << " " << (int)dp[0][i].second << endl;
        }

        for(int n = 1; n < N; n++)
        {
            for(int i = 0; i < M; i++)
            {
                int nextScore = INF;
                int nextX = 0;
                for(int j = 0; j < M; j++)
                {
                    int tmpScore = (int)dp[0][i].first + pow( fix(abs( (int)dp[0][i].second + in_C[j] - in_X[n] )), 2.0 );
                    /*
                    cout << (int)dp[0][i].first << endl;
                    cout << (int)dp[0][i].second << endl;
                    cout << in_C[j] << endl;
                    cout << in_X[n] << endl;
                    cout << (int)dp[0][i].second + in_C[j] - in_X[1] << endl;
                    cout << (fix((int)dp[0][i].second + in_C[j] - in_X[1])) << endl;
                    cout << tmpScore << endl;
                    cout << endl;
                    //*/
                    if((int)tmpScore < (int)nextScore)
                    {
                        nextScore = tmpScore;
                        nextX = fix((int)dp[0][i].second + in_C[j]);
                    }
                }
                dp[1][i] = DP{ (int)nextScore, nextX };
            }
            for(int i = 0; i < M; i++)
                dp[0][i] = dp[1][i];
        }

        int ans = INF;
        for(int i = 0; i < M; i++)
        {
            if( dp[0][i].first < ans) ans = dp[0][i].first;
        }
        cout << ans << endl;
        //*/


    }



    return 0;
}

C++: #include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <memory>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <typeinfo>
#include <utility>
#include <valarray>
#include <vector>
#include <climits>

#define rep(i, m, n) for(int i=int(m);i<int(n);i++)
//#define EACH(i, c) for (auto &(i): c)
#define all(c) begin(c),end(c)
//#define EXIST(s, e) ((s).find(e)!=(s).end())
//#define SORT(c) sort(begin(c),end(c))
//#define pb emplace_back
//#define MP make_pair
//#define SZ(a) int((a).size())

//#define LOCAL 0
//#ifdef LOCAL
//#define DEBUG(s) cout << (s) << endl
//#define dump(x)  cerr << #x << " = " << (x) << endl
//#define BR cout << endl;
//#else
//#define DEBUG(s) do{}while(0)
//#define dump(x) do{}while(0)
//#define BR
//#endif


//改造
typedef long long int ll;
using namespace std;
#define INF (1 << 30) - 1
#define INFl (ll)5e15
#define DEBUG 0 //デバッグする時1にしてね
#define dump(x)  cerr << #x << " = " << (x) << endl
#define MOD 1000000007
//ここから編集する


bool solve() {
    int N, M;
    cin >> N >> M;
    if (N == 0 && M == 0) {
        return false;
    }

    vector<ll> C(M), x(N + 1);

    rep(i, 0, M) cin >> C[i];
    rep(i, 1, N + 1) cin >> x[i];

    vector<vector<ll> > dp(N + 1, vector<ll>(256, INFl));
    dp[0][128] = 0;

    rep(i, 0, N) {
//            ll bef = dp[i + 1][j];
        rep(j, 0, M) {
            rep(k, 0, 256) {
//                dp[i + 1][k] = min(dp[i + 1][k], dp[i][k] + (ll) round(pow(x[i + 1] - k, 2)));
                ll nexk = max(0LL, min(k + C[j], 255LL));
                ll next = dp[i][k] + (ll) round(pow((nexk - x[i + 1]), 2));
                dp[i + 1][nexk] = min(dp[i + 1][nexk], next);
            }
        }
    }

    ll ans = INFl;
    rep(i, 0, 256) {
        ans = min(ans, dp[N][i]);
    }

    cout << ans << endl;


    return true;
}

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);

    while (solve()) {

    }


    return 0;
}

C++: #include<iostream>
#include<algorithm>
#define INF 100000000
using namespace std;
int main(){
	int n,m,dp[20][260],cb[20],input[20001];//now,now
	int temp;
	while(cin>>n>>m,n||m){
		
		for(int i=0;i<m;i++)
			cin>>cb[i];
		for(int i=0;i<n;i++){
			cin>>input[i];
		}
		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;
}