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

#define pb(n) push_back(n)
#define fi first
#define se second
#define all(r) (r).begin(),(r).end()
#define gsort(st,en) sort((st),(en),greater<int>())
#define vmax(ary) *max_element(all(ary))
#define vmin(ary) *min_element(all(ary))
#define debug(x) cout<<#x<<": "<<x<<endl
#define fcout(n) cout<<fixed<<setprecision((n))
#define scout(n) cout<<setw(n)
#define vary(type,name,size,init) vector< type> name(size,init)

#define rep(i,n) for(int i = 0; i < (int)(n);++i)
#define REP(i,a,b) for(int i = (a);i < (int)(b);++i)
#define repi(it,array) for(auto it = array.begin(),end = array.end(); it != end;++it)
#define repa(n,array) for(auto &n :(array))

using ll = long long;
using vi = vector<int>;
using vl = vector<ll>;
using dict = map<string,int>;
using pii = pair<int,int> ;
using pll = pair<ll,ll> ;

const int mod = 1000000007;
constexpr int imax = ((1<<30)-1)*2+1 ;
constexpr int inf = 100000000;
constexpr double PI = acos(-1.0) ;
double eps = 1e-10 ;
const int dy[] = {-1,0,1,0};
const int dx[] = {0,-1,0,1};

inline bool value(int x,int y,int w,int h){
  return (x >= 0 && x < w && y >= 0 && y < h);
}

template<typename T>
void Unique(vector<T> &v){
  sort(all(v));
  v.erase(unique(all(v)),v.end());
}

template<typename T>
T ston(string& str, T n){
  istringstream sin(str) ;
  T num ;
  sin >> num ;
  return num ;
}

void Ans(bool f){
  if(f) cout << "YES"<<endl;
  else cout << "NO"<<endl;
}

int main(){
  cin.tie(0);
  ios::sync_with_stdio(false);
  ll n,m;
  while(cin >> n >> m&&n && m){
    vary(ll,c,m,0);
    vary(ll,x,n+1,0);
    rep(i,m){
      cin >> c[i];
    }
    rep(j,n){
      cin >> x[j+1];
    }
    vector<vector<ll>> dp(n+1 ,vector<ll>(260,inf));
    dp[0][128] = 0;
    REP(i,1,n+1){
      rep(j,256){
        rep(k,m){
          ll C = j+c[k];
          if(C > 255) C = 255;
          if(C < 0) C = 0;
          dp[i][C] = min(dp[i][C], dp[i-1][j]+(ll)pow(x[i] - C,2));
        }
      }
    }
    ll ans = inf;
    rep(i,256){
      ans = min(ans,dp[n][i]);
    }
    cout << ans << endl;
  }
  return 0;
}

C++: #include <iostream>
#include <vector>
#include <cmath>
#define INF INT32_MAX

using namespace std;

int main(void) {
	cout << INF << endl;
	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;
}

C++: #include<iostream>
#include<algorithm>
#define INF 1145141919
using namespace std;
int n,m;
int dp[2][256],c[16];
int main(){
	while(cin>>n>>m&&n+m){
	for(int i=0;i<m;i++)cin>>c[i];
	for(int i=0;i<256;i++)dp[0][i]=INF,dp[1][i]=INF;
	dp[0][128]=0;
	for(int i=0;i<n;i++){
		int x;cin>>x;bool t=i%2;
		for(int j=0;j<256;j++){
			for(int k=0;k<m;k++){
				int y=j+c[k];if(y<0)y=0;if(y>255)y=255;
				int z=(x-y)*(x-y)+dp[t][j];
				dp[1-t][y]=min(dp[1-t][y],z);
			}
		}
		for(int j=0;j<256;j++)dp[t][j]=INF;
	}
	int ans=INF;
	for(int i=0;i<256;i++){
		ans=min(ans,dp[n%2][i]);
	}
	cout<<ans<<endl;
	}
	return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;

int main()
{
    while (1)
    {
        int N, M;
        cin >> N >> M;
        if (N == 0 || M == 0)
        {
            break;
        }
        long long C[M];
        for (int i = 0; i < M; i++)
        {
            cin >> C[i];
        }
        long long x[N];
        for (int i = 0; i < N; i++)
        {
            cin >> x[i];
        }
        vector<vector<long long>> dp(N + 1, vector<long long>(256, 100000000000));
        dp[0][128] = 0;
        for (int i = 0; i < N; i++)
        {
            for (int j = 0; j <= 255; j++)
            {
                for (int k = 0; k < M; k++)
                {
                    long long tmp = x[i] - j - C[k];
                    if (j + C[k] < 0)
                    {
                        tmp = x[i];
                        dp[i + 1][0] = min(dp[i + 1][0], dp[i][j] + tmp * tmp);
                    }
                    else if (j + C[k] > 255)
                    {
                        tmp = x[i] - 255;
                        dp[i + 1][255] = min(dp[i + 1][255], dp[i][j] + tmp * tmp);
                    }
                    else
                    {
                        dp[i + 1][j + C[k]] = min(dp[i + 1][j + C[k]], dp[i][j] + tmp * tmp);
                    }
                }
            }
        }
        long long ans = 100000000000;
        for (int i = 0; i <= 255; i++)
        {
            ans = min(ans, dp[N][i]);
        }
        cout << ans << endl;
    }
}