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;
#define int long long
#define rep(i, n) for(int i = 0; i < n; i++)
#define all(a) a.begin(), a.end()
#define P pair<int, int>
#define F first
#define S second
#define prime 1000000007
#define INF 40000000000000000
int dx8[8] = {-1, 0, 1, 1, 1, 0, -1, -1};
int dy8[8] = {1, 1, 1, 0, -1, -1, -1, 0};
int dx4[4] = {-1, 0, 1, 0};
int dy4[4] = {0, -1, 0, 1};
signed main(void) {
    while(true) {
        int N, M;
        cin >> N >> M;
        if(N == 0 && M == 0) break;
        int Code[M], X[N];
        rep(i, M) cin >> Code[i];
        rep(i, N) cin >> X[i];
        vector<vector<int>> dp(20010, vector<int>(256, INF));
        dp[0][128] = 0;
        rep(i, N) rep(j, 256) {
            rep(k, M) {
                int next = j + Code[k];
                next = max(next, (int)0);
                next = min(next, (int)255);
                dp[i + 1][next] = min(dp[i + 1][next], dp[i][j] + (next - X[i]) * (next - X[i]));
            }
        }
        int ans = INF;
        rep(i, 256) ans = min(ans, dp[N][i]);
        cout << ans << endl;
    }
}

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

using namespace std;

#define chmin(a, b) (a=a<b?a:b);

const int inf = 1e9;

int N, M;
int c[16], x[20001];

int dp[20001][256];

signed main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);

    while (cin >> N >> M, N) {
        for (int i = 0; i <= 20000; ++i) for (int j = 0; j < 256; ++j) dp[i][j] = inf;

        for (int i = 0; i < M; ++i) cin >> c[i];
        for (int i = 1; i <= N; ++i) cin >> x[i];

        dp[0][128] = 0;

        for (int i = 1; i <= N; ++i) {
            for (int j = 0; j < 256; ++j) {
                if (dp[i - 1][j] != inf) {
                    for (int k = 0; k < M; ++k) {
                        int v = (c[k] < 0 ? max(j + c[k], 0) : min(j + c[k], 255));
                        int w = abs(v - x[i]);
                        chmin(dp[i][v], dp[i - 1][j] + w * w);
                    }
                }
            }
        }

        cout << *min_element(dp[N], dp[N] + 256) << endl;
    }
}

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

int main(){
  int i,j,k,n,m,c[17],N[20001],dp[256][17],x;
  
  while(1){
    cin >> n >> m;
    if( n == 0 && m ==  0 ) break;
  
    for( i = 0; i < m; i++ ) cin >> c[i];
    for( i = 0; i < n; i++ ) cin >> N[i];
    
    for( i = 0; i < 256; i++ ){
      for( j = 0; j <= n; j++ ){
	dp[i][j] = MAX;
      }
    }
    
    dp[128][0] = 0;
    //  int data[20000];
      for( j = 0; j < n; j++ ){
    for( i = 0; i <= 255; i++ ){
	//	int Min = 100000;
	for( k = 0; k < m; k++ ){
	  x = ( i + c[k] );
	  if( x < 0 ) x = 0;
	  if( x > 255 ) x = 255;
	  //dp[x][0] = 0;
	  dp[x][j+1] = min( dp[x][j+1], dp[i][j] + (x - N[j]) *( x - N[j] ) );

	  /*  if( Min > ( x - N[j] ) * ( x - N[j] ) ){
	    Min = ( x - N[j] ) * ( x - N[j] );
	    cout << "Min" << " " << Min << endl;
	    data[j] = x;
	  }
	  */
	}
      }
    }
    
      /* for( i = 0; i < 256; i++ ){
      cout << "i" << " " << i << " ";
      for( j = 0; j <= n; j++ ){
	cout <<  dp[i][j] << " ";
      }
      cout << endl;
      }*/
    int Min = 1000000;

     for( i = 0; i < 256; i++ ){
       if ( Min > dp[i][n] ){
	 Min = dp[i][n];
	 }
     }
     cout << Min << endl;
  }
  
  return 0;
}

C++: #include<iostream>
#include<vector>
#include<algorithm>

using namespace std;
int N,M;
vector<int> C,X;

void input(){
  C.resize(M);
  X.resize(N);
  for(int i = 0; i < M; i++) cin >> C[i];
  for(int i = 0; i < N; i++) cin >> X[i];
}

void solve(){
  const int Row = 2;
  const int Col = 256;
  const long long INF = (1LL<<50);
  
  long long dp[Row][Col];
  fill(dp[0],dp[0]+Row*Col,INF);

  dp[0][128] = 0;

  for(int i = 0; i < N; i++){
    for(int pos = 0; pos < Col; pos++) dp[(i+1)%2][pos] = INF;

    for(int j = 0; j < Col; j++){
      if(dp[i%2][j] == INF) continue;
      for(int k = 0; k < M; k++){
	int nex = j+C[k];
	nex = min(255,nex);
	nex = max(0,nex);
	dp[(i+1)%2][nex] = min(dp[(i+1)%2][nex],dp[i%2][j]+(X[i]-nex)*(X[i]-nex));
      }
    }
  }

  long long ans = INF;

  for(int i = 0; i < Col; i++) ans = min(ans,dp[N%2][i]);;
  cout << ans << endl;
}

int main(){
  while(cin >> N >> M && N+M){
    input();
    solve();
  }
  return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;
#define inf 1e9
#define ll long long
#define ull unsigned long long
#define M 1000000007
#define P pair<int,int>
#define PLL pair<ll,ll>
#define FOR(i,m,n) for(int i=m;i<n;i++)
#define RFOR(i,m,n) for(int i=m;i>=n;i--)
#define rep(i,n) FOR(i,0,n)
#define rrep(i,n) RFOR(i,n,0)
#define all(a) a.begin(),a.end()
const int vx[4] = {0,1,0,-1};
const int vy[4] = {1,0,-1,0};
#define PI 3.14159265



void f(int n,int m){
  int c[1000000];
  
  rep(i,m){
    cin>>c[i];
  }

  int dp[20][300]={};
  rep(i,20){
    rep(j,300){
      dp[i][j]=inf;
    }
  }

  dp[0][128]=0;

  FOR(i,1,n+1){
    int x;
    cin>>x;
    rep(j,256){
      rep(k,m){
        int a = j+c[k];
        a = min(a,255);
        a = max(a,0);
        dp[i][a]=min(dp[i][a],dp[i-1][j]+(x-a)*(x-a));
      }
    }
  }
  int ans = inf;
  rep(i,256){
    ans = min(ans,dp[n][i]);
  }

  cout<<ans<<endl;
}


int main(){
  int n,m;
  while(1){
    cin>>n>>m;
    if(n==0) break;
    f(n,m);
  }

  return 0;
}