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 vi=vector<int>;
using vvi=vector<vi>;
using vs=vector<string>;
using msi=map<string,int>;
using mii=map<int,int>;
using pii=pair<int,int>;
using vlai=valarray<int>;
using ll=long long;
#define rep(i,n) for(int i=0;i<n;i++)
#define range(i,s,n) for(int i=s;i<n;i++)
#define all(a) a.begin(),a.end()
#define rall(a) a.rbegin(),a.rend()
#define fs first
#define sc second
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define INF 1e9
#define EPS 1e-9

int main(){
	int n,m;
	while(cin>>n>>m,n){
        vi code(m);
        rep(i,m)cin>>code[i];
        vvi dp(n+1,vi(256,INF));
        dp[0][128]=0;
        range(i,1,n+1){
            int num; cin>>num;
            rep(j,256){
                rep(k,m){
                    int next=min(max(j+code[k],0),255);
                    int diff=num-next;
                    dp[i][next]=min(dp[i][next],dp[i-1][j]+diff*diff);
                }
            }
        }
        cout<<*min_element(all(dp[n]))<<endl;
	}
	return 0;
}

C++: #include <bits/stdc++.h>
#define PB push_back
#define MP make_pair
#define REP(i,n) for (int i=0;i<(n);i++)
#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define ALL(a) (a).begin(),(a).end()
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> P;
const int INF=2e9;
double EPS=1e-10;
int main(){
      int n,m;
      while(cin>>n>>m&&(n||m)){
            int a[n],b[m];
            ll dp[2][256];
            REP(i,m)cin>>b[i];
            REP(i,n)cin>>a[i];
            memset(dp,-1,sizeof(dp));
            dp[0][128]=0;
            REP(i,n){
                  int l=i&1;
                  REP(j,256)dp[l^1][j]=-1;
                  REP(j,256){
                        if(dp[l][j]==-1)continue;
                        REP(k,m){
                              int nxt=j+b[k];
                              nxt=max(0,min(255,nxt));
                              if(dp[l^1][nxt]==-1)dp[l^1][nxt]=dp[l][j]+(a[i]-nxt)*(a[i]-nxt);
                              else dp[l^1][nxt]=min(dp[l^1][nxt],dp[l][j]+(a[i]-nxt)*(a[i]-nxt));
                        }
                  }
            }
            ll ans=-1;
            REP(i,256)if(dp[n&1][i]!=-1){
                  if(ans==-1)ans=dp[n&1][i];
                  else ans=min(ans,dp[n&1][i]);
            }
            cout<<ans<<endl;
      }
}

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

using namespace std;

#define REP(i,n) for(int i=0;i<(n);i++)
#define REPR(i,n) for(int i=(n)-1;i>=0;i--)

#define FOR(i,m,n) for(int i=(m);i<(n);i++)
#define int long long
#define ALL(x) (x).begin(),(x).end()
typedef pair<int, int> pii;

int dp[20020][400];//i番目までで、y[i]=jとしたときの誤差の二乗和の最小値

void solve() {
	int N, M; cin >> N >> M;
	if (N == 0)exit(0);
	vector<int>C(M);
	REP(i, M)cin >> C[i];
	vector<int>X(N);
	REP(i, N) {
		cin >> X[i];
	}
	REP(i, N + 1) {
		REP(j, 300)dp[i][j] = 1e18;
	}
	dp[0][128] = 0;
	REP(i, N) {
		REP(j, 256) {
			for (int c : C) {
				int nxt = j + c;
				if (nxt < 0)nxt = 0;
				if (nxt > 255)nxt = 255;
				int dif = X[i] - nxt;
				dif *= dif;
				dp[i + 1][nxt] = min(dp[i + 1][nxt], dp[i][j] + dif);
			}
		}
	}
	int ans = 1e18;
	REP(j, 256) {
		ans = min(ans, dp[N][j]);
	}
	cout << ans << endl;
}

signed main() {
	while(true)solve();
}

C++: #include <iostream>
#include <cstdio>

using namespace std;

int dp[20010][256];
int book[20];
int input[20010];
int N, M;

static const int MAX = 10000000;

void init() {
  fill(&dp[0][0], &dp[20009][256], MAX);
}

void solve() {
  dp[0][128] = 0;

  for (int i = 0; i < N; i++) {
    for (int j = 0; j < 256; j++) {
      for (int k = 0; k < M; k++) {
        int tar = j + book[k];
        if (tar < 0) {
          tar = 0;
        } else if (tar > 255) {
          tar = 255;
        }
        int sub = tar - input[i];
        sub *= sub;
        dp[i + 1][tar] = min(dp[i + 1][tar], dp[i][j] + sub);
      }
    }
  }

  int mn = MAX;
  for (int i = 0; i < 256; i++) {
    mn = min(mn, dp[N][i]);
  }
  printf("%d\n", mn);
}

int main() {
    while (true) {
      scanf("%d %d ", &N, &M);
      if (N == 0 && M == 0) {
        break;
      }
      init();
      for (int i = 0; i < M; i++) {
        scanf("%d ", &book[i]);
      }
      for (int i = 0; i < N; i++) {
        scanf("%d ", &input[i]);
      }
      solve();
    }
}