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;
using ll = long long;

int main(){
    while(1){
        int n, m;
        cin >> n >> m;
        if(n == 0 && m == 0) break;
        vector<int> sample(n), c(m);
        for(int i = 0; i < m; i++) cin >> c[i];
        for(int i = 0; i < n; i++) cin >> sample[i];
        vector<vector<ll>> dp(n+1, vector<ll>(256, -1));
        dp[0][128] = 0;
        for(int i = 0; i < n; i++){
            for(int j = 0; j <=255; j++){
                if(dp[i][j] != -1){
                    for(int k = 0; k < m; k++){
                        int idx = j+c[k];
                        idx = min(255, idx);
                        idx = max(0, idx);
                        if(dp[i+1][idx] == -1) dp[i+1][idx] = dp[i][j] + (idx-sample[i])*(idx-sample[i]);
                        else dp[i+1][idx] = min(dp[i+1][idx], dp[i][j]+(idx-sample[i])*(idx-sample[i]));
                    }
                }
            }
        }

        ll ans = 1e9;
        for(int i = 0; i <= 255; i++){
            if(dp[n][i] != -1) ans = min(ans, dp[n][i]);
        }
        cout << ans << endl;
    }
}

C++: #include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define FOR(i, s, e) for (int i = (int)(s); i <= (int)(e); i++)
#define printYesNo(is_ok) puts(is_ok ? "Yes" : "No");
#define SORT(v) sort(v.begin(), v.end());
#define RSORT(v) sort(v.rbegin(), v.rend());
#define REVERSE(v) reverse(v.begin(), v.end());

int solve(int N, int M)
{
  vector<int> C(M), x(N);
  rep(i, M)
  {
    cin >> C[i];
  }
  rep(i, N)
  {
    cin >> x[i];
  }
  vector<vector<int>> dp(N + 1, vector<int>(256, INT32_MAX));
  dp[0][128] = 0;
  rep(i, N) rep(j, 256)
  {
    if(dp[i][j] != INT32_MAX)
    {
      rep(k, M)
      {
        int nj = j + C[k];

        if(nj < 0)
          nj = 0;          

        if(nj > 255)
          nj = 255;
        int dist = pow(nj - x[i], 2);
        dp[i + 1][nj] = min(dp[i + 1][nj], dist + dp[i][j]);
      }
    }
  }
  int dst = INT32_MAX;
  rep(i, 256)
  {
    dst = min(dst, dp[N][i]);
  }
  return dst;
}

int main()
{
  while(true)
  {
    int N, M;
    cin >> N >> M;
    if(N != 0 || M != 0)
      cout << solve(N, M) << endl;
    else
      break;
  }
  return 0;
}

C++: #include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define loop(i, x, n) for (int i = (x); i < (n); i++)
#define all(v) (v).begin(), (v).end()
#define int long long
#define INF 1e9
using namespace std;
template<typename T> void cmin(T &a, T b) { a = min(a, b); }
template<typename T> void cmax(T &a, T b) { a = max(a, b); }

int dp[20005][256];

signed main() {
  int n, m;
  while (cin >> n >> m) {
    if (n == 0 && m == 0) break;
    rep(i, 20005) rep(j, 256) dp[i][j] = INF;
    vector<int> C(m), x(n);
    rep(i, m) cin >> C[i];
    rep(i, n) cin >> x[i];
    dp[0][128] = 0;
    rep(i, n) {
      rep(j, 256) {
        if (dp[i][j] == INF) continue;
        rep(k, m) {
          int y = j + C[k];
          cmin(y, 255LL);
          cmax(y, 0LL);
          int t = abs(y - x[i]);
          dp[i + 1][y] = min(dp[i + 1][y], dp[i][j] + t * t);
        }
      }
    }
    int ans = INF;
    rep(i, 256) cmin(ans, dp[n][i]);
    cout << ans << endl;
  }

  return 0;
}

C++: #include "bits/stdc++.h"

#define REP(i, n ,N) for(ll i = (n); i < (N); i++)
#define p(s) cout<<(s)<<endl
#define p2(a, b) cout<<(a)<<" "<<(b)<<endl
using namespace std;
typedef long long ll;
ll MOD = 1e9+7;
ll inf = 1e18;


ll N, M;
ll dp[20010][256];
int main() {
	while (cin >> N >> M && N > 0) {
		ll C[20];
		ll x[20010];
		REP(i,0,20010){
			REP(j,0,256){
				dp[i][j] = inf;
			}
		}
		dp[0][128] = 0;
		REP(i,0,M) cin >> C[i];
		REP(j,0,N) cin >> x[j];
		REP(i,0,N){
			REP(y,0,256){
				REP(k,0,M){
					ll next_y = max(min(y + C[k], 255LL), 0LL);
					dp[i+1][next_y] = min(dp[i+1][next_y], dp[i][y] + (x[i] - next_y)*(x[i] - next_y));
				}
			}
		}
		ll ans = inf;
		REP(y,0,256){
			ans = min(ans, dp[N][y]);
		}
		p(ans);
	}

	return 0;
}