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"
#define Rep(i,n) for(int i=0;i<(int)(n);i++)
#define For(i,n1,n2) for(int i=(int)(n1);i<(int)(n2);i++)
#define REP(i,n) for(ll i=0;i<(ll)(n);i++)
#define RREP(i,n) for(ll i=((ll)(n)-1);i>=0;i--)
#define FOR(i,n1,n2) for(ll i=(ll)(n1);i<(ll)(n2);i++)
#define RFOR(i,n1,n2) for(ll i=((ll)(n1)-1);i>=(ll)(n2);i--)
#define all(a)  (a).begin(),(a).end()
#define IOS std::cin.tie(0),std::ios::sync_with_stdio(false)

namespace idaten {

	using namespace std;
	using ll = int_fast64_t;
	using ull = uint_fast64_t;
	using P = std::pair<int64_t, int64_t>;
	template<typename T1, typename T2> inline bool chmin(T1& a, T2 b) { if (a > b) { a = b; return 1; }return 0; }
	template<typename T1, typename T2> inline bool chmax(T1& a, T2 b) { if (a < b) { a = b; return 1; }return 0; }
	template<class Type>struct is_vector : std::false_type {};
	template<class ValueType, class Alloc>struct is_vector<std::vector<ValueType, Alloc>> : std::true_type {};
	template <typename T> inline std::ostream& operator << (std::ostream& out, const std::vector<T>& v) {
		if (v.empty())return out;
		constexpr bool is_vector_v = is_vector<T>::value;
		if (is_vector_v)for (auto itr = v.begin(); itr != v.end();)out << (*itr), out << ((++itr != v.end()) ? "\n" : "");
		else for (auto itr = v.begin(); itr != v.end();)out << (*itr), out << ((++itr != v.end()) ? " " : "");
		return out;
	}
	inline void put() {}
	template<class T> inline void put(const T& first) { std::cout << first << "\n"; }
	template<class T, class... N> inline void put(const T& first, const N& ... rest) { std::cout << first << " "; put(rest...); }
	inline void putn() {}
	template<class T, class... N> inline void putn(const T& first, const N& ... rest) { std::cout << first << "\n"; putn(rest...); }

	void solve() {
		IOS;
		int n, m;
		cin >> n >> m;
		while (n + m != 0) {
			vector<int> c(m), x(n);
			REP(i, m) {
				cin >> c[i];
			}
			REP(i, n) {
				cin >> x[i];
			}
			vector<vector<ll>> dp(n + 1, vector<ll>(256, LLONG_MAX));
			dp[0][128] = 0;
			REP(i, n) {
				REP(j, 256) {
					if (dp[i][j] != LLONG_MAX) {
						REP(k, m) {
							int t = j + c[k];
							if (t < 0)t = 0;
							if (255 < t)t = 255;
							chmin(dp[i + 1][t], dp[i][j] + (x[i] - t) * (x[i] - t));
						}
					}
				}
			}
			ll res = LLONG_MAX;
			REP(j, 256) {
				chmin(res, dp[n][j]);
			}
			put(res);
			cin >> n >> m;
		}
		
	}
} // namespace idaten

int main() {
	idaten::solve();
	return 0;
}

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

using namespace std;

int square(int x) {
	return x * x;
}

int main() {
	int n, m; 
	while (cin >> n >> m, n) {
		vector<int> c(m), x(n);
		for (int i = 0; i < m; ++i) cin >> c[i];
		for (int i = 0; i < n; ++i) cin >> x[i];
		vector<vector<int> > dp(n + 1, vector<int>(256, 1e9));
		dp[0][128] = 0;
		for (int i = 0; i < n; ++i) {
			for (int j = 0; j < m; ++j) {
				for (int k = 0; k <= 255; ++k) {
					int next = min(255, max(0, k + c[j]));
					dp[i + 1][next] = min(dp[i + 1][next], dp[i][k] + square(next - x[i]));
				}
			}
		}
		int ans = 1e9;
		for (int i = 0; i <= 255; ++i) ans = min(ans, dp[n][i]);
		cout << ans << endl;
	}

	return 0;
}

C++: #include<iostream>
#include<algorithm>
using namespace std;
int dp[256][20001];
main()
{
	int n,m;
	while(cin>>n>>m,n)
	{
		int c[16];
		for(int i=0;i<m;i++)cin>>c[i];
		for(int i=0;i<256;i++)for(int j=0;j<=n;j++)dp[i][j]=1<<28;
		dp[128][0]=0;
		for(int i=1;i<=n;i++)
		{
			int a;cin>>a;
			for(int j=0;j<256;j++)
			{
				for(int k=0;k<m;k++)
				{
					int u=j+c[k];
					if(u<0)u=0;
					if(u>=256)u=255;
					dp[u][i]=min(dp[u][i],(a-u)*(a-u)+dp[j][i-1]);
				}
			}
		}
		int mindp=1<<28;
		for(int i=0;i<256;i++)mindp=min(mindp,dp[i][n]);
		cout<<mindp<<endl;
	}
}

C++: #include<stdio.h>
#include<algorithm>
using namespace std;
int dp[2][256];
int c[16];
int d[20000];
int M(int a){return a*a;}
int main(){
	int inf=999999999;
	int a,b;
	while(scanf("%d%d",&a,&b),a){
		for(int i=0;i<b;i++)scanf("%d",c+i);
		for(int i=0;i<a;i++)scanf("%d",d+i);
		for(int i=0;i<2;i++)for(int j=0;j<256;j++)dp[i][j]=inf;
		dp[0][128]=0;
		for(int i=0;i<a;i++){
			for(int j=0;j<256;j++)dp[(i+1)%2][j]=inf;
			for(int j=0;j<256;j++){
				if(dp[i&1][j]==inf)continue;
				for(int k=0;k<b;k++)dp[!(i&1)][max(min(j+c[k],255),0)]=min(dp[!(i&1)][max(min(j+c[k],255),0)],dp[i&1][j]+M(d[i]-(max(min(j+c[k],255),0))));
			}
		}
		int ret=999999999;
		for(int i=0;i<256;i++)ret=min(ret,dp[a&1][i]);printf("%d\n",ret);
	}
}