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>
using namespace std;
#define rep(i, n) for ( int i = 0; i < n; i++ )
#define NMAX 20001
#define MMAX 16
#define INFTY (1<<30)
int N, M, C[MMAX], X[NMAX];
unsigned long long T[256][NMAX];

unsigned long long compute(){
    rep(i, 256) rep(j, N+1) T[i][j] = INFTY;
    T[128][0] = 0;
    rep(j, N){
	rep(i,256){
	    if ( T[i][j] == INFTY ) continue;
	    rep(k, M){
		int p = i + C[k];
		if ( p > 255 ) p = 255;
		if ( p < 0 ) p = 0;
		T[p][j+1] = min(T[p][j+1], T[i][j] + (X[j+1]-p)*(X[j+1]-p));
	    }
	}
    }
    unsigned long long minv = INFTY;
    rep(i, 256) minv = min(minv, T[i][N]);
    return minv;
}

main(){
    while(cin >> N >> M && N ){
	rep(i, M) cin >> C[i];
	rep(i, N) cin >> X[i+1];
	cout << compute() << endl;
    }
}

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

using namespace std;

#define MAX_M 16
#define MAX_N 20000
#define INF 1e8

int N, M, C[MAX_M], x[MAX_N];
int dp[MAX_N][256];

int main(){
  while(cin >> N >> M , (N | M)){
    for(int i = 0 ; i < M ; i++){
      cin >> C[i];
    }
 
    for(int i = 0 ; i < N ; i++){
      cin >> x[i];
    }
    int ans = INF;
    fill(dp[0], dp[MAX_N], INF);
    for(int i = 0 ; i <= 255 ; i++){
      dp[0][i] = 0;
    }
    for(int i = 0 ; i < N ; i++){
      for(int j = 0 ; j <= 255 ; j++){
        for(int k = 0 ; k < M ; k++){
          int t = j + C[k];
          if(t < 0) t = 0;
          if(t > 255) t = 255;
          dp[i+1][t] = min(dp[i+1][t], dp[i][j] + (t-x[i])*(t-x[i]));
        }
      }
    }
    for(int i = 0 ; i <= 255 ; i++){
      ans = min(ans, dp[N][i]);
    } 
    cout << ans << endl;
  }

  return 0;
}

C++: //include
//------------------------------------------
#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <ctime>
#include <climits>
#include <queue>

using namespace std;

//typedef
//------------------------------------------
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef long long LL;

//container util
//------------------------------------------
#define ALL(a)  (a).begin(),(a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define MP make_pair
#define SZ(a) int((a).size())
#define EACH(i,c) for(typeof((c).begin()) i=(c).begin(); i!=(c).end(); ++i)
#define EXIST(s,e) ((s).find(e)!=(s).end())
#define SORT(c) sort((c).begin(),(c).end())

//repetition
//------------------------------------------
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n)  FOR(i,0,n)

//constant
//--------------------------------------------
const double EPS = 1e-10;
const double PI  = acos(-1.0);

int main(){
  cin.tie(0);
  ios_base::sync_with_stdio(false);

  int N, M;
  while(cin>>N>>M,N){
	VI C(M); REP(i,M)cin>>C[i];
	int t = 0;
	vector< vector<LL> > calc(2, vector<LL>(256, 1234567890));
	calc[t][128] = 0;
	REP(i,N){
	  int x; cin >> x;
	  fill(ALL(calc[1-t]), 1234567890);
	  REP(j,M){
		REP(b,256){
		  int d = max(0, min(255, b+C[j]));
		  calc[1-t][d] = min(calc[1-t][d], calc[t][b] + (x-d)*(x-d));
		}
	  }
	  t = 1-t;
	}
	LL ans = 1234567890;
	REP(i,256)
	  ans = min(ans, calc[t][i]);
	cout << ans << endl;
  }

  return 0;
}

C++: #include <bits/stdc++.h>
#define PB push_back
#define MP make_pair
#define REP(i,n) for (int i=0;i<(n);i++)
#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define ALL(a) (a).begin(),(a).end()
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> P;
const int INF=2e9;
double EPS=1e-10;
int main(){
      int n,m;
      while(cin>>n>>m&&(n||m)){
            int a[n],b[m];
            ll ans=1e18;
            REP(i,m)cin>>b[i];
            REP(i,n)cin>>a[i];
            REP(i,m){
                  int now=128;
                  ll res=0;
                  REP(j,n){
                        now+=b[i];
                        now=min(255,max(0,now));
                        res+=(a[j]-now)*(a[j]-now);
                  }
                  ans=min(ans,res);
            }
            cout<<ans<<endl;
      }
}