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<string.h>
using namespace std;

int cut(int n){
	if(n<0) return 0;
	if(n>255) return 255;
	return n;
}

		int dp[20001][256];
int main()
{
	int N,M;
	while(cin>>N>>M && N!=0){
		int book[16],signal[20000];
		memset(book,-1,sizeof(bool));
		memset(signal,-1,sizeof(signal));

		for(int i=0;i<M;i++)
			cin>>book[i];
		for(int i=0;i<N;i++)
			cin>>signal[i];


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

		for(int i=0;i<N;i++){
			for(int j=0;j<=255;j++){
				if(dp[i][j]<0) continue;
				for(int k=0;k<M;k++){
					if(dp[i+1][cut(j+book[k])]<0)
						dp[i+1][cut(j+book[k])]=dp[i][j]+(signal[i]-cut(j+book[k]))*(signal[i]-cut(j+book[k]));
					else dp[i+1][cut(j+book[k])]=min(dp[i+1][cut(j+book[k])],dp[i][j]+(signal[i]-cut(j+book[k]))*(signal[i]-cut(j+book[k])));
					
				}
			}
		}
		int ans=100000000;

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

C++: #include <iostream>
#include <algorithm>
#include <cstdio>
#include <string>
#include <cstring>
#include <cctype>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <set>
#include <queue>
#include <stack>
#include <map>
#include <numeric>
using namespace std;
 
#define REP(i, s, e) for (int i = (s); i < (e); i++)
#define REPI(i, s, e) for (int i = (s); i <= (e); i++)
#define rep(i, n) REP(i, 0, n)
#define repi(i, n) REPI(i, 0, n)
#define ALL(v) (v).begin(), (v).end()
 
#define dump(x) (cout << #x << " = " << x << endl)
#define dump2(x, y) (cout << "(" << #x << ", " << #y << ") = (" << x << ", " << y << ")" << endl)
#define dump3(x, y, z) (cout << "(" << #x << ", " << #y << ", " << #z << ") = (" << x << ", " << y << ", "<< z << ")" << endl)
 
typedef long long ll;
typedef pair<int, int> pii;

int N, M;
vector<int> cs, xs;

const int INF = 1<<29;

const int MAX_N = 20010;

int dp[MAX_N][256];

inline int dbl(int n) { return n*n; }

int main()
{
	while (cin >> N >> M, N|M) {
		cs = vector<int>(M);
		xs = vector<int>(N);

		rep(i, M) cin >> cs[i];
		rep(i, N) cin >> xs[i];

		rep(i, MAX_N) rep(j, 256) dp[i][j] = INF;
		dp[0][128] = 0;
		for (int i = 1; i <= N; i++) {
			for (int u = 0; u <= 255; u++) {
				if (dp[i-1][u] == INF) continue;
				for (int j = 0; j < M; j++) {
					int t = u + cs[j];
					t = t<0?0:t>255?255:t;
					dp[i][t] = min(dp[i][t], dp[i-1][u]+dbl(t-xs[i-1]));
				}
			}
		}
		cout << *min_element(dp[N], dp[N]+256) << endl;
	}
	return 0;
}

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

using namespace std;
using ll=long long;
using ld=long double;
using P=pair<ll,ll>;
#define FOR(i,n,m) for(ll i=n; i<(ll)m;i++)
#define FORr(i,n,m) for(ll i=n; i>(ll)m; i--)
#define FORm(i,m) for(auto i=m.begin();i!=m.end();i++)
#define VEC2(x,a,b) vector<vector<ll>> x = vector<vector<ll>>(a,vector<ll>(b,0))
#define sortAl(v) sort(v.begin(),v.end())
#define sortAlr(v) sort(v.begin(),v.end()); reverse(v.begin(),v.end())
#define cinv(v,n,m) FOR(i,n,m) cin>>v.at(i)
#define coutv(v,n,m) FOR(i,n,m) cout<<v.at(i) <<" "; cout<<endl
#define cout(n) cout<<fixed<<setprecision(n)
int main(){
  cin.tie(0);
  ios::sync_with_stdio(false);

  ll n,m;
  cin>>n>>m;
  while(n!=0){
    //dp初期化
    VEC2(dp,n+1,256);
    FOR(i,0,n+1) FOR(j,0,256) dp[i][j] = 1e12;
    dp[0][128] = 0;

    vector<ll> code(m);
    vector<ll> pls(n);
    cinv(code,0,m);
    cinv(pls,0,n);
    FOR(i,0,n){
      FOR(j,0,256){
        FOR(k,0,code.size()){
          ll pos = j + code[k];
          if(pos<0) pos=0;
          if(pos>255) pos=255;
          ll x = dp[i][j] + ( pls[i] - pos )*( pls[i] - pos );
          dp[i+1][pos] = min(dp[i+1][pos], x);

        }
      //cout<<"i = "<< i <<"j = "<<j-255 <<" " <<dp[i][j]<<endl;
      }
    }
    ll res = 1e15;
    FOR(i,0,256){
      res = min(dp[n][i], res);
    }
    cout<<res<<endl;
    cin>>n>>m;
  }

}

C++: #include <bits/stdc++.h>
 
#define loop(n, i) for(int i=0;i<n;i++)
#define all(v) v.begin(),v.end()

using namespace std;
typedef long long ll;
int dp[20010][260];

int main()
{
    while (1) {
        int n, m; cin >> n >> m;
        if (!n) break;
        vector<int> C(m), x(n+1, 128);
        loop (m, i) cin >> C[i];
        loop (n, i) cin >> x[i+1];
        fill(dp[0], dp[20009]+260, 1<<29);
        loop (256, i) dp[n+1][i] = 0;

        for (int i = n; i >= 0; i--) {
            for (int j = 0; j < 256; j++) {
                int d = (x[i] - j) * (x[i] - j);
                for (int c : C) {
                    int k = j + c;
                    k = max(k, 0);
                    k = min(k, 255);
                    dp[i][j] = min(dp[i][j], dp[i+1][k] + d);
                }
            }
        }
        cout << dp[0][128] << endl;
    }
    return 0;
}