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 <vector>
#include <string>
#include <stack>
#include <queue>
#include <deque>
#include <map>
#include <set>
#include <tuple>
#include <bitset>
#include <memory>
#include <cmath>
#include <algorithm>
#include <functional>
#include <iomanip>
#include <numeric>
#include <climits>
#include <cfloat>
void change_to_min(int& target, const int suspect) {
	if (target > suspect) target = suspect;
}
int square(const int value) { return value * value; }
int cal_min_diff(const std::vector<int>& signal, const std::vector<int>&code, const int current_pos, const int prev_value, std::vector<std::vector<int>>& memo) {
	if (current_pos == signal.size()) return 0;
	if (memo[current_pos][prev_value] >= 0) return memo[current_pos][prev_value];
	memo[current_pos][prev_value] = INT_MAX;
	for (const auto c : code) {
		if (prev_value + c < 0) change_to_min(memo[current_pos][prev_value], cal_min_diff(signal, code, current_pos + 1, 0, memo) + square(signal[current_pos]));
		else if (255 < prev_value + c) change_to_min(memo[current_pos][prev_value], cal_min_diff(signal, code, current_pos + 1, 255, memo) + square(255 - signal[current_pos]));
		else change_to_min(memo[current_pos][prev_value], cal_min_diff(signal, code, current_pos + 1, prev_value + c, memo) + square(signal[current_pos] - prev_value - c));
	}
	return memo[current_pos][prev_value];
}
int main() {
	while (true) {
		int n, m; std::cin >> n >> m; if (n == 0 && m == 0) return 0;
		std::vector<int> code_book(m); for (auto& c : code_book) std::cin >> c;
		std::vector<int> signal(n); for (auto& s : signal) std::cin >> s;
		std::vector<std::vector<int>>memo(n, std::vector<int>(256, -1));
		std::cout << cal_min_diff(signal, code_book, 0, 128, memo) << std::endl;
	}
}

C++: #include<iostream>
#include<cmath>
#include<algorithm>
#include<string>
#include<vector>
#include<stack>
#include<queue>
#include<set>
#include<map>
#include<iomanip>
#include<bitset>
using namespace std;
typedef long long ll;
typedef pair<ll,ll> P;
typedef pair<ll,P> P1;
typedef pair<P,P> P2;
#define pu push
#define pb push_back
#define mp make_pair
#define eps 1e-7
#define INF 1000000000
#define fi first
#define sc second
#define rep(i,x) for(ll i=0;i<x;i++)
#define repn(i,x) for(ll i=1;i<=x;i++)
#define SORT(x) sort(x.begin(),x.end())
#define ERASE(x) x.erase(unique(x.begin(),x.end()),x.end())
#define POSL(x,v) (lower_bound(x.begin(),x.end(),v)-x.begin())
#define POSU(x,v) (upper_bound(x.begin(),x.end(),v)-x.begin())

const int MAX = 21000;
const int MOD = 1000000007;

ll dp[MAX][260];
// dp[i][j] は i個までみた時 現在 j の最小値

int main(){
  vector<ll> ans;
  while(1){
    ll N ,M; cin >> N >> M;
    if(N == 0 and M == 0) break;
    vector<ll> c(M); rep(i,M) cin >> c[i];
    vector<ll> x(N); rep(i,N) cin >> x[i];
    // dp table 初期化
    rep(i,MAX) rep(j,260) dp[i][j] = INF;
    dp[0][128] = 0;
    rep(i,N){
      rep(j,256){
        rep(k,M){
          // from j to j + c[k]をそれぞれ追っていく
          if(j + c[k] < 0){
            dp[i + 1][0] = min(dp[i + 1][0] , dp[i][j] + x[i] * x[i]);
          }
          else if(j + c[k] > 255){
            dp[i + 1][255] = min(dp[i + 1][255] , dp[i][j] + (255 - x[i]) * (255 - x[i]) );
          }
          else{
            dp[i + 1][j + c[k]] = min(dp[i + 1][j + c[k]] , dp[i][j] + (j + c[k] - x[i]) * (j + c[k] - x[i]) );
          }
        }
      }
    }
    // rep(i,N + 1) for(ll j = 255; j < 256; j++) cout << i << " " << j << " " <<  dp[i][j] << endl;

    ll tmp = INF;
    rep(j,256) tmp = min(tmp , dp[N][j]);
    ans.pb(tmp);
  }
  rep(i,ans.size()) cout << ans[i] << endl;
}

C++: #include <iostream>
#include <vector>
#include <cmath>
#define INF (1<<21)

using namespace std;

int main(void) {
	int N,M;
	while (cin >> N >> M, N) {
		vector<int> C(M);
		for (int i = 0; i < M; ++i) {
			cin >> C[i];
		}
		vector<int> x(N);
		for (int i = 0; i < N; ++i) {
			cin >> x[i];
		}

		vector< vector<int> > dp(256,vector<int>(N+1));
		for (int i = 0; i < 256; ++i) {
			for (int j = 0; j < N+1; ++j) {
				dp[i][j] = INF;
			}
		}
		dp[128][0] = 0;

		for (int j = 0; j < N; ++j) {
			for (int i = 0; i < 256; ++i) {
				if (dp[i][j] != INF) {
					for (int k = 0; k < M; ++k) {
						if (C[k]+i > 255) {
							dp[255][j+1] = min(dp[255][j+1],dp[i][j]+(int)pow(x[j]-255,2));
						}
						else if (C[k]+i < 0) {
							dp[0][j+1] = min(dp[0][j+1],dp[i][j]+(int)pow(x[j]-0,2));
						}
						else {
							dp[C[k]+i][j+1] = min(dp[C[k]+i][j+1],dp[i][j]+(int)pow(x[j]-C[k]-i,2));
						}
					}
				}
			}
		}

		int res = INF;
		for (int i = 0; i < 256; ++i) {
			if (dp[i][N] < res) {
				res = dp[i][N];
			}
		}
		cout << res << endl;
	}
	return 0;
}