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>
using namespace std;

int main(){
  int i,j,k,n,m;
  while(cin >> n >> m && n && m){
    vector<int> C,x;
    for(i=0;i<m;i++){
      int a;
      cin >> a;
      C.push_back(a);
    }
    for(i=0;i<n;i++){
      int a;
      cin >> a;
      x.push_back(a);
    }
    sort(C.begin(),C.end());

    long long y[256][2];
    for(i=0;i<256;i++) y[i][0] = y[i][1] = -1;
    y[128][0] = 0;

    for(i=0;i<n;i++){
      for(j=0;j<m;j++){
	for(k=0;k<256;k++){
	  if(y[k][0] != -1){
	    int a = k + C[j];
	    if(a < 0) a = 0;
	    else if(a > 255) a = 255;
	    if(y[a][1] == -1) y[a][1] = y[k][0]+(x[i]-a)*(x[i]-a);
	    else y[a][1] = min(y[a][1],y[k][0]+(x[i]-a)*(x[i]-a));
	  }
	}
      }
      for(j=0;j<256;j++){
	y[j][0] = y[j][1];
	y[j][1] = -1;
      }
    }

    long long ans = -1;
    for(i=1;i<256;i++){
      if(y[i][0] != -1){
	if(ans == -1) ans = y[i][0];
	else ans = min(ans,y[i][0]);
      }
    }

    cout << ans << endl;
  }
  return 0;
}

C++: #include <iostream>
#include <memory>
#include <memory.h>
#include <cmath>
#include <fstream>
#include <cmath>
#include <numeric>
#include <vector>
#include <stack>
#include <string>
#include <queue>
#include <sstream>
#include <cstdlib>
#include <cassert>
#include <cstdio>
#include <map>
#include <iomanip>
#include <list>
#include <cctype>
#include <algorithm>
#include <complex>
#define USE_MATH_DEFINES
using namespace std;

typedef complex<double> xy_t;
typedef pair<xy_t, xy_t> line;
typedef long long ll;
typedef pair<int, int> P;
typedef pair<int, P> PP;
typedef pair<int, PP> PPP;
const int INF = 1 << 29;
const double EPS = 1E-10;

namespace std{
	bool operator < (const xy_t &a, const xy_t &b){
		return real(a) != real(b) ? real(a) < real(b) : imag(a) < imag(b);
	}
}

double cross(const xy_t &a, const xy_t &b){
	return imag(conj(a) * b);
}

double dot(const xy_t &a, const xy_t &b){
	return real(conj(a)*b);
}

int ccw(xy_t a, xy_t b, xy_t c){
	b -= a; c -= a;
	if(cross(b, c) > 0) return 1;
	if(cross(b, c) < 0) return -1;
	if(dot(b, c) < 0) return +2;
	if(norm(b) < norm(c) ) return -2;
	return 0;
}

bool intersectSS(const line &s, const line &t){
	return ccw(s.first, s.second, t.first) * ccw(s.first, s.second, t.second) <= 0 &&
		ccw(t.first, t.second, s.first) * ccw(t.first, t.second, s.second) <= 0;
}

xy_t crosspoint(const line &l, const line &m){
	double a = cross(l.second - l.first, m.second - m.first);
	double b = cross(l.second - l.first, l.second - m.first);
	if(abs(a) < EPS && abs(b) < EPS) return m.first;
	if(abs(a) < EPS) assert(false);
	return m.first + b / a * (m.second - m.first);
}

typedef pair<double, string> data;
data dat[200];
bool comp(const data &d1, const data &d2){
	return abs(d1.first - d2.first) > EPS ? d1.first < d2.first : d1.second  > d2.second;
}

int dp[20001][256];
int C[20];
int X[20001];
int pow(int n){
	return n * n;
}

int main(){
	int n, m;
	while(cin >>n >> m && (n || m)){
		fill(&dp[0][0], &dp[20000][255] + 1, INF);
		for(int i = 0; i < m; i++){
			cin >> C[i];
		}
		for(int i = 1; i <= n; i++){
			cin >> X[i];
		}
		dp[0][128] = 0;
		for(int i = 1; i <= n; i++){
			for(int j = 0; j < 256; j++){
				if(dp[i-1][j] == INF) continue;
				for(int k = 0; k < m; k++){
					if(C[k] < 0){
						dp[i][max(0, j+C[k])] = min(dp[i][max(0, j+C[k])], dp[i-1][j] + pow(X[i] - max(0, j+C[k])));
					}else{
						dp[i][min(255, j+C[k])] = min(dp[i][min(255, j+C[k])], dp[i-1][j] + pow(X[i] - min(255, j+C[k])));
					}
				}
			}
		}
		int res = INF;
		for(int i = 0; i < 256; i++) res = min(res, dp[n][i]);
		cout << res << endl;
	}
	return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
#define FOR(i,k,n) for(int i = (k); i < (n); i++)
#define REP(i,n) FOR(i,0,n)
#define INF 114514810
#define MOD 1000000007
#define ALL(a) (a).begin(),(a).end()
#define ms(m,v) memset(m,v,sizeof(m))
#define D10  fixed<<setprecision(10) 
typedef vector<int> vi;
typedef vector<string> vs;
typedef pair<int, int> P;
typedef long long ll;
struct edge{ int from, to, cost; };

int n, m;
int c[20]; int x[20005];
int memo[20005][300];

int dp(int y, int i)
{
	if (i == n) return 0;
	if (memo[i][y] != INF) return memo[i][y];
	int res = INF;
	REP(j, m)
	{
		int tmp = y;
		tmp += c[j];
		if (tmp > 255) tmp = 255;
		if (tmp < 0) tmp = 0;
		res = min(res, dp(tmp,i+1)+(tmp-x[i])*(tmp-x[i]));
	}
	return memo[y][i] = res;
}

int main()
{
	while (cin >> n >> m, n)
	{
		REP(i, 20005)REP(j, 300) memo[i][j] = INF;
		REP(i, m) cin >> c[i];
		REP(i, n) cin >> x[i];
		cout << dp(128, 0) << endl;
	}
	return 0;
}