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 int ll;
typedef pair<int, int> P;
typedef pair<ll, ll> Pll;
typedef vector<int> Vi;
typedef tuple<int, int, int> T;
#define FOR(i,s,x) for(int i=s;i<(int)(x);i++)
#define REP(i,x) FOR(i,0,x)
#define ALL(c) c.begin(), c.end()
#define MP make_pair
#define DUMP( x ) cerr << #x << " = " << ( x ) << endl
#define fst first
#define snd second

#define INF 1300500000

int dp[2][256];
int sample[20010];


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

  while (1) {
    int N, M, code[16];
    cin >> N >> M;
    if (N == 0 && M == 0) break;
    REP(i, M) cin >> code[i];
    REP(i, N) cin >> sample[i];

    fill(dp[0], dp[0]+256, -1);
    dp[0][128] = 0;

    REP(i, N) {
      fill(dp[(i+1)%2], dp[(i+1)%2]+256, -1);
      REP(j, 256) {
	if (dp[i%2][j] == -1) continue;
	REP(k, M) {
	  int next = max(0, min(255, j + code[k]));
	  int diff = sample[i] - next;
	  if (dp[(i+1)%2][next] == -1 || dp[i%2][j] + diff * diff < dp[(i+1)%2][next]) {
	  dp[(i+1)%2][next] = dp[i%2][j] + diff * diff;
	  }
	}
      }
    }
    int ans = -1;
    REP(i, 256) {
      if (ans == -1 || (dp[N%2][i] != -1 && dp[N%2][i] < ans))
	ans = dp[N%2][i];
    }
    cout << ans << endl;
  }

  return 0;
}

C++: #include <algorithm>
#include <climits>
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;

int n,m;
int c[16],x[20000];

long dp[20000][256];

long rec(int p, int y){ //?????¨p???????????¨???????????????????????????????????????y???x[n]??¨????????????????????????
	if(dp[p][y]>=0) return dp[p][y];
	
	if(p==n) return 0;
	
	long ret=LONG_MAX;
	for(int i=0; i<m; ++i){ //??????????????????????????¨????????????????????°????????????
		int ny = y+c[i];
		if(ny<0) ny=0;
		else if(ny>255) ny=255;
		
		ret = min(ret, rec(p+1,ny)+(ny-x[p])*(ny-x[p]));
	}
	
	return dp[p][y]=ret;
}

int main(){
	while(1){		
		//input
		scanf(" %d %d", &n, &m);
		if(n==0) break;
		
		for(int i=0; i<m; ++i) scanf(" %d", &c[i]);
		for(int i=0; i<n; ++i) scanf(" %d", &x[i]);	
		
		memset(dp,-1,sizeof(dp));
		printf("%ld\n", rec(0,128));
	}
}

C++: #define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define ALL(v) v.begin(), v.end()
typedef long long ll;

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

int main() { 
  ll n,m;
  while(cin>>n>>m){
    if(n==0 && m==0) return 0;
  
    vector<ll> C(m),X(n+1);
    rep(i,m) cin>>C[i];
    rep(i,n) cin>>X[i+1];
    
    ll dp[20001][256];
    rep(i,20001){
      rep(j,256) dp[i][j]=1e9;
    }
    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++){
          if(dp[i][j]==1e9) continue;
          if(j+C[k]>255) dp[i+1][255]=min(dp[i+1][255],dp[i][j]+(255-X[i+1])*(255-X[i+1]));
          else if(j+C[k]<0) dp[i+1][0]=min(dp[i+1][0],dp[i][j]+X[i+1]*X[i+1]);
          else dp[i+1][j+C[k]]=min(dp[i+1][j+C[k]],dp[i][j]+(j+C[k]-X[i+1])*(j+C[k]-X[i+1]));
        }
      }
    }
    
    ll ans=1e9;
    for(int j=0;j<=255;j++) ans=min(ans,dp[n][j]);
    cout<<ans<<endl;
  }
}

C++: #include <iostream>
#include <vector>
#include <climits>
using namespace std;

#define INF 1 << 24
#define SQUARE(n) ((n)*(n))
typedef long INT;
typedef vector <int> VI;
typedef vector <VI> VVI;

int main( void )
{
	int n, m; // N: üÍM, M: R[hubNÌ·³
	while ( cin >> n >> m && n && m )
	{
		// üÍ
		VI C(m);
		for ( int i = 0; i < m; i++ )
		{
			cin >> C[i];
		}
		VI x(n);
		for ( int i = 0; i < n; i++ )
		{
			cin >> x[i];
		}

		VVI  dp = VVI( n+1, VI( 256, INF ) );
		dp[0][128] = 0;

		for ( int k = 1; k <= n; k++ )
		{
			for ( int i = 0; i < 256; i++ )
			{
				for ( int j = 0; j < m; j++ )
				{
					int next = i + C[j];
					next = next < 0 ? 0 : next;
					next = next > 255 ? 255 : next;
					dp[k][next] = min( dp[k][next], dp[k-1][i] + SQUARE( next - x[k-1] ) );
				}
			}
		}

		int answer = INF;
		for ( int i = 0; i < 256; i++ ) answer = min( answer, dp[n][i] );
		cout << answer << endl;
	}
	return 0;
}