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;

// region template
#define rep(i, a, b) for (int i = (a); i < (b); ++i)
#define trav(a, x) for (auto &a : x)
#define all(x) begin(x), end(x)
#define sz(x) (int)(x).size()
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef vector<int> vi;

ll gcd(ll a, ll b) { return __gcd(a, b); }

ll euclid(ll a, ll b, ll &x, ll &y) {
    if (b) { ll d = euclid(b, a % b, y, x);
        return y -= a/b * x, d; }
    return x = 1, y = 0, a;
}

typedef unsigned long long ull;
typedef long double ld;
ull mod_mul(ull a, ull b, ull M) {
    ll ret = a * b - M * ull(ld(a) * ld(b) / ld(M));
    return ret + M * (ret < 0) - M * (ret >= (ll)M);
}
ull mod_pow(ull b, ull e, ull mod) {
    ull ans = 1;
    for (; e; b = mod_mul(b, b, mod), e /= 2)
        if (e & 1) ans = mod_mul(ans, b, mod);
    return ans;
}

const ll mod = 1000000007;
struct Mod {
    ll x;
    Mod(ll xx) : x(xx) {}
    Mod operator+(Mod b) { return Mod((x + b.x) % mod); }
    Mod operator-(Mod b) { return Mod((x - b.x) % mod); }
    Mod operator*(Mod b) { return Mod((x * b.x) % mod); }
    Mod operator/(Mod b) { return *this * invert(b); }
    Mod invert(Mod a) {
        ll x, y, g = euclid(a.x, mod, x, y);
        assert(g == 1); return Mod((x + mod) % mod);
    }
    Mod operator^(ll e) {
        if (!e) return Mod(1);
        Mod r = *this ^ (e / 2); r = r * r;
        return e & 1 ? *this * r : r;
    }
};
// endregion

ll dp[20005][256];

ll sqr(ll x) { return x * x; }

int main() {
    rep(i, 0, 256) dp[0][i] = 0x3f3f3f3f3f3f3f3fll;
    dp[0][128] = 0;
    for (;;) {
        int n, m; cin >> n >> m; if (!n) break;
        // reset dp matrix
        rep(i, 1, n + 1) rep(j, 0, 256) dp[i][j] = 0x3f3f3f3f3f3f3f3fll;
        vector<int> sig(n + 1);
        vector<int> diff(m);
        rep(i, 0, m) cin >> diff[i];
        rep(i, 1, n + 1) cin >> sig[i];
        rep(i, 1, n + 1) {
            rep(j, 0, m) {
                rep(k, 0, 256) {
                    int nv = k + diff[j];
                    if (nv < 0) nv = 0;
                    if (nv > 255) nv = 255;
                    dp[i][nv] = min(dp[i][nv], dp[i - 1][k] + sqr(nv - sig[i]));
                }
            }
        }
        ll ans = 0x3f3f3f3f3f3f3f3fll;
        rep(i, 0, 256) ans = min(ans, dp[n][i]);
        cout << ans << endl;
    }
}

C++: #include <iostream>
#include <sstream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <ctime>
#include <vector>
#include <string>
#include <map>
#include <set>
#include <deque>
#include <queue>
#include <stack>
#include <functional>
#include <algorithm>

using namespace std;

#define rep(i,j) REP((i), 0, (j))
#define REP(i,j,k) for(int i=(j);(i)<(k);++i)
#define BW(a,x,b) ((a)<=(x)&&(x)<=(b))
#define ALL(v) (v).begin(), (v).end()
#define LENGTHOF(x) (sizeof(x) / sizeof(*(x)))
#define AFILL(a, b) fill((int*)a, (int*)(a + LENGTHOF(a)), b)
#define SQ(x) ((x)*(x))
#define Mod(x, mod) (((x)+(mod)%(mod))
#define MP make_pair
#define PB push_back
#define Fi first
#define Se second
#define INF (1<<29)
#define EPS 1e-10
#define MOD 1000000007

typedef pair<int, int> pi;
typedef pair<int, pi> pii;
typedef vector<int> vi;
typedef queue<int> qi;
typedef long long ll;

int N,M,C[16],x[20010];
int dp[20010][256];

void solve(){
  rep(i,20010) fill(dp[i], dp[i]+256, INF);
  dp[0][128] = 0;

  rep(i,N){
    rep(j,256){
      if(dp[i][j]==INF) continue;
      rep(k,M){
	int next = max(0, min(j+C[k], 255));
	dp[i+1][next] = min(dp[i+1][next], dp[i][j] + SQ(x[i]-next));
      }
    }
  }
  int res = INF;
  rep(i,256) res = min(res, dp[N][i]);
  printf("%d\n", res);
}

int main(){
  while(scanf("%d%d", &N,&M) && N||M){
    rep(i,M) scanf("%d",&C[i]);
    rep(i,N) scanf("%d",&x[i]);
    
    solve();		   
  }
  return 0;
}

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

#define N 20001
#define M 17
#define V 256

using namespace std;

const int infty = 1<<29;

int correct(int i){
  return min( max( i, 0 ), V-1 );
}

int solve(int n, int m, int codebook[], int decode[]){
  int ret = infty;
  int **dp = new int*[N+1];
  for(int i = 0; i < N+1; ++i){
    dp[i] = new int[V];
  }
  
  for(int i = 0; i < N + 1; ++i){
    for(int j = 0; j < V; ++j){
      dp[i][j] = infty;
    }
  }
  
  dp[0][128] = 0;
  for(int i = 1; i <= n; ++i){
    for(int j = 0; j < V; ++j){
      if(dp[i-1][j]<infty){
	for(int k = 0; k < m; ++k){
	  int nextj = correct( j+codebook[k] );
	  //cout << " I : " << i << " NEXT J " << nextj << endl;
	  dp[i][nextj] = min( dp[i][nextj], dp[i-1][j] + ( decode[i-1] - nextj ) * ( decode[i-1] - nextj ) );
	  //cout << " DP : " << dp[i][nextj] << endl;
	}
      }
    }
  }
  for(int i = 0; i < V; ++i){
    ret = min( ret, dp[n][i] );
  }
  for(int i = 0; i < N+1; ++i){
    delete [] dp[i];
  }
  delete [] dp;
  return ret;
}

int main(){
  while(true){
    int n,m;
    int codebook[M];
    int decode[N];
    cin >> n >> m;
    if( n == 0 && m == 0 ) break;
    for(int i = 0; i < m; ++i){
      cin >> codebook[i];
    }
    for(int i = 0; i < n; ++i){
      cin >> decode[i];
    }
    cout << solve(n,m,codebook,decode) << endl;
  }
  return 0;
}