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<(N);i++)
#define FOR(i,a,b) for(int i=(a);i<(b);i++)
using namespace std;
const long long MOD = 1e9 + 7;
const long long INF = 1e12;
const int inf = 1e9;
typedef long long ll;
typedef pair<ll,int> P;
typedef set<int> S;
int n,m;
vector<int> c(20,0),x(20010,0),v;
int dp[20010][260];
int main(){
    while(1){
        cin>>n>>m;
        if(n==0 && m==0) break;
        v.clear();
        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<20010;i++){
            for(int j=0;j<260;j++){
                dp[i][j]=inf;
            }
        }
        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++){
                    if(j+c[k]>255){
                        int next=255;
                        dp[i+1][next]=min(dp[i+1][next],dp[i][j]+(next-x[i])*(next-x[i]));
                    }
                    else if(j+c[k]<0){
                        int next=0;
                        dp[i+1][next]=min(dp[i+1][next],dp[i][j]+(next-x[i])*(next-x[i]));
                    }
                    else{
                        int next=j+c[k];
                        dp[i+1][next]=min(dp[i+1][next],dp[i][j]+(next-x[i])*(next-x[i]));
                    }
                }
            }
        }
        int min=inf;
        for(int j=0;j<256;j++){
            if(dp[n][j]<min) min=dp[n][j];
        }
        cout<<min<<endl;
    }
    return 0;
}

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

#define rep(i, n) for(int i = 0; i < n; i++)
using namespace std;

typedef long long ll;

const int INF = 1 << 30;
const ll LLINF = 1LL << 62;

int mod = 1000000007;

int dp[20001][256];

void init(int N){
    rep(i, N+1) rep(j, 256) dp[i][j] = mod;
}

int main(void){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    vector<int> ans;
    while(true){
        int N, M; cin >> N >> M;
        if(N == 0) break;
        vector<int> code(M); rep(i, M) cin >> code[i];
        vector<int> X(N); rep(i, N) cin >> X[i];
        init(N);

        dp[0][128] = 0;
        rep(i, N){
            rep(j, 256){
                if(dp[i][j] != mod){
                    rep(k, M){
                        int to = j + code[k];
                        if(to < 0) to = 0; if(to > 255) to = 255;
                        dp[i+1][to] = min(dp[i+1][to], dp[i][j] + (X[i]-to)*(X[i]-to));
                    }
                }
            }
        }
        int res = mod;
        rep(j, 256) res = min(res, dp[N][j]);
        ans.push_back(res);
    }
    rep(i, ans.size()) cout << ans[i] << endl;
    return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
const int MAX_N=2e4+10,MAX_M=20,MAX=256,INF=1e9;

int N,M,nxt,ans;
int C[MAX_M],x[MAX_N];
int dp[MAX_N][MAX];

void solve(){
    for (int i=0;i<MAX_N;++i)
        for (int j=0;j<MAX;++j)
            dp[i][j]=INF;
    dp[0][128]=0;
    for (int i=0;i<N;++i){
        for (int j=0;j<MAX;++j){
            for (int k=0;k<M;++k){
                nxt=max(0,min(MAX-1,j+C[k]));
                dp[i+1][nxt]=min(dp[i+1][nxt],dp[i][j]+(x[i]-nxt)*(x[i]-nxt));
            }
        }
    }
    ans=INF;
    for (int j=0;j<MAX;++j) ans=min(ans,dp[N][j]);
    cout << ans << '\n';
}

int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    while(cin >> N >> M,N){
        for (int i=0;i<M;++i) cin >> C[i];
        for (int i=0;i<N;++i) cin >> x[i];
        solve();
    }
}

C++: #include <iostream>

using namespace std;

int main() {
 int n,m;
 while(cin >> n >> m, n) {
  vector<int> code(m), value(n);
  for (int i = 0; i < m; i++)
   cin >> code[i];
  for (int i = 0; i < n; i++)
   cin >> value[i];
  vector<vector<int> > dp(256, vector<int>(n + 1, 1000000000));
  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 val = min(255,max(0,j+code[k]));
     dp[i+1][val] = min(dp[i+1][val], dp[i][j] + (val -value[i]) * (val - value[i]));
    }
   }
  }
  int ans = 1000000000;
  for (int i = 0; i < 256; i++) {
   ans = min(ans, dp[n][val]);
  }
  cout << ans << endl;
 }
 return 0;
}