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>
typedef long long ll;
using namespace std;
ll MOD = 1000000007;

#define vec                vector<int>
#define vecll              vector<ll>
#define vecd               vector<double>
#define vecst              vector<string>
#define vecb               vector<bool>
#define vec2(var, n, m)    vector<vector<int>>  var(n, vector<int>(m, INF))
#define vecb2(var, n, m)   vector<vector<bool>> var(n, vector<bool>(m, false))
#define vecll2(var, n, m)  vector<vector<ll>>   var(n, vector<ll>(m, INF))

#define rep(i,n)         for(ll i=(ll)0; i<(ll)n; i++)
#define REP(i,m,n)       for(ll i=(ll)m; i<(ll)n; i++)
#define arr(var, n)      vec var(n); rep(i,n){cin >> var[i];}
#define arrll(var, n)    vecll var(n); rep(i,n){cin >> var[i];}
#define arrst(var, n)    vecst var(n); rep(i,n){cin >> var[i];}
#define all(var)         (var).begin(), (var).end()
#define sortall(var)     sort(all(var))
#define uniqueall(v)     v.erase(unique(v.begin(), v.end()), v.end());
#define f_sum(var)       accumulate(all(var), 0)
#define f_sumll(var)     accumulate(all(var), 0LL)
#define chmin(v1, v2)    v1 = min(v1, v2)
#define chmax(v1, v2)    v1 = max(v1, v2)
#define pb(var)          push_back(var)
#define prt(var)         cout << (var) << "\n"
#define prtd(n, var)     cout << fixed << setprecision(n) << (var) << "\n"
#define prtfill(n, var)  cout << setw(n) << setfill('0') << (var);
#define prt2(v1, v2)     cout << (v1) << " " << (v2) << "\n"
#define prt3(v1, v2, v3) cout << (v1) << " " << (v2) << " " << (v3) << "\n"
#define prtall(v)        rep(i,v.size()){cout<<v[i]<<(i!=v.size()-1?" ":"\n");}
void prtok(bool ok){prt(ok ? "Yes" : "No");}
//----------------------------------------------------------------

int main(void) {
  while(1){
    int n, m;
    cin >> n >> m;
    if(n==0){break;}
    arr(c,m);
    arr(x,n);

    ll INF = 1e18;
    vecll2(dp, 20001, 256);

    dp[0][128]=0;

    rep(i,n){
      rep(j,256){
        if(dp[i][j]==INF) continue;
        rep(k,m){
          int yn = j + c[k];
          if(yn>255) yn=255;
          if(yn<0) yn=0;
          int tmp=(x[i]-yn)*(x[i]-yn);
          chmin(dp[i+1][yn], dp[i][j]+tmp);
        }
      }
    }

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

C++: #include<iostream>
#include<vector>
#define NUM 255
using namespace std;
typedef long long ll;
#define int ll
int min(int a,int b){
  return (a<b)?a:b;
}
signed main(){
  int n,m,y,num,ma_c,mi_c;
  cin >> n >> m;
  while(n!=0){
   int c[m],x[n],low=100000000; 
   ma_c=-300,mi_c=300;
   vector<int> dp[20001];
    for(int i=0;i<m;i++){
      cin >> c[i];
      ma_c=(ma_c<c[i])?c[i]:ma_c;
      mi_c=(mi_c>c[i])?c[i]:mi_c;
    }
    for(int i=0;i<n;i++){
      cin >> x[i];
    }
    for(int i=0;i<=n;i++){
      for(int j=0;j<=NUM;j++){
        dp[i].push_back(-1);
      }
    }
    dp[0][128]=0;
    for(int i=1;i<=n;i++){
      for(int j=NUM-ma_c;j<=NUM;j++){
        if(dp[i-1][j]!=-1){
          if(dp[i][NUM]==-1)dp[i][NUM]=dp[i-1][j]+(x[i-1]-NUM)*(x[i-1]-NUM);
          else dp[i][NUM] = min(dp[i-1][j]+(x[i-1]-NUM)*(x[i-1]-NUM),dp[i][NUM]);
        } 
      }
      for(int j=-mi_c;j>=0;j--){
        if(dp[i-1][j]!=-1){
          if(dp[i][0]==-1)dp[i][0]=dp[i-1][j]+(x[i-1])*(x[i-1]);
          else dp[i][0] = min(dp[i-1][j]+(x[i-1])*(x[i-1]),dp[i][0]);
        } 
      }
      for(int j=0;j<=NUM;j++){
        for(int k=0;k<m;k++){
          num=j-c[k];
          if(num<0) continue;
          else if(num>NUM)continue;
          if(dp[i-1][num]!=-1){
            if(dp[i][j]==-1) dp[i][j]=dp[i-1][num]+(x[i-1]-j)*(x[i-1]-j);
            else dp[i][j]=min(dp[i-1][num]+(x[i-1]-j)*(x[i-1]-j),dp[i][j]);
          }
        }
//        cout << i<<" "<<j<<" "<<dp[i][j]<<endl;
      }
    }
//    for(int i=0;i<=n;i++){for(int j=0;j<=NUM;j++)cout <<dp[i][j]<<" ";cout << endl;}
    for(int i=0;i<=NUM;i++){
      if(dp[n][i]!=-1){
        low=min(low,dp[n][i]);
      }
    }
    cout << low<<endl;
    cin >> n >> m;
  }
}

C++: #include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll,ll> pll;

ll inf=1e18;

int main() {
  while(1) {
    ll n,m;
    ll mx=255,mi=0;
    cin >> n >> m;
    if(n==0&&m==0) break;
    vector<ll> code(m);
    for(ll i=0;i<m;i++) {
      cin >> code[i];
    }
    vector<ll> a(n);
    for(ll i=0;i<n;i++) {
      cin >> a[i];
    }
    vector<vector<ll>> dp(n+1,vector<ll> (256,inf));
    dp[0][128]=0;
    for(ll i=0;i<n;i++) {
      for(ll j=0;j<256;j++) {
        for(ll k=0;k<m;k++) {
          ll temp=j+code[k];
          temp=max(mi,min(mx,temp));
          dp[i+1][temp]=min(dp[i+1][temp],dp[i][j]+(a[i]-temp)*(a[i]-temp));
        }
      }
    }
    ll ans=inf;
    for(ll i=0;i<256;i++) {
      ans=min(dp[n][i],ans);
    }
    cout << ans << endl;
  }
}