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 <iostream>
#include <cstdio>

using namespace std;

typedef int ll;

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

static const ll MAX = 100000000;

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;
        }
        ll sub = tar - input[i];
        sub *= sub;
        dp[i + 1][tar] = min(dp[i + 1][tar], dp[i][j] + sub);
      }
    }
  }

  ll mn = MAX;
  for (int i = 0; i < 256; i++) {
    mn = min(mn, dp[N][i]);
  }
  printf("%ld\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();
    }
}

C++: #include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <ctime>
#include <fstream>
#include <queue>

#pragma warning( disable: 4996 )

using namespace std;

typedef long long ll;
typedef pair<int, int> P;


#define INF 1000000000
#define EPS 1e-10

#define MAX_N 20000
#define MAX_M 16

int N, M;
int C[MAX_M];
int x[MAX_N];

ll dp[MAX_N + 1][256];

vector<ll> ans_list;

int main() {

	while (true){
		cin >> N >> M;
		if (N == 0 && M == 0)
			break;

		for (int i = 0; i < M; i++){
			cin >> C[i];
		}

		for (int i = 0; i < N; i++){
			cin >> x[i];
		}

		for (int i = 0; i < N + 1; i++){
			for (int j = 0; j <= 255; j++){
				dp[i][j] = INF;
			}
		}

		dp[0][128] = 0;

		for (int i = 0; i < N; i++){
			for (int j = 0; j <= 255; j++){
				for (int k = 0; k < M; k++){
					int num = j + C[k];
					if (num < 0)
						num = 0;
					if (num > 255)
						num = 255;
					dp[i+1][num] = min(dp[i+1][num], (x[i] - num) * (x[i] - num) + dp[i][j]);
				}
			}
		}

		ll ans = INF;

		for (int i = 0; i <= 255; i++){
			ans = min(ans, dp[N][i]);
		}

		ans_list.push_back(ans);

	}
	
	for (int i = 0; i < ans_list.size(); i++){
		cout << ans_list[i] << endl;
	}
	

	return 0;
}

C++: #include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<ll, ll> P;

#define EACH(i,a) for (auto& i : a)
#define FOR(i,a,b) for (ll i=(a);i<(b);i++)
#define RFOR(i,a,b) for (ll i=(b)-1;i>=(a);i--)
#define REP(i,n) for (ll i=0;i<(n);i++)
#define RREP(i,n) for (ll i=(n)-1;i>=0;i--)
#define debug(x) cout<<#x<<": "<<x<<endl
#define pb push_back
#define ALL(a) (a).begin(),(a).end()

const ll linf = 1e18;
const int inf = 1e9;
const double eps = 1e-12;
const double pi = acos(-1);

template<typename T>
istream& operator>>(istream& is, vector<T>& vec) {
	EACH(x,vec) is >> x;
	return is;
}
/*
template<class... T>
ostream& operator<<(ostream& os, tuple<T...>& t) {
	for (size_t i = 0; i < tuple_size< tuple<T...> >::value; ++i) {
		if (i) os << " ";
		os << get<0>(t);
	}
	return os;
}
*/
template<typename T>
ostream& operator<<(ostream& os, vector<T>& vec) {
	REP(i,vec.size()) {
		if (i) os << " ";
		os << vec[i];
	}
	return os;
}
template<typename T>
ostream& operator<<(ostream& os, vector< vector<T> >& vec) {
	REP(i,vec.size()) {
		if (i) os << endl;
		os << vec[i];
	}
	return os;
}

ll dp[20001][256];
ll f(ll x, ll y) {
	return (x - y) * (x - y);
}
int main() {
	std::ios::sync_with_stdio(false);
	std::cin.tie(0);
	int N, M;
	while ( cin >> N >> M, N || M ) {
		fill(dp[0], dp[20001], linf);
		vector<int> C(M); cin >> C;
		vector<int> x(N); cin >> x;
		dp[0][128] = 0;
		REP(i, N) {
			REP(j, 256) {
				if (dp[i][j] == linf) continue;
				REP(k, M) {
					int nj = max(0LL, min(255LL, j + C[k]));
					dp[i+1][nj] = min(dp[i+1][nj], dp[i][j] + f(nj, x[i]));
				}
			}
		}
		ll ans = linf;
		REP(i, 256) ans = min(ans, dp[N][i]);
		cout << ans << endl;
	}
}