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>

using namespace std;

typedef long long ll;

const int MAX_N = 20000;
const int MAX_M = 16;
const ll INF = 1e12;
const int y0 = 128;
int N, M;
int C[MAX_M + 1], x[MAX_N + 1];
ll dp[MAX_N + 10][260];

int main()
{
    while (cin >> N >> M, N || M) {
        // Input
        for (int i = 0; i < M; ++i)
            cin >> C[i];
        for (int i = 0; i < N; ++i)
            cin >> x[i];

        // Init
        for (int i = 0; i <= N; ++i)
            fill(dp[i], dp[i] + 259, INF);
        dp[0][128] = 0;

        // Solve
        for (int n = 0; n < N; ++n) {
            for (int y = 0; y <= 255; ++y) {
                for (int i = 0; i < M; ++i) {
                    int next_y = y + C[i];
                    if (next_y < 0)
                        next_y = 0;
                    else if (next_y > 255)
                        next_y = 255;

                    dp[n + 1][next_y] = min(dp[n + 1][next_y],
                                            dp[n][y] + (x[n] - next_y) * (x[n] - next_y));
                }
            }
        }

        ll ans = INF;
        for (int i = 0; i <= 255; ++i)
            ans = min(ans, dp[N][i]);

        cout << ans << endl;
    }

    return 0;
}

C++: #include <bits/stdc++.h>
//----***やべーやつら***----
using namespace std;
#define int long long

//----***型定義***----
using ll = long long;
using P = pair<int,int>;

//----***Like a Pythonista***----
#define REP(ii,jj,nn) for (ll ii=jj;ii<(nn);ii++)
#define RREP(ii,nn,jj) for (ll ii = nn; jj<ii;ii--)
#define each(i,...) for (auto&& i:__VA_ARGS__)
#define ALL(vec) (vec).begin(),(vec).end()
#define sum(...) accumulate(ALL(__VA_ARGS__),0LL)
#define dsum(...) accumulate(ALL(__VA_ARGS__),0.0)
#define vec(type,name,...) vector<type> name(__VA_ARGS__)
template<class T> inline auto max(const T& a){ return *max_element(ALL(a)); }
template<class T> inline auto min(const T& a){ return *min_element(ALL(a)); }
inline ll gcd(ll a,ll b){if(b == 0) return a;return  gcd(b,a%b);}
inline ll lcm(ll a,ll b){ll g = gcd(a,b);return a / g * b;}

//----***定数***----
#define MOD 1000000007
#define INF 100000000000000000
#define EPS 1e-9
const vector<vector<int>> DXY={{-1,0},{1,0},{0,-1},{0,1}};

//----***パーツ***----
#define fr first
#define se second
#define pb push_back

//----***入出力***---
#define print(out) cout<< out  << "\n";
#define debug(var)  do{std::cerr << #var << " ↓ "<<"\n";view(var);}while(0);
#define dbg cerr<<"🥺🥺🥺🥺🥺🥺"<<endl;
template<typename T> void view(T e){std::cout << e << std::endl;}
template<typename T> void view(const std::vector<T>& v){for(const auto& e : v){ std::cout << e << " "; } std::cout << std::endl;}
template<typename T> void view(const std::vector<std::vector<T> >& vv){ for(const auto& v : vv){ view(v); } }

//----***初期時読み込み***----
struct initial{initial(){cin.tie(0); ios::sync_with_stdio(0); cout<<fixed<<setprecision(15);};}initial_;

vector<vector<int>>dp(30,vector<int>(500,0));

signed main(){
  int N,M;
  while(true){
    cin>>N>>M;
    if(N==0&&M==0)break;
    vector<int> C(M),Y(N);
    REP(i,0,M)cin>>C[i];
    REP(i,0,N)cin>>Y[i];
    vector<vector<int>> dp(256,vector<int>(N+1,INF));
    dp[128][0]=0;
    REP(i,0,N){
      REP(j,0,256){
        REP(k,0,M){
          int idx=C[k];
          idx=max(0LL,min(idx+j,255LL));
          dp[idx][i+1]=min(dp[idx][i+1],(Y[i]-idx)*(Y[i]-idx)+dp[j][i]);
        }
      }
    }
    int ans=INF+1;
    REP(i,0,256)ans=min(ans,dp[i][N]);
    print(ans)
  }

}

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

#define REP(i,k,n) for(int i=k;i<n;i++)
#define rep(i,n) for(int i=0;i<n;i++)
#define INF 1<<30

using namespace std;
typedef long long ll;

int dp[200005][300];

int main()
{
    int n,m;
    while(cin >> n >> m) {
        if(n == 0 && m == 0) break;

        vector<int> c(m);
        rep(i,m) cin >> c[i];

        vector<int> x(n);
        rep(i,n) cin >> x[i];

        rep(i,n+1) rep(j,300) dp[i][j] = INF;

        dp[0][128] = 0;

        REP(i,1,n+1) {
            rep(j,256) {
                if(dp[i-1][j] == INF) continue;

                rep(k,m) {
                    int to = j + c[k];
                    to = max(to,0);
                    to = min(to,255);

                    int ret = (x[i-1]-to) * (x[i-1]-to);
                    dp[i][to] = min(dp[i][to],dp[i-1][j] + ret);
                }
            }
        }

        int ans = INF;
        rep(i,300) ans = min(ans,dp[n][i]);

        cout << ans << endl;
    }

    return 0;
}

C++: #include "bits/stdc++.h"

using namespace std;

long long int N,M,K,L,R,H,W;

const long long int MOD=1000000007;



int main(){
	
	while(cin>>N>>M,N){
		vector<int>v(M);
		for(auto &i:v)cin>>i;
		vector<int>w(N+1);
		for(int i=1;i<=N;i++)cin>>w[i];
		vector<vector<int>>dp(N+1,vector<int>(256,MOD));
		dp[0][128]=0;
		for(int i=0;i<N;i++){
			for(int j=0;j<256;j++){
				for(auto k:v){
					int nx=max(0,min(255,j+k));
					dp[i+1][nx]=min(dp[i+1][nx],dp[i][j]+abs(nx-w[i+1])*abs(nx-w[i+1]));
				}
			}
		}
		cout<<*min_element(dp.back().begin(),dp.back().end())<<endl;
	}
	return 0;
}