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;
using ll = long long;
using P = pair<int, int>;

#define rep(i, n) for (int i = 0; i < n; ++i)
#pragma region Debug
template <typename T>
void view(const std::vector<T> &v)
{
    for (const auto &e : v)
    {
        std::cout << e << " ";
    }
    std::cout << std::endl;
}
template <typename T>
void view(const std::vector<std::vector<T>> &vv)
{
    for (const auto &v : vv)
    {
        view(v);
    }
}
#pragma endregion
#pragma region chminmax
template <typename T>
inline bool chmin(T &a, T b)
{
    if (a > b)
    {
        a = b;
        return true;
    }
    return false;
}
template <typename T>
inline bool chmax(T &a, T b)
{
    if (a < b)
    {
        a = b;
        return true;
    }
    return false;
}
#pragma endregion

int main()
{
    int n, m;
    cin >> n >> m;
    do
    {
        vector<int> c(m);
        rep(i, m) cin >> c[i];
        vector<int> x(n);
        rep(i, n) cin >> x[i];

        vector<vector<ll>> dp(n + 1, vector<ll>(256, INT32_MAX));
        dp[0][128] = 0;
        rep(i, n) rep(j, m) rep(k, 256)
        {
            int p = min(255, max(0, k + c[j]));
            ll d = x[i] - p;

            chmin(dp[i + 1][p], dp[i][k] + d * d);
        }

        ll ans = min_element(dp[n].begin(), dp[n].end())[0];

        cout << ans << endl;
        cin >> n >> m;
    } while (n != 0 && m != 0);

    return 0;
}

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

#define NMAX 20000
#define MMAX 16
#define INF 100000000000000
using namespace std;

int N,M;
int cb[MMAX];
int sa[NMAX];
long long dp[NMAX][256];

long long dfs(long long n,long long d,long long k){
	if(dp[d][k]!=-1&&dp[d][k]<=n) return dp[d][k];
	if(d==N) return n;
	long long a=INF;
	int kk;
	for(int i=0;i<M;i++){
		kk=k+cb[i];
		if(kk>255) kk=255;
		if(kk<0) kk=0;
		a=min(a,dfs(n+(kk-sa[d])*(kk-sa[d]),d+1,k+cb[i]));
	}
	if(dp[d][k]!=-1) dp[d][k]=min(a,dp[d][k]);
	else dp[d][k]=a;
	return a;
}

int main(void){

	std::ios_base::sync_with_stdio();

	while(1){

		cin>>N>>M;
		if(N==0&&M==0) break;

		for(int i=0;i<M;i++){
			cin>>cb[i];
		}
		for(int i=0;i<N;i++){
			cin>>sa[i];
		}
		for(int i=0;i<NMAX;i++){
			for(int j=0;j<256;j++){
				dp[i][j]=-1;
			}
		}
		cout<<dfs(0,0,128)<<endl;
	}

	return 0;
}

C++: #include <iostream>
#include <cstdio>
#define INF 1000000000
using namespace std;

int code[18];
int num[20001];
int N,M;
int minn;
int count = 0;
int nm[20001][256];
void saiki(int y,int sum,int kai){
  count++;
  //  cout <<"count=" << count << endl;
  //cout <<"y="<< y <<" sum=" << sum << " kai="<< kai<< endl;

  if(kai == N && y != -1) nm[N][y] = 0;//,printf("N,nm[%d][%d] = %d\n",kai,y,nm[kai][y]);
  
   if(kai < N && sum < minn && nm[kai][y] == INF) {
     for(int i=0;i<M;i++) {
       int yn = y+code[i];
       if(yn < 0) yn = 0;
       if(yn > 255) yn = 255;
      saiki(yn,sum+(num[kai]-yn)*(num[kai]-yn),kai+1);
      if(nm[kai+1][yn] + (num[kai]-yn)*(num[kai]-yn) < nm[kai][y]) nm[kai][y] = nm[kai+1][yn] + (num[kai]-yn)*(num[kai]-yn);
      //  printf("nm[%d][%d] = %d\n",kai,y,nm[kai][y]);
     }

   } else if(kai < N)saiki(-1,nm[kai][y]+sum,N);
   
  
}

int main() {

  while(1) {
    cin >> N >> M;
   
  if(N == 0 && M == 0) break;
  for(int i=0;i<M;i++) cin >> code[i];
  for(int i=0;i<N;i++) cin >> num[i] ;
  
  minn = INF;
  for(int i=0;i<=N;i++)
    for(int j=0;j<=255;j++)
      nm[i][j] = INF;
  
  saiki(128,0,0);

  cout << nm[0][128] << endl;
  
  //cout << minn << endl;
  //cout << count << endl;
  }
  return 0;
  
}