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 <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <unordered_map>
#include <vector>
#include <string.h>
#include <set>
#include <stack>

using namespace std;

#define COUT(x) cout << #x << " = " << (x) << " (L" << __LINE__ << ")" << endl

template<class T> void printvec(const vector<T>& v) {
  for (auto x : v) { cout << x << " "; }
  cout << endl;
}
template<class T> void printtree(const vector< vector<T> >& tree) {
  for (long long i = 0; i < tree.size(); ++i) {
    cout << i + 1 << ": "; printvec(tree[i]);
  }
}
template<class T, class U> void printmap(const map<T, U>& mp) {
  for (auto x : mp) { cout << x.first << "=>" << x.second << endl; }
}

template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }

#define rep(i, n) for(ll i = 0; i < n; ++i)
#define all(s) s.begin(), s.end()
#define sz(x) (ll)(x).size()
#define fr first
#define sc second
#define mp make_pair
#define pb push_back
#define eb emplace_back

typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll> P;
typedef tuple<ll, ll, ll> triple;
typedef double D;
typedef vector<ll> vl;
typedef vector<P> vp;

const ll INF = 1e15;
const ll MOD = 1000000007;  // 1e9 + 7

int main(int argc, char** argv) {
  cin.tie(NULL);
  cout.tie(NULL);
  ios_base::sync_with_stdio(false);
  //cout << setprecision(10) << fixed;

  while (true) {
    ll n,m;
    cin >> n >> m;
    if (n == 0 && m == 0) { break; }
    vector<ll> c(m);
    rep(i,m) {
      cin >> c[i];
    }
    vector<ll> x(n);
    rep(i,n) {
      cin >> x[i];
    }

    // dp[i][j] .. min cost using [0,i), select j as prev. j is in [0,255]
    vector<vector<ll>> dp(n+1,vector<ll>(256, INF));
    dp[0][128] = 0;
    rep(i,n) {
      rep(j,256) { // prev value
        if (dp[i][j] == INF) { continue; }
        // cout << "dp[i][j]: " << dp[i][j] << endl;
        rep(k,m) {
          ll now = j + c[k];
          chmax(now, 0ll);
          chmin(now, 255ll);
          ll cost = (x[i]-now)*(x[i]-now); // when select k, cost is (x[i]-now)^2
          chmin(dp[i+1][now], dp[i][j] + cost);
        }
      }
    }

    ll ans = INF;
    rep(i,256) {
      chmin(ans, dp[n][i]);
    }
    cout << ans << endl;
  }
}

C++: // http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=2199

#include <iostream>
#include <algorithm>

using namespace std;

long long DP[20010][256]; // DP[i][j] = y_i=jである場合のi文字目までの最小コスト
int C[20];
int X[20010];

const long long infty = 1e12;

int main() {
  int N, M;
  while (cin >> N >> M && N) {
    for (int i=0; i<M; i++) {
      cin >> C[i];
    }
    for (int i=0; i<N; i++) {
      cin >> X[i];
    }
    fill(&DP[0][0], &DP[0][0]+20010*256, infty);
    DP[0][128] = 0;
    for (int i=0; i<N; i++) {
      for (int j=0; j<256; j++) {
	if (DP[i][j] < infty) {
	  for (int k=0; k<M; k++) {
	    int y = j + C[k];
	    if (y > 255) y = 255;
	    if (y < 0) y = 0;
	    long long cost = DP[i][j] + (X[i] - y) * (X[i] - y);
	    if (DP[i+1][y] > cost) {
	      DP[i+1][y] = cost;
	    } 
	  }
	}
      }
    }
    long long ans = infty;
    for (int j=0; j<256; j++) {
      if (ans > DP[N][j]) {
	ans = DP[N][j];
      }
    }
    cout << ans << endl;
  }
}

C++: #include <bits/stdc++.h>

using namespace std;

#define INF (1 << 25)

int dp[20001][256];


int main()
{
  int N, M;
  while(cin >> N >> M, N || M){
    int C[16], x[20000];
    for(int i = 0; i < M; i++) cin >> C[i];
    for(int i = 0; i < N; i++) cin >> x[i];
    for(int i = 0; i < 20001; i++) for(int j = 0; j < 256; j++) dp[i][j] = INF;
    dp[0][128] = 0;
    for(int i = 0; i < N; i++){
      for(int j = 0; j < 256; j++){
	if(dp[i][j] == INF) continue;
	for(int k = 0; k < M; k++){
	  int y = (j + C[k] < 0 ? 0 : (j + C[k] < 255 ? j + C[k] : 255));
	  int sqsum = (x[i] - y)*(x[i] - y);
	  dp[i+1][y] = min(dp[i+1][y], dp[i][j] + sqsum);	  
	}
      }
    }
    cout << *min_element(dp[N], dp[N] + 256) << endl;
  }
  return 0;
}