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

using namespace std;

const long long INF = 1e15;
const long long Y = 128;
int main(){
	long long n,m;
	while(cin>>n>>m,n){
		vector<long long> cod(m),signal(n);
		for(int i=0;i<m;i++){//code input
			cin >> cod[i];
		}
		for(int i=0;i<n;i++){//signal input
			cin >> signal[i];
		}
		vector<vector<long long>> dp(n+1,vector<long long>(256,INF));
		for(int i=0;i<m;i++){
			int next_j = Y + cod[i];
			if(next_j>255)next_j = 255;
			else if(next_j<0)next_j = 0;
			dp[0][next_j] = (signal[0] - (next_j))*(signal[0] - (next_j));
		}
		for(int i=0;i<n-1;i++){
			for(int j=0;j<256;j++){
				if(dp[i][j] == INF)continue;
				for(int k=0;k<m;k++){
					int next_j = j + cod[k];
					if(next_j>255)next_j = 255;
					else if(next_j<0)next_j = 0;
					int zip_square = (signal[i+1] - next_j)*(signal[i+1] - next_j);
					dp[i+1][next_j] = min(dp[i+1][next_j],dp[i][j] + zip_square);
				}
			}
		}
		long long ans = INF;
		for(int i = 0;i<256;i++)
			ans = min(ans,dp[n-1][i]);
		cout << ans << endl;
	}
	return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> P;
typedef vector<int> VI;
 
template<class T> bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T> bool chmin(T &a, const T &b) { if (a>b) { a=b; return 1; } return 0; }
#define _overload3(_1,_2,_3,name,...) name
#define _rep(i,n) repi(i,0,n)
#define repi(i,a,b) for(int i=int(a);i<int(b);++i)
#define rep(...) _overload3(__VA_ARGS__,repi,_rep,)(__VA_ARGS__)
#define all(x) (x).begin(),(x).end()
const int mod=1e9+7;

int round255(int x){
    if(x>255)return 255;
    if(x<0)return 0;
    return x;
}

int main(){
    while(true){
        int n,m;cin>>n>>m;if(!n)break;
        int c[m];rep(i,m)cin>>c[i];
        int x[n];rep(i,n)cin>>x[i];
        int dp[n+1][256];
        rep(i,n+1) rep(j,256) dp[i][j]=mod;
        dp[0][128]=0;
        rep(i,n){
            rep(j,256){
                if(dp[i][j]==mod)continue;
                rep(k,m){
                    chmin(dp[i+1][round255(j+c[k])],dp[i][j]+abs(x[i]-round255(j+c[k]))*abs(x[i]-round255(j+c[k])));
                }
            }
        }
        int ans=mod;
        rep(i,256)chmin(ans,dp[n][i]);
        cout<<ans<<endl;
    }
}

C++: #include<string>
#include <vector>
#include <stack>
#include <map>
#include<iostream>
#include <algorithm>
#include <limits.h> 
#include <functional>

using namespace std;
int n;
int m;
int a;

int main(){
	while(true){
		cin >>n;
		if(n==0){
			break;
		}
		cin >>m;
		if(m==0){
			break;
		}

		vector<int> codebook;
		vector<int> data;
		for(int i=0;i<m;i++){
			cin >> a;
			codebook.push_back(a);
		}
		for(int i=0;i<n;i++){
			cin >> a;
			data.push_back(a);
		}
		//一個前のハッシュ値による最小誤差でハッシュ。
		
		//map<int,int> hash;
		int hash[256];
		for(int i=0;i<256;i++){
			hash[i]=INT_MAX;
		}
		hash[128]=0;

		int _mi;int _mindex=-1;
		for(int i=0;i<n;i++){
			int d=data[i];//次のデータ		
			int next_hash[256];
			for(int j=0;j<256;j++){
				next_hash[j]=INT_MAX;
			}
			//最小値とdの間のみ保存。
			for(int j=0;j<256;j++)
			{
				for(int k=0;k<m;k++){
					if(hash[j]!=INT_MAX){
						int f=max(min(j+codebook[k],255),0);
						int div=f-d;
						int s=hash[j]+div*div;
						next_hash[f]=min(next_hash[f],s);
					}
				}
			}
			for(int j=0;j<256;j++){
				hash[j]=next_hash[j];
			}
		}
		int mi=INT_MAX;
		for(int i=0;i<256;i++){
			mi=min(hash[i],mi);
		}
		cout << mi <<endl;
	}
}

C++: #include <bits/stdc++.h>

using namespace std;

#define INF (1 << 25)

int dp[20001][256];


int main()
{
  int N, M;
  while(cin >> N >> M, N || M){
    int C[16], x[20000];
    for(int i = 0; i < M; i++) cin >> C[i];
    for(int i = 0; i < N; i++) cin >> x[i];
    for(int i = 0; i < 20001; i++) for(int j = 0; j < 256; j++) dp[i][j] = INF;
    dp[0][128] = 0;
    for(int i = 0; i < N; i++){
      for(int j = 0; j < 256; j++){
	if(dp[i][j] == INF) continue;
	for(int k = 0; k < M; k++){
	  int y = j + C[k];
	  if(y < 0) y = 0;
	  else if(y > 255) y = 255;
	  int sqsum = (x[i] - y)*(x[i] - y);
	  dp[i+1][y] = min(dp[i+1][y], dp[i][j] + sqsum);	  
	}
      }
    }
    cout << *min_element(dp[N], dp[N] + 256) << endl;
  }
  return 0;
}

C++: #include <iostream>
#include <algorithm>
#include <queue>
#include <tuple>
#include <limits>

using namespace std;

int main()
{
	for(int n,m;cin>>n>>m,n|m;){
		int cs[m],xs[n];
		for(int& c:cs) cin>>c;
		for(int& x:xs) cin>>x;
		
		queue<tuple<int,int,int>> q; // (index,value,sqrsum)
		q.emplace(0,128,0);
		
		int dp[n+1][256];
		for(int i=0;i<=n;i++)
			fill(dp[i],dp[i]+256,numeric_limits<int>::max());
		
		while(!q.empty()){
			auto cur=q.front(); q.pop();
			int ci=get<0>(cur),cval=get<1>(cur),csum=get<2>(cur);
			
			if(dp[ci][cval]<=csum)
				continue;
			dp[ci][cval]=csum;
			
			if(ci==n)
				continue;
			
			for(int c:cs){
				int nval=max(min(cval+c,255),0);
				int nsum=csum+(nval-xs[ci])*(nval-xs[ci]);
				q.emplace(ci+1,nval,nsum);
			}
		}
		
		cout<<*min_element(dp[n],dp[n]+256)<<endl;
	}
	
	return 0;
}