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>
#include <stack>
#include <cstring>
#include <queue>

#define rep(i,n) for(int i = 0; i < (n); i++)
#define all(v) (v).begin(), (v).end()
#define MP make_pair
using namespace std;

typedef vector<int> vi;
typedef pair<int, int> P;
typedef long long ll;

int dp[20000][256];

int main(){
	//freopen("test.txt", "r", stdin);
	int n, m;
	while(cin >> n >> m, n|m){
		vector<int> C(m);
		rep(i, m) cin >> C[i];
		vector<int> x(n);
		rep(i, n) cin >> x[i];

		rep(i, 20000) rep(j, 256) dp[i][j] = 2<<29;

		rep(i, m){
			int val = 128+C[i];
			if(val < 0) val = 0;
			if(val > 255) val = 255;
			dp[0][val] = (x[0]-val)*(x[0]-val);
		}
		rep(i, n-1){
			rep(j, 256){
				if(dp[i][j] == -1) continue;
				rep(k, m){
					int val = j+C[k];
					if(val < 0) val = 0;
					if(val > 255) val = 255;
					int d = (x[i+1]-val)*(x[i+1]-val);
					dp[i+1][val] = min(dp[i+1][val], dp[i][j]+d);
				}
			}
		}
		int ans = 2<<29;
		rep(i, 256){
			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> pii;
#define rep(i,n) for(ll i=0;i<(ll)(n);i++)
#define all(a)  (a).begin(),(a).end()
#define pb push_back
#define INF 1LL<<50


int main(){
    int n,m;
    while(cin>>n>>m){
        if(n==0&&m==0)break;
        vector<int> c,x;
        rep(i,m){
            int a;
            cin>>a;
            c.pb(a);
        }
        rep(i,n){
            int a;
            cin>>a;
            x.pb(a);
        }
        
        
        static ll dp[20100][260];
        rep(i,20100)rep(j,260)dp[i][j]=INF;
        dp[0][128]=0;
        
        for(int i=0;i<n;i++){
            rep(j,256){
                rep(k,m){
                    int val = j+c[k];
                    if(val>255)val=255;
                    if(val<0)val=0;
                    int pow2 = (x[i]-val)*(x[i]-val);
                    dp[i+1][val] = min(dp[i+1][val] , dp[i][j]+pow2);
//                    if(j-c[k]>=0 && j-c[k]<=255)dp[i][j] = min(dp[i][j],dp[i-1][j-c[k]]+pow2);
                }
            }
        }
        ll mini=INF;
        rep(i,256){
            mini = min(mini,dp[n][i]);
        }
        cout<<mini<<endl;
        
    }
}

C++: #include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef pair<ll,ll> mp;
ll inf =  1e9;


int main(){
 while(1){
  ll n,m;
  cin>>n>>m;
  if(n == 0 ) break;
  vector<ll> c(m),x(n);
  for(ll i=0;i<m;i++)cin>>c[i];
  for(ll i=0;i<n;i++)cin>>x[i];
  vector<vector<ll> > dp(n+1,vector<ll>(256,inf) );
  dp[0][128] = 0;
 //cout<<"OK"<<endl;
  for(ll i=0;i<n;i++){
   for(ll j=0;j<256;j++){
    if( dp[i][j] == inf ) continue;
    for(ll k=0;k<m;k++){
     //if( 0<= j+c[k] && j + c[k] < 256 ){
      ll next = min(max(0ll,j+c[k]),255ll);
      dp[i+1][next] = min( dp[i+1][next] , dp[i][j] + ( next - x[i] )*( next - x[i] ) );
     //}
 /*    if( j - c[k] >= 0 ){
      dp[i+1][j-c[k]] = min( dp[i+1][j-c[k]] , dp[i][j] + ( j-c[k] - x[i] )*( j-c[k] - x[i] ) );
     }*/
    }

   }
  }
  ll res = inf;
  for(ll i=0;i<256;i++) res = min( res , dp[n][i] );
  cout<<res<<endl;

 }
    return 0;
}

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

#define IF 3040401
#define lengthof(x) (sizeof(x) / sizeof(*(x)))

using namespace std;

int main(int argc, char const *argv[])
{
	int n,m;
	int dp[1<<8][2];
	int min_;

	while(1){
		cin>>n>>m;
		if(n+m==0) break;

		vector<int> cb(m);
		vector<int> x(n);
		for(int i1=0;i1<m;i1++){
			cin>>cb[i1];
		}

		for(int i1=0;i1<n;i1++){
			cin>>x[i1];
		}

		fill((int*)dp,(int *)(dp+lengthof(dp)),IF);

		dp[128][0]=0;
		for(int i1=0;i1<n;i1++){
			for(int i2=0;i2<(1<<8);i2++){
				if(dp[i2][i1%2]!=IF){

					for(int i3=0;i3<m;i3++){
						int temp=i2+cb[i3];
						if(temp<0) temp=0;
						if(temp>255) temp=255;
						dp[temp][(i1+1)%2]=min(dp[temp][(i1+1)%2],dp[i2][i1%2]+(temp-x[i1])*(temp-x[i1]));
					}
					dp[i2][i1%2]=IF;
				}

			}
		}

		min_=IF;
		for(int i1=0;i1<(1<<8);i1++){
			min_=min(min_,dp[i1][n%2]);
		}

		cout<<min_<<endl;
	}
	return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;
#define r(i,n) for(int i=0;i<n;i++)
#define INF 100000000
int dp[20001][256],n,m;
main(){
  while(cin>>n>>m,n){
    int b[m];int a[n+1];
    r(i,m)cin>>b[i];
    r(i,n)cin>>a[i+1];
    r(i,n+1)r(j,256)if(dp[i][j]!=INF)dp[i][j]=INF;
    dp[0][128]=1;
    r(i,n)r(j,256)
        if(dp[i][j]!=INF)r(k,m){
          int p=j+b[k];
          if(p>255)p=255;
          if(p<0)p=0;
          dp[i+1][p]=min(dp[i+1][p],dp[i][j]+(a[i+1]-p)*(a[i+1]-p));
        }
    int p=100000000;
    r(i,256)if(dp[n][i]!=INF)
      p=min(p,dp[n][i]);
    cout<<p-1<<endl;
  }
}