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 all(x) (x).begin(), (x).end()
typedef long long ll;
#define MOD 1000000007
using namespace std;
int main() {
    while(true) {
        int n, m;
        cin >> n >> m;
        if(n == 0 && m == 0) {
            return 0;
        }
        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<ll> > dp(n + 1, vector<ll>(256, LLONG_MAX / 3));
        dp[0][128] = 0;
        for(int i = 0; i < n; i++) {
            for(int j = 0; j < 256; j++) {
                for(int l = 0; l < m; l++) {
                    ll next = min(max(j + c[l], 0), 255);
                    dp[i + 1][next] =
                        min(dp[i + 1][next],
                            dp[i][j] + (next - x[i]) * (next - x[i]));
                }
            }
        }
        // cout << dp[1][132] << endl;
        cout << *min_element(all(dp[n])) << endl;
    }
}

C++: #include<iostream>
#include<vector>
#include<algorithm>
#include<utility>
#include<string>
#include<cmath>
#include<cstring>
#include<queue>
#include<map>
#include<climits>
#include<set>

#define REP(i, n) for(int i = 0;i < n;i++)
#define REPR(i, n) for(int i = n;i >= 0;i--)
#define FOR(i, m, n) for(int i = m;i < n;i++)
#define FORR(i, m, n) for(int i = m;i >= n;i--)
#define SORT(v, n) sort(v, v+n);
#define VSORT(v) sort(v.begin(), v.end());
#define llong long long
#define pb(a) push_back(a)
using namespace std;
typedef pair<int, int> pii;
typedef long long int ll;
typedef pair<ll, ll> pll;
int dx[4] = { 1,0,0,-1 };
int dy[4] = { 0,1,-1,0 };
#define MOD 1000000007
#define ARRAY_MAX 100005

const int INF = 1e9 + 7;


int dp[20005][300];//dp[i][j]:=i番目までのみて信号の値がjの場合の二乗和の最小値

int main() {

	
	int n, m;
	ios::sync_with_stdio(false);
	cin.tie(0);

	while (cin >> n >> m, n | m)
	{
		vector<int> C(m + 10);
		vector<int> x(n + 10);
		for (int i = 1; i <= m; i++)
		{
			cin >> C[i];
		}
		for (int i = 0; i < n; i++)
		{
			cin >> x[i];
		}
		int mini = INF;

		memset(dp, INF, sizeof(dp));
		dp[0][128] = 0;

		for (int i = 0; i < n; i++)
		{
			for (int j = 0; j < 256; j++)
			{
				if (dp[i][j] == INF)
				{
					continue;
				}
				for (int k = 1; k <= m; k++)
				{
					int code = max(0, min(255,j + C[k]));//次のコード

					dp[i + 1][code] = min(dp[i+1][code], dp[i][j] + (x[i]-code)*(x[i]-code));
				}
			}
		}
		int ans = INF;
		
		for (int i = 0; i <= 256; i++)
		{
			ans = min(ans, dp[n][i]);
		}
		cout << ans << endl;	

	}

	return 0;
}

C++: #include <iostream>
#define REP(i, a, n) for(int i = ((int) a); i < ((int) n); i++)
#define INF 100000000
using namespace std;
typedef long long ll;

int N, M, C[16], X[20000];
ll dp[20000][256];

int main(void) {
  while(cin >> N >> M, N || M) {
    REP(i, 0, M) cin >> C[i];
    REP(i, 0, N) cin >> X[i];

    REP(i, 0, N + 1) REP(j, 0, 256) dp[i][j] = INF;
    dp[0][128] = 0;

    REP(i, 0, N) REP(j, 0, 256) if(dp[i][j] < INF) REP(k, 0, M) {
      ll y = max(0, min(255, j + C[k]));
      dp[i + 1][y] = min(dp[i + 1][y], dp[i][j] + (y - X[i]) * (y - X[i]));
    }

    ll ans = INF;
    REP(i, 0, 256) ans = min(ans, dp[N][i]);
    cout << ans << endl;
  }
}

C++: #include <vector>
#include <iostream>
#include <utility>
#include <cmath>

using namespace std;

const long long INF = 1e15;
const long long Y = 128;
int main(){
	long long n,m;
	while(cin>>n>>m,n){
		vector<long long> cod(m),signal(n+1);
		vector<vector<pair<long long,long long>>> dp(n+1,vector<pair<long long,long long>>(m,make_pair(INF,INF)));
		double ave_signal = 0;
		for(int i=0;i<m;i++){//code input
			cin >> cod[i];
		}
		for(int i=1;i<n+1;i++){//signal input
			cin >> signal[i];
			ave_signal += signal[i];
		}
		ave_signal /= n;
		
		for(int i=0;i<m;i++){//?????????input
			dp[0][i].first = Y;
			dp[0][i].second = 0;
			//cout << "(" << dp[0][i].first << ", " << dp[0][i].second << ")";
		}
			//cout << endl;

		for(int i=1;i<n+1;i++){//dp
			for(int j=0;j<m;j++){//??\????????±???
				int min_line = ave_signal;
				int max_line = signal[i];
				if(ave_signal > signal[i]) swap(min_line,max_line); 
					bool is_bad = true;
				for(int k=0;k<m;k++){//?????¶??±
					int tmp_signal = dp[i-1][k].first + cod[j];
					if(tmp_signal>255)tmp_signal = 255;
					else if(tmp_signal<0)tmp_signal = 0;

						if(dp[i-1][k].second + (signal[i]  - tmp_signal)*(signal[i]  - tmp_signal) < dp[i][j].second){
							is_bad = false;
							dp[i][j].second = dp[i-1][k].second + (signal[i]  - tmp_signal)*(signal[i]  - tmp_signal);
							dp[i][j].first = tmp_signal;
						
					}else if(is_bad){
						dp[i][j].second = dp[i-1][k].second + (signal[i]  - tmp_signal)*(signal[i]  - tmp_signal);
						dp[i][j].first = tmp_signal;
					}
				}
//				cout << "(" << dp[i][j].first << ", " << dp[i][j].second << ")";
			}
			//cout << endl;
		}
		long long ans = INF;
		for(int i=0;i<m;i++)
			ans = min(ans,dp[n][i].second);
		cout << ans << endl;
	}
	return 0;
}