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 <queue>
using namespace std; typedef long long ll; int an=0; const ll INF = 1e18; const ll MOD = 1e9+7;
typedef pair<int,int> P; const int xx[4]={1,-1,0,0}, yy[4]={0,0,1,-1};
int a[90][90]; int n,m; struct tlp {int x,y,cs;};

int main(){
  while(true) {
    int n,m; cin>>n>>m; if (n==0) break;
    ll c[m], x[n];
    for(int i=0;i<m;i++) cin>>c[i];
    for(int i=0;i<n;i++) cin>>x[i];
    ll p[2][256]; fill(p[0],p[2],INF); p[0][128]=0;
    for(int i=0;i<n;i++) {
      int bb=i%2, aa=(i+1)%2;
      for(int j=0;j<256;j++) p[aa][j]=INF;
      for(ll j=0;j<256;j++) {
        if (p[bb][j] == INF) continue;
        for(int k=0;k<m;k++) {
          int pc = max(0LL,min(255LL,j+c[k])); //cout<<pc<<endl;
          p[aa][pc] = min(p[aa][pc], p[bb][j]);
        }
      }
      /*
      for(int j=0;j<256;j++) {
        if (p[aa][j]!=INF) cout<<"("<<j<<":"<<p[aa][j]<<") "<<endl;
      }*/
      for(ll j=0;j<256;j++) {
        if (p[aa][j] != INF) p[aa][j] += (x[i]-j) * (x[i]-j);
      }
    }
    ll an=INF;
    for(int j=0;j<256;j++) {
      //if (p[n%2][j]!=INF) cout<<"("<<j<<":"<<p[n%2][j]<<") "<<endl;
      an = min(p[n%2][j],an);
    }
    cout<<an<<endl;
  }
}

C++: #include<iostream>
#include<cstdio>
#include<math.h>
#include<string>
#include<algorithm>
#include<functional>
#include<vector>
#include<set>
#include<map>
#define INF 999999999
#define EPS 1.0e-6

using namespace std;

typedef long long ll;
typedef pair<int, int> P;
typedef pair<int, P> PP;

int n, m;
int c[20];
ll x[21000];
ll d[21000][260];

int main()
{
	while (cin >> n >> m) {
		if (n == 0 && m == 0)return 0;
		for (int i = 0; i < m; i++) {
			cin >> c[i];
		}

		for (int i = 1; i <= n; i++) {
			cin >> x[i];
		}
		for (int i = 0; i <= n; i++) {
			for (int j = 0; j <= 256; j++) {
				d[i][j] = INF;
			}
		}
		d[0][128] = 0;

		for (int i = 0; i < n ; i++) {
			for (int j = 0; j <= 255; j++) {
				if (d[i][j] != INF) {
					for (int k = 0; k < m; k++) {
						int l = j + c[k];
						//cout << l << endl;
						if (l < 0)d[i + 1][0] = min(d[i + 1][0], x[i + 1] * x[i + 1]);
						else if (l > 255) {
							d[i + 1][255] = min(d[i + 1][256], d[i][j]+(x[i + 1] - 255)*(x[i + 1] - 255));
							//cout << d[i + 1][256] << endl;
						}
						else{
							d[i+1][l]= min(d[i + 1][l], d[i][j] + (x[i + 1] - l)*(x[i + 1] - l));
							//cout << i + 1 << " " << j << " " << l << " " << d[i + 1][l];
						}
						//cout << i + 1 << " " << j << " " << l << " " << d[i + 1][l]<<endl;
						
					}
				}
			}
		/*
			for (int j = 0; j <= 256; j++) {
				cout << i<< "j:" <<j<<" "<<d[i][j] << endl;

			}
			cout << endl;
			*/
			
		}
		ll ans = INF;
		for (int i = 0; i <= 255; i++) {
			//cout <<"d[n-1][i]:"<< d[n][i] << endl;
			ans = min(ans, d[n][i]);
		}
		cout << ans << endl;
	}

	return 0;
}

C++: #include<bits/stdc++.h>
#define INF 0x3f3f3f3f
#define rep(i,n)for(int i=0;i<(n);i++)
using namespace std;

int c[16], x[20001];
int dp[20001][256];
int d(int a) {
	if (a < 0)return 0;
	if (a > 255)return 255;
	return a;
}
int diff(int i, int a) {
	return (x[i] - a)*(x[i] - a);
}
int main() {
	int n, m;
	while (scanf("%d%d", &n, &m), n) {
		rep(i, m)scanf("%d", &c[i]);
		rep(i, n)scanf("%d", &x[i]);
		memset(dp, 0x3f, sizeof(dp));
		dp[0][128] = 0;
		rep(i, n)rep(j, 256) {
			if (dp[i][j] == INF)continue;
			rep(k, m) {
				int a = d(j + c[k]);
				dp[i + 1][a] = min(dp[i + 1][a], dp[i][j] + diff(i, a));
			}
		}
		int Min = INF;
		rep(j, 256)Min = min(Min, dp[n][j]);
		printf("%d\n", Min);
	}
}

C++: #include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <sstream>
#include <string>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <utility>
#include <cctype>
#include <numeric>
using namespace std;

#define rep(i,n) for(int (i)=0; (i)<(int)(n); ++(i))
#define foreach(c,i) for(__typeof((c).begin()) i=(c).begin();i!=(c).end();i++)

int N, M, C[30], x[20010];
int dp[2][300];
const int kInf = 1 << 28;

void init() {
  rep(i,M) scanf("%d", C+i);
  rep(i,N) scanf("%d", x+i);
}

inline int pow(int v) {
  return v * v;
}

void run() {
  rep(i,2) fill(dp[i], dp[i] + 300, kInf);
  dp[0][128] = 0;
  int *prv = dp[0], *nxt = dp[1];
  
  rep(i,N) {
    fill(nxt, nxt + 300, kInf);
    rep(j,256) {
      rep(k,M) {
        int cur = j, next = j + C[k];
        if (prv[cur] == kInf) continue;
        if (next < 0) next = 0;
        if (next > 255) next = 255;
        int diff = pow(next - x[i]);
        // printf("cur %d : C %d : next %d : diff %d : nxt[next] %d : prv[cur]+diff %d\n",
        //        cur, C[k], next, diff, nxt[next], prv[cur]+diff);
        nxt[next] = min(nxt[next], prv[cur] + diff);
        // if (nxt[next] > prv[cur] + diff) {
        //   printf("cur %d : C %d : next %d : diff : %d\n", cur, C[k], next, diff);
        // }
      }
    }
    swap(prv, nxt);
  }
  printf("%d\n", *min_element(prv, prv + 256));
}

int main() {
  while (scanf("%d%d", &N, &M), N | M) {
    init();
    run();
  }
  return 0;
}