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>
#include <cmath>
#include <cstring>
#include <algorithm>
using namespace std;

#define INF (1<<31-1)
int dp[20001][256];

int tw(int n, int c)
{
	return (n-c)*(n-c);
}
int ck(int n)
{
	if(n<=0) return 0;
	if(n>=255) return 255;
	
	return n;	
}

int main()
{
	int N,M, c[16];
	while(cin >> N >> M, (N||M))
	{
		int Lv;
		for(int i=0; i<M; i++)
		cin >> c[i];
		
		for(int i=0; i<=N; i++)
		for(int j=0; j<256; j++)
		{
			dp[i][j] = INF;
		}
		
		dp[0][128] = 0;
		for(int i=0; i<N; i++)
		{
			cin >> Lv;
			
			for(int j=0; j<256; j++)
			{
				if(dp[i][j]!=INF)
				{
					for(int k=0; k<M; k++)
					{
						int nc = ck(j+c[k]);
						dp[i+1][nc] = min(dp[i+1][nc], dp[i][j]+tw(nc,Lv));
					}
				}
			}
		}
		
		int m = INF;
		for(int i=0; i<256; i++)
		{
			m=min(m,dp[N][i]);
		}
		cout << m << endl;
	}
}

C++: #include <iostream>
#include <vector>
using namespace std;
const int MAX = 255;
const unsigned int INF = 1<< 29;
int cal(int f,int s){
  return (f-s)*(f-s);
}
int solve(vector<vector<int> > dp){
  int ans = INF;
  for(int i = 0;i < dp.size();i++){
    ans = min(ans,dp[i][dp[0].size()-1]);
  }
  return ans;   
}
int main(){
  int n,m;
  while(cin >> n >> m,n && m){
    vector<int> c(m,0);
    vector<int> x(n,0);
    for(int i = 0;i < m;i++){
      cin >> c[i];
    }
    for(int i = 0;i < n;i++){
      cin >> x[i];
    }
    vector<vector<int> > dp(MAX+1,vector<int>(n+1,INF));
    dp[128][0] = 0;
    for(int i = 0;i < n;i++){
      for(int j = 0;j <= 255;j++){
	for(int k = 0;k < m;k++){
	  int plus = j+c[k];
	  if(plus < 0)plus = 0;
	  else if(plus > 255)plus = 255;
	  dp[plus][i+1] = min(dp[plus][i+1],dp[j][i]+cal(plus,x[i]));
	}
      }
    }
    /*    for(int j = 0;j <= n;j++){
      for(int i = 118;i <= 136;i++){
	cout << dp[i][j] << " ";
      }
      cout << endl;
      }*/
    cout << solve(dp) << endl;
    
  }
  return 0;
}

C++: #include <iostream>
#include <climits>
#include <algorithm>
using namespace std;
#define INF (1ll<<50)
int main(){
	int C[16];
	int n,m;
	while(cin >> n >> m && n){
		for(int i = 0 ; i < m ; i++) cin >> C[i];
		long long dp[2][256];
		for(int i = 0 ; i < 2 ; i++)
			for(int j = 0 ; j < 256 ; j++)
				dp[i][j] = INF;
		dp[0][128] = 0;
		
		for(int j = 1 ; j <= n ; j++){
			int x; cin >> x;
			for(int i = 0 ; i < 256 ; i++) dp[j&1][i] = INF;
			for(int i = 0 ; i < 256 ; i++){
				for(int k = 0 ; k < m ; k++){
					int y = i + C[k];
					y = min(y,255); y = max(0,y);
					dp[j&1][y] = min(dp[j&1][y],dp[j&1^1][i] + (y-x)*(y-x) );
				}
			}
		}
		cout << *min_element(dp[n&1],dp[n&1]+256) << endl;
	}
}

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

using namespace std;
using LL = long long;
using P = pair<int, int>;
using Tapris = tuple<int, int, int>;

#define REP(i, n) for(LL i = 0; i < n; ++i)
#define FOR(i, a, n) for(LL i = a; i < n; ++i)
#define pb(a) push_back(a)
#define all(x) (x).begin(),(x).end()

const int INF = (int)1e9;
const LL INFL = (LL)1e15;
const int MOD = 1e9 + 7;

int dy[]={0, 0, 1, -1, 0};
int dx[]={1, -1, 0, 0, 0};

//#define int long long

/*************** using variables ***************/
int n, m;
vector<int> c, x;
LL dp[20005][550];
/**********************************************/

signed main(){
    while(cin >> n >> m, n != 0 || m != 0){
        c.resize(m);
        x.resize(n);
        REP(i, m) cin >> c[i];
        REP(i, n) cin >> x[i];

        REP(i, 20005) REP(j, 550) dp[i][j] = INF;
        dp[0][128 + 255] = 0;
        FOR(i, 1, n+1){
            for(int j = -255; j <= 255; j++){
                bool flag = false;
                if(j == -255){
                    REP(k, m){
                        for(int z = -255 - c[k]; z >= -255; z--){
                            if(dp[i-1][z + 255] != INF){
                                dp[i][-255 + 255] = min(dp[i][-255 + 255], dp[i-1][z + 255] + (LL)pow(abs(x[i-1] - (-255)), 2));
                            }
                        }
                    }
                }else if(j == 255){
                    REP(k, m){
                        for(int z = 255 - c[k]; z <= 255; z++){
                            if(dp[i-1][z + 255] != INF){
                                dp[i][255 + 255] = min(dp[i][255 + 255], dp[i-1][z + 255] + (LL)pow(abs(x[i-1] - (255)), 2));
                            }
                        }
                    }
                }else{
                    REP(k, m) flag |= dp[i-1][j-c[k] + 255] != INF;
                    if(flag){
                        REP(k, m){
                            if(j-c[k] >= 0 && j-c[k] <= 255 && dp[i-1][j-c[k] + 255] != INF){
                                dp[i][j + 255] = min(dp[i][j + 255], dp[i-1][j-c[k] + 255] + (LL)pow(abs(x[i-1] - j), 2));
                                //cout << i << " " << dp[i][j + 255] << " " << x[i-1] << " " << j-c[k] << endl;
                            }
                        }
                    }
                }
            }
        }

        LL ans = INF;
        for(int i = -255; i <= 255; i++) ans = min(ans, dp[n][i + 255]);
        cout << ans << endl;
    }

}