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>

using namespace std;

const int INF = 1e8;


void debug(vector<int> c, vector<int> x){
	for(int i=0;i<c.size();i++) cout<< " "<< c[i]; cout<< endl;
	for(int i=0;i<x.size();i++) cout<< " "<< x[i]; cout<< endl;
}

void debug(vector<vector<int>>& dp){
	cerr<< " -- debug -- "<< endl;
	for(int i=0;i<dp.size();i++){
		for(int j=0;j<dp[i].size();j++){
			if(dp[i][j] != INF) cerr<< "("<< i<< ","<< j<< ")"<< dp[i][j]<< " ";
		}
		cerr<< endl;
	}
}

int norm(int a, int b){
	return (a - b) * (a - b);
}

bool solve(){
	int n, m;
	cin>> n>> m;
	if(!n && !m) return false;

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

	vector<vector<int>> dp(n+1, vector<int>(256, INF));
	dp[0][128] = 0;
	for(int i=0;i<dp.size()-1;i++){
		for(int j=0;j<dp[i].size();j++){
			if(dp[i][j] == INF) continue;
			for(int k=0;k<c.size();k++){
				int jj = j + c[k];
				jj = min(jj, 255); jj = max(jj, 0);
				dp[i+1][jj] = min(dp[i+1][jj], dp[i][j] + norm(x[i], jj));
			}
		}
	}
	//debug(dp);

	int ans = INF;
	for(int i=0;i<dp[n].size();i++) ans = min(ans, dp[n][i]);
	cout<< ans<< endl;
	return true;
}

int main(){
	cout.setf(ios::fixed); cout.precision(10);
	while(solve());
	return 0;
}

C++: #define _USE_MATH_DEFINES
#define INF 0x3f3f3f3f

#include <iostream>
#include <cstdio>
#include <sstream>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <stack>
#include <limits>
#include <map>
#include <string>
#include <cstring>
#include <set>
#include <deque>
#include <bitset>
#include <list>
#include <cctype>
#include <utility>
  
using namespace std;
  
typedef long long ll;
typedef pair <int,int> P;
typedef pair <int,P> PP;
  
static const double EPS = 1e-8;
  
static const int tx[] = {0,1,0,-1};
static const int ty[] = {-1,0,1,0};


int main(){
  int signal_num,code_num;
  while(~scanf("%d %d",&signal_num,&code_num)){
    if(signal_num == 0 && code_num == 0) break;

    int code[16];
    for(int i=0;i<code_num;i++){
      scanf("%d",code+i);
    }
    int input_signal[20000];
    for(int i=0;i<signal_num;i++){
      scanf("%d",input_signal+i);
    }

    int prev[256],next[256];
    memset(prev,0x3f,sizeof(prev));
    memset(next,0x3f,sizeof(next));

    prev[128] = 0;
    for(int step=0;step<signal_num;step++){
      for(int from = 0; from < 256;from++){
	for(int code_idx=0;code_idx<code_num;code_idx++){
	  int to = from + code[code_idx];
	  if(to < 0) to = 0;
	  if(to >= 256) to = 255;

	  next[to] = min(next[to],
			 prev[from] 
			 + (to - input_signal[step]) * (to - input_signal[step]));
	  
	}
      }
      memcpy(prev,next,sizeof(int)*256);
      memset(next,0x3f,sizeof(next));
    }

    int min_v = INF;
    for(int i=0;i<256;i++){
      min_v = min(min_v,prev[i]);
    }
    printf("%d\n",min_v);
  }
}

C++: #include<iostream>
#include<vector>
#include<utility>
#include<cmath>

#define INF (1 << 29)

using namespace std;

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;
        }

        int dp[2][256];
        for(int i = 0; i < 256; i++)
        {
            dp[0][i] = dp[1][i] = INF;
        }

        for(int i = 0; i < M; i++)
        {
            int sub = fix(128 + in_C[i]);
            dp[0][sub] = (in_X[0] - sub) * (in_X[0] - sub);
        }

        for(int n = 1; n < N; n++)
        {
            for(int i = 0; i < 256; i++)
            {
                if(dp[0][i] == INF)continue;
                for(int j = 0; j < M; j++)
                {
                    int sub = fix(i + in_C[j]);
                    int nextScore = dp[0][i] + (in_X[n] - sub) * (in_X[n] - sub);
                    dp[1][sub] = min(dp[1][sub], nextScore);
                    /*
                    cout << sub << endl;
                    cout << dp[0][sub] << endl;
                    cout << pow( in_X[n] - sub, 2.0 ) << endl;
                    cout << endl;
                    //*/
                }
            }
            for(int i = 0; i < 256; i++)
            {
                dp[0][i] = dp[1][i];
                dp[1][i] = INF;
            }
        }

        int ans = INF;
        for(int i = 0; i < 256; i++)
        {
            ans = min(ans, dp[0][i]);
        }
        cout << ans << endl;

    }



    return 0;
}