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 INF (1 << 25)

int dp[20001][256];


int main()
{
  int N, M;
  while(cin >> N >> M, N || M){
    int C[16], x[20000];
    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 < 20001; i++){
      for(int j = 0; j < 256; j++) dp[i][j] = INF;
    }
    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 = 0; k < M; k++){
	  int y = j + C[k];
	  if(y < 0) y = 0;
	  else if(y > 255) y = 255;
	  int sqsum = (x[i] - y)*(x[i] - y);
	  dp[i+1][y] = min(dp[i+1][y], dp[i][j] + sqsum);	  
	}
      }
    }
    cout << *min_element(dp[N], dp[N] + 256) << endl;
  }
  return 0;
}

C++: #include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef long double ld;
#define MOD 998244353
template<class T>inline bool chmax(T& a,T b){if (a<b){a=b;return 1;}return 0;}
template<class T>inline bool chmin(T& a,T b){if (a>b){a=b;return 1;}return 0;}
const long long INF = 1LL << 60;
bool pairCompare(const pair<double,ll>& firstElof, const pair<double,ll>& secondElof){
    return firstElof.first < secondElof.first;
}
bool pairCompareSecond(const pair<double,ll>& firstElof, const pair<double,ll>& secondElof){
    return firstElof.second < secondElof.second;
}
//四方向への移動ベクトル
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};

struct edge{//グラフに使うヤツ
    ll from,to,cost;
};

typedef vector<vector<edge> > G;
ll gcd(ll a,ll b){
    if (a%b==0)return(b);
    else return(gcd(b,a%b));
}

int main() {
    while(true){
        ll n,m;
        cin >> n >> m;
        if (n+m==0)break;
        ll c[m],x[n];
        for (ll i=0;i<m;i++){
            cin >> c[i];
        }
        for (ll i=0;i<n;i++){
            cin >> x[i];
        }
        ll dp[n+1][260];
        for (ll i=0;i<=n;i++){
            fill(dp[i],dp[i]+260,INF);
        }
        dp[0][128]=0;
        for (ll i=1;i<=n;i++){
            for (ll j=0;j<=255;j++){
                for (ll k=0;k<m;k++){
                    ll num=max(0LL,min(255LL,j+c[k]));
                    dp[i][num]=min(dp[i][num],dp[i-1][j]+(x[i-1]-num)*(x[i-1]-num));
                }
            }
        }
        ll ans=INF;
        for (ll i=0;i<=255;i++){
            ans=min(dp[n][i],ans);
        }
        cout << ans << endl;
    }
    return 0;
}

C++: #include<iostream>
#include<vector>
#include<algorithm>

using namespace std;

const int INFTY = 1000000000;

int main() {
    int N, M;
    cin >> N >> M;
    while(N!= 0){
        vector<int> cvect(M),xvect(N);
        for(int i=0; i< M ;i++){
            cin >> cvect[i];
        }
        for(int i=0; i< N ; i++){
            cin >> xvect[i];
        }
        vector<int> dp(256, INFTY), dp_old(256, INFTY);
        dp_old[128]=0;
        int tmp;
        for(int i=0; i<N; i++){
            vector<bool> check(256, false);
            for(int j=0; j<256 ; j++){
                dp[j] = INFTY;
            }
            for(int j=0; j<256 ; j++){
                for(int k=0; k< M; k++){
                    tmp = min(max(0, j+cvect[k]),255);
                    if(check[tmp]){
                        dp[tmp] = min(dp[tmp], dp_old[j] + (tmp-xvect[i])*(tmp-xvect[i]));
                    }else{
                        dp[tmp] = dp_old[j] + (tmp-xvect[i])*(tmp-xvect[i]);
                        check[tmp] = true;
                    }
                }
            }
            swap(dp,dp_old);
        }
        int ans=INFTY;
        for(int i=0; i<256 ; i++){
            ans= min(ans, dp_old[i]);
        }
        cout << ans << endl;
        cin >> N >> M;
    }
    return 0;
}

C++: #include<cstdlib>
#include<algorithm>

using namespace std;

const long long inf=1LL<<60;
long long memo[20000][256];

int main()
{
	for(int n,m;scanf("%d%d",&n,&m),n;){
		int c[16];
		static int x[20001];
		for(int i=0;i<m;i++)	scanf("%d",c+i);
		for(int i=0;i<n;i++)	scanf("%d",x+i);

		for(int i=0;i<=n;i++)for(int j=0;j<256;j++)	memo[i][j]=inf;

		memo[0][128]=0;
		for(int i=1;i<=n;i++){
			for(int j=0;j<256;j++){
				if(memo[i-1][j]==inf)	continue;
				for(int k=0;k<m;k++){
					int next=min(255,max(0,j+c[k]));
					memo[i][next]=min(memo[i][next],
						memo[i-1][j]+(next-x[i-1])*(next-x[i-1]));
				}
			}
		}

		long long emin=inf;
		for(int j=0;j<256;j++)	emin=min(emin,memo[n][j]);
		printf("%lld\n",emin);
	}

	return 0;
}