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 <limits.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <algorithm>
#include <cassert>
#include <cfloat>
#include <complex>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <vector>

using namespace std;

#define chmax(x, y) x = max(x, y)
#define chmin(x, y) x = min(x, y)
#define rep(i, n) for (ll i = 0; i < (n); ++i)
#define Sort(v) sort(v.begin(), v.end())
#define Reverse(v) reverse(v.begin(), v.end())
#define Lower_bound(v, x) \
  distance(v.begin(), lower_bound(v.begin(), v.end(), x))
#define Upper_bound(v, x) \
  distance(v.begin(), upper_bound(v.begin(), v.end(), x))

using ll = long long;
using ull = unsigned long long;
using P = pair<ll, ll>;
using T = tuple<ll, ll, ll>;
using vll = vector<ll>;
using vP = vector<P>;
using vT = vector<T>;
using vvll = vector<vector<ll>>;
using vvP = vector<vector<P>>;
using dqll = deque<ll>;

ll dx[9] = {-1, 1, 0, 0, -1, -1, 1, 1, 0};
ll dy[9] = {0, 0, -1, 1, -1, 1, -1, 1, 0};

const ll INF = 1LL << 50;

const ll mod = 1e9 + 7;

int main() {
  while (true) {
    ll n, m;
    cin >> n >> m;
    if (n == 0 && m == 0) break;

    vll cs(m);
    rep(i, m) cin >> cs[i];
    vll xs(n);
    rep(i, n) cin >> xs[i];

    auto dis = [](ll x, ll y) { return (x - y) * (x - y); };

    vvll dp(n + 1, vll(256, INF));
    dp[0][128] = 0;
    rep(i, n){
      rep(j, 256){
        rep(k, m) { ll nj = j + cs[k];
          chmin(nj, 255LL);
          chmax(nj, 0LL);
          ll cst = dis(nj, xs[i]);
          chmin(dp[i + 1][nj], dp[i][j] + cst);
        }  // k
      } // j
    } // i
    ll ans = INF;
    rep(j, 256) chmin(ans, dp[n][j]);
    cout << ans << endl;
  }  // while

  return 0;
}

C++: #include <iostream>
#include <algorithm>
using namespace std;

typedef long long lli;

const int MAX_N = 20001;
const int MAX_M = 16;
const int MAX_V = 256;
const lli INF = 1LL<<60;

lli T[MAX_N][MAX_V];
int N, M;
lli C[MAX_M];
lli x[MAX_N];

lli solve() {
  fill(T[0], T[N+1], INF);
  T[0][128] = 0LL;
  for(int i = 1; i <= N; ++i) {
    for(int j = 0; j <= 255; ++j) {
      for(int k = 0; k < M; ++k) {
	lli value = j + C[k];
	if(value < 0LL) value = 0LL;
	if(value > 255LL) value = 255LL;
	T[i][value] = min(T[i][value], T[i-1][j] + (x[i]-value)*(x[i]-value));
      }
    }
  }
  lli res = INF;
  for(int i = 1; i <= 255; ++i) {
    res = min(res, T[N][i]);
  }
  return res;
}

int main() {
  while(cin >> N >> M && (N || M)) {
    for(int i = 0; i < M; ++i) {
      cin >> C[i];
    }
    for(int i = 1; i <= N; ++i) {
      cin >> x[i];
    }
    cout << solve() << endl;
  }
}

C++: #include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <map>
#include <set>
#include <list>
#include <queue>
#include <deque>
#include <stack>
#include <vector>
#include <algorithm>
#include <functional>
#include <cstring>
#include <string>
#include <sstream>
#include <bitset>
using namespace std;
#define INF	1000000000000
#define MOD 1000000007
#define pb push_back
#define mp make_pair
#define fi first
#define sec second
#define lb lower_bound
#define ub upper_bound
#define SS stringstream
#define rep(i,n) for(int i = 0; i < n; i++)
#define SORT(x) sort((x).begin(), (x).end())
#define clr(a,b) memset((a),(b),sizeof(a))
typedef long long int ll;
typedef pair<int, int> P;
typedef vector<int> Vi;
typedef vector<ll> Vll;
typedef vector<P> Vp;
typedef priority_queue<P, vector<P>, greater<P> > PQ;

ll dp[20001][256];
int c[16];
int x[20000];

int main(){
	int n,m;
	while(scanf("%d%d", &n, &m),n||m){
		rep(i,20001) rep(j,256) dp[i][j] = INF;
		dp[0][128] = 0;
		rep(i,m) scanf("%d", &c[i]);
		rep(i,n) scanf("%d", &x[i]);
		for(int i = 1; i <= n; i++){
			for(int j = 0; j < 256; j++){
				for(int k = 0; k < m; k++){
					if(0 > j+c[k]) dp[i][0] = min(dp[i][0],dp[i-1][j]+x[i-1]*x[i-1]);
					else if(j+c[k] >= 256) dp[i][255] = min(dp[i][255],dp[i-1][j]+(255-x[i-1])*(255-x[i-1]));
					else dp[i][j+c[k]] = min(dp[i][j+c[k]],dp[i-1][j]+(j+c[k]-x[i-1])*(j+c[k]-x[i-1]));
				}
			}
		}
		ll ans = INF;
		rep(i,256){
			ans = min(ans,dp[n][i]);
		}
		cout << ans << endl;
	}
}