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 <cstdio>
#include <iostream>
#include <cmath>
#include <ctype.h>
#include <string> 
#include <sstream>
#include <iostream>
#include <algorithm>
#include <cstdlib>
#include <map>
#include <queue>
#include <utility>
#include <vector>
#include <set>
#include <list>
#include <cstring>
#include <stack>
   
using namespace std;

int result[20001][256];

int main()
{
    int n, m;
    while(1){
        cin >> n >> m;
        if(n == 0 && m == 0) break;
        int c[17];
        int x[20001];
        for(int i = 0; i < m; i++){
            cin >> c[i];
        }
        for(int j = 0; j < n; j++){
            cin >> x[j];
        }
        
        // result[i][j] : i???????????????j?????£?????¨?????????????????§???????????±?????????????°????
        for(int i = 0; i < n + 1; i++){
            for(int j = 0; j < 256; j++){
                result[i][j] = -1;
            }
        }
        for(int i = 0; i < m; i++){
            int tmp = 128 + c[i];
            if(tmp > 255) tmp = 255;
            if(tmp < 0) tmp = 0;
            result[0][tmp] = (tmp - x[0]) * (tmp - x[0]);
        }
        for(int i = 1; i < n; i++){
            for(int j = 0; j < 256; j++){
                if(result[i - 1][j] == -1) continue;
                for(int k = 0; k < m; k++){
                    int tmp = j + c[k];
                    if(tmp > 255) tmp = 255;
                    if(tmp < 0) tmp = 0;
                    if(result[i][tmp] == -1 || result[i][tmp] > result[i - 1][j] + (tmp - x[i]) * (tmp - x[i])){
                        result[i][tmp] = result[i - 1][j] + (tmp - x[i]) * (tmp - x[i]);
                    }
                }
            }
        }
        // for(int i = 0; i < n; i++){
        //     for(int j = 0; j < 256; j++){
        //         cout << result[i][j] << " ";
        //     }
        //     cout << endl;
        // }
        int ans = 100000000;
        for(int i = 0; i < 256; i++){
            if(result[n - 1][i] != -1) ans = min(ans, result[n - 1][i]);
        }
        cout << ans << endl;
    }
    return 0;
}

C++: #include <iostream>
#include <algorithm>
#include <utility>
#include <vector>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <string>
#include <sstream>
#include <cstring>
#include <cstdlib>

using namespace std;

#define ALL(c) c.begin(),c.end()
#define RALL(c) c.rbegin(),c.rend()
#define SORT(x) sort((x).begin(),(x).end())
#define REP(i,x,y) for(int i=(x);i<(y);++i)
#define MP(a,b) make_pair((a),(b))
#define F_ first
#define S_ second
#define debug(x) #x << "=" << (x)
 
#ifdef DEBUG
#define _GLIBCXX_DEBUG
#define dump(x) std::cerr << debug(x) << " (L:" << __LINE__ << ")" << std::endl
#else
#define dump(x)
#endif

typedef long long int lli;
typedef pair<int,int> pii;

const int INF=1<<30;
const long long int INF_=1LL<<58;
const double EPS=1e-9;
const int dx[]={1,0,-1,0},dy[]={0,1,0,-1};

template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec){
	os << "[";
	for (const auto &v : vec) {
		os << v << ",";
	}
	os << "]";
	return os;
}

int main(){
	std::ios::sync_with_stdio(false);
	std::cin.tie(0);
	int N,M;
	while(true){
		cin >> N >> M;
		if(!N&&!M) break;
		vector<lli> C(M),x(N+1);
		REP(i,0,M) cin >> C[i];
		REP(i,1,N+1) cin >> x[i];
		vector<vector<lli>> dp(N+1,vector<lli>(256,INF_));
		dp[0][128]=0;
		lli ans=INF_;
		REP(i,0,N+1){
			REP(j,0,256){
				if(i!=N){
					REP(k,0,M){
						lli v=j+C[k];
						if(v<0) v=0;
						if(v>=256) v=255;
						dp[i+1][v]=min(dp[i+1][v],dp[i][j]+(v-x[i+1])*(v-x[i+1]));
					}
				}
				if(i==N) ans=min(ans,dp[i][j]);
			}
		}
		cout << ans << endl;
	}
	return 0;
}

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

typedef long long lli;

const int MAX_N = 20001;
const int MAX_M = 16;
const int MAX_V = 256;
const lli INF = 1LL<<60;

lli T[MAX_N][MAX_V];
int N, M;
lli C[MAX_M];
lli x[MAX_N];

lli solve() {
  fill(T[0], T[N+1], INF);
  T[0][128] = 0LL;
  for(int i = 1; i <= N; ++i) {
    for(int j = 0; j <= 255; ++j) {
      for(int k = 0; k < M; ++k) {
	lli value = j + C[k];
	if(value < 0LL) value = 0LL;
	if(value > 255LL) value = 255LL;
	T[i][value] = min(T[i][value], T[i-1][j] + (x[i]-value)*(x[i]-value));
      }
    }
  }
  lli res = INF;
  for(int i = 0; i <= 255; ++i) {
    res = min(res, T[N][i]);
  }
  return res;
}

int main() {
  while(cin >> N >> M && (N || M)) {
    for(int i = 0; i < M; ++i) {
      cin >> C[i];
    }
    for(int i = 1; i <= N; ++i) {
      cin >> x[i];
    }
    cout << solve() << endl;
  }
}