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;
typedef long long ll;
typedef pair<ll, ll> l_l;
typedef pair<int, int> i_i;

#define EPS (1e-7)
#define INF (1e9)
#define PI (acos(-1))
//const ll mod = 1000000007;
ll C[20];
ll x[100500];
ll dp[100500][300];

void chmin(ll &a, ll b) {
    a = min(a, b);
}
void chmax(ll &a, ll b) {
    a = max(a, b);
}
int main() {
    //cout.precision(10);
    cin.tie(0);
    ios::sync_with_stdio(false);
    while(true) {
        ll N, M;
        cin >> N >> M;
        if(N == 0) break;
        for(int i = 1; i <= M; i++) cin >> C[i];
        for(int i = 1; i <= N; i++) cin >> x[i];
        for(int i = 0; i <= N; i++) {
            for(int val = 0; val <= 255; val++) dp[i][val] = 1e18;
        }
        //for(int val = 0; val <= 255; val++) dp[0][val] = 0;
        dp[0][128] = 0;
        for(int i = 0; i < N; i++) {
            for(int before = 0; before <= 255; before++) {
                for(int j = 1; j <= M; j++) {
                    ll newval = before + C[j];
                    chmax(newval, 0);
                    chmin(newval, 255);
                    chmin(dp[i+1][newval], dp[i][before] + (x[i+1] - newval) * (x[i+1]-newval));
                }
            }
        }
        ll ans = 1e18;
        for(int i = 0; i <= 255; i++) chmin(ans, dp[N][i]);
        cout << ans << endl;
    }
    return 0;
}

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

#define REP(i, n) for(int i=0;i<int(n);++i)
#define FOR(i, a, b) for(int i=int(a);i<int(b);++i)

int dp[2][256];

template <typename T> inline void assign_min(T& x, T v) { if (v < x) x = v; }
template <typename T> inline T square(T x) { return x*x; }

int main() {
    cin.tie(0); ios_base::sync_with_stdio(false);

    int N, M;
    while (cin >> N >> M, N | M) {
        vector<int> codebook(M);
        vector<int> signals(N+1);
        REP(i, M) cin >> codebook[i];
        REP(i, N) cin >> signals[i+1];

        REP(v, 256) dp[0][v] = 1e8;
        dp[0][128] = 0;

        REP(i, N) {
            int curr = i%2;
            int next = (i+1)%2;
            REP(v, 256) dp[next][v] = 1e8;
            REP(v, 256) {
                REP(j, M) {
                    int value = min(max(v + codebook[j], 0), 255);
                    int diff = value - signals[i+1];
                    assign_min(dp[next][value], dp[curr][v] + square(diff));
                }
            }
        }

        cout << *min_element(dp[N%2], dp[N%2]+256) << '\n';
    }
}

C++: #ifdef DEBUG
#include "../utils.cpp"
#endif
#include <bits/stdc++.h>
/*
Problem


Category
DP
 
URL:


考察:



Date: 2020/06/01
*/
 

using namespace std;
using ll = int64_t;
#define int int64_t
#define INF 10e9

 
/* ==== declarations ====== */
// functions
void solve();
void getInput();
 
// global variables
int N, M;
vector<int> C;
vector<int> X;
vector<vector<int>> dp;

//2254
/* ======================== */


void getInput() {
    cin>>N>>M;
    C=vector<int>(M, 0);
    X=vector<int>(N+1, 0);
    dp  =vector<vector<int>>(N+1, vector<int>(256, INF));
    for(int i=0; i<M; ++i) {
        cin>>C[i];
    }
    for(int i=0; i<N; ++i) {
        cin>>X[i];
    }
}
 

void solve() {
    // print(C, X);
    // print(dp);
    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 nextValue = j + C[k];
                nextValue = nextValue < 0 ? 0 : nextValue;
                nextValue = nextValue > 255 ? 255 : nextValue;
                int loss = pow(X[i] - nextValue, 2);
                // print("loss of ", nextValue, "is", loss, X[i]);
                dp[i+1][nextValue] = min(dp[i+1][nextValue], dp[i][j] + loss);
            }
        }
    }
    // print(dp);
    int ans = INF;
    for(auto a: dp[N]) {
        ans = min(ans, a);
    }
    // print(dp);
    cout<<ans<<endl;
}
 
 
signed main(){
    while(true){
        getInput();
        if (N==0 and M==0) break;
        solve();
    }
    return 0;
}

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

int DP[20010][256];
int N, M;
int B[17];
int X[20010];
int intMax = 1000000000;

int solve() {
	
	for(int i = 0; i <= N; i++) for(int j = 0; j <= 255; j++) DP[i][j] = intMax;
	for (int m = 0; m < M; m++) {
		cin >> B[m];
	}
	for (int n = 1; n <= N; n++) {
		cin >> X[n];
	}
	DP[0][128] = 0;
	for (int n = 0; n < N; n++) {
		for (int i = 0; i <= 255; i++) {
			for (int m = 0; m < M; m++) {
				int c = min(255, max(i + B[m], 0));
				int s = X[n + 1] - c;
				s = s * s;
				DP[n + 1][c] = min(DP[n][i] + s, DP[n + 1][c]);
			}
		}
	}
	int ans = intMax;
	for(int i = 0; i <= 255; i++) {
		ans = min(ans, DP[N][i]) ;
	}
	cout << ans << endl;
}

int main() {
	cin >> N >> M;
	while(N) {
		solve();
		cin >> N >> M;
	}
}