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 <algorithm>
using namespace std;

#define rep(i,x,y) for(int (i)=(x);i<(y);(i)++)
#define INF 1000000

int main()
{
  while(true){
    int N,M;
    cin>>N>>M;
    if(N==0&&M==0){
      break;
    }

    vector<int> C,x;
    int temp;
    rep(i,0,M){
      cin>>temp;
      C.push_back(temp);
    }
    rep(i,0,N){
      cin>>temp;
      x.push_back(temp);
    }

    vector<vector<int>> dp(N+1,vector<int>(256,INF));
    dp[0][128]=0;
    rep(i,0,N){
      rep(j,0,256){
        if(dp[i][j]==INF){
          continue;
        }
        rep(k,0,M){
          int yn=j+C[k];
          yn=min(255,yn);
          yn=max(0,yn);
          int sum=(x[i]-yn)*(x[i]-yn);
          dp[i+1][yn]=min(dp[i+1][yn],dp[i][j]+sum);
        }
      }
    }

    int ans=dp[N][0];
    rep(i,0,256){
      ans=min(ans,dp[N][i]);
    }
    cout<<ans<<endl;
  }

  return 0;
}

C++: #include <stdio.h>
#include <cmath>
#include <algorithm>
#include <stack>
#include <queue>
#include <vector>
typedef long long int ll;

#define BIG_NUM 2000000000

using namespace std;

int N,M;
int** dp;

void func(){
	int codeTable[M];

	for(int i = 0; i < M; i++){
		scanf("%d",&codeTable[i]);
	}

	int num;//初期化
	dp[0][128] = 0;

	for(int count = 1; count <= N; count++){
		scanf("%d",&num);

		for(int i = 0; i <= 255; i++){
			if(dp[count-1][i] != BIG_NUM){
				for(int k = 0; k < M; k++){
                    int now;
                    now = i + codeTable[k];
					if(now > 255){
						dp[count][255] = min(dp[count][255],dp[count-1][i]+(num-255)*(num-255));
					}else if(now < 0){
						dp[count][0]  = min(dp[count][0],dp[count-1][i]+(num)*(num));
					}else{
						dp[count][now] = min(dp[count][now],dp[count-1][i] + (num-now)*(num-now));
					}
				}
			}
		}
	}

	int minimum = BIG_NUM;

	for(int i = 0; i <= 255; i++){
		minimum = min(minimum,dp[N][i]);
	}

	printf("%d\n",minimum);

}

int main(){

	dp = new int*[20001];
	for(int i = 0; i <= 20000; i++){
		dp[i] = new int[256];
	}

	while(true){
		scanf("%d %d",&N,&M);
		if(N == 0 && M == 0)break;

		for(int i = 0; i <= N; i++){
			for(int k = 0; k <= 255; k++)dp[i][k] = BIG_NUM;
		}

		func();
	}

	return 0;
}

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];
	x[0] = 128;
	for(int i=0;i<n;i++) cin>> x[i+1];
	//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+1], 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++: #include <iostream>
#include <vector>
#include <numeric>

using namespace std;

const int INF = 1<<29;

int main(){
  int n, m;
  while(cin >> n >> m && (n|m)){
    vector<int> C(m);
    for(int i=0; i<m; i++) cin >> C[i];
    
    vector<int> dp(256, INF);
    dp[128] = 0;
    while(n--) {
      vector<int> next(256, INF);
      int in; cin >> in;
      for(int i=0; i<=255; i++){
	if(dp[i] < INF) {
	  for(int j=0; j<m; j++){
	    int now = i + C[j];
	    if(now < 0)   { now = 0;   }
	    if(now > 255) { now = 255; }
	    int diff = (in - now) * (in - now);
	    next[now] = min(next[now], dp[i] + diff );
	  }
	}
      }
      dp = next;
    }
    
    cout << *min_element(dp.begin(), dp.end()) << endl;
  }
  return 0;
}

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

Int solve(Int n, Int m){
	vector<Int> a(m), b(n);
	for(auto &i:a) cin >> i;
	for(auto &i:b) cin >> i;
	vector<vector<Int>> dp(n+1, vector<Int>(256, 1e10));
	dp[0][128] = 0;
	for(Int i=0; i<n; ++i){
		for(Int j=0; j<256; ++j){
			for(Int k=0; k<m; ++k){
				Int l = j + a[k];
				if(l < 0) l = 0;
				if(l > 255) l = 255;
				dp[i+1][l] = min(dp[i+1][l], dp[i][j] + (l-b[i])*(l-b[i]));
			}
		}
	}
	Int ans = 1e10;
	for(Int i=0; i<256; ++i) ans = min(ans, dp[n][i]);
	return ans;
}

int main(){
	cin.tie(0);
	ios::sync_with_stdio(false);
	while(1){
		Int n, m; cin >> n >> m;
		if(n == 0) break;
		cout << solve(n, m) << "\n";
	}
}