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

int c[30],x[30000];
int dp[300000][256];

int sq(int x) {
    return x*x;
}
int add(int x, int y) {
    return max(0, min(255, x+y));
}
int main(void) {
    while(1) {
        int n,m;
        scanf("%d%d",&n,&m);
        if(!n) break;
        memset(dp, 0x6F, sizeof(dp));

        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<256; i++)
            dp[0][i] = 0xFFFFFF;
        dp[0][128] = 0;

        for(int i=0; i<n; i++)
            for(int j=0; j<256; j++)
                for(int k=0; k<m; k++)
                    dp[i+1][add(j,c[k])] = min(dp[i+1][add(j,c[k])], dp[i][j] + sq(x[i]-add(j,c[k])));

        int res=0xFFFFFF;
        for(int j=0; j<256; j++)
            res = min(res, dp[n][j]);

        printf("%d\n", res);

    }
    return 0;
}
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;

int c[30],x[30000];
int dp[300000][256];

int sq(int x) {
    return x*x;
}
int add(int x, int y) {
    return max(0, min(255, x+y));
}
int main(void) {
    while(1) {
        int n,m;
        scanf("%d%d",&n,&m);
        if(!n) break;
        memset(dp, 0x6F, sizeof(dp));

        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<256; i++)
            dp[0][i] = 0xFFFFFF;
        dp[0][128] = 0;

        for(int i=0; i<n; i++)
            for(int j=0; j<256; j++)
                for(int k=0; k<m; k++)
                    dp[i+1][add(j,c[k])] = min(dp[i+1][add(j,c[k])], dp[i][j] + sq(x[i]-add(j,c[k])));

        int res=0xFFFFFF;
        for(int j=0; j<256; j++)
            res = min(res, dp[n][j]);

        printf("%d\n", res);

    }
    return 0;
}

C++: #include <iostream>
#include <algorithm>
#include <string.h>
#include <climits>

#define N 50000
#define M 20
#define lengthof(x) (sizeof(x)/sizeof(*(x)))

int c[M];
int x[N];
int y[N][M];
int sum[N][M];

using namespace std;

int dif(int a,int b)
{
	return (a-b)*(a-b);
}

int main(void)
{
	int n,m;
	int i1,i2,i3;
	int minn;
	int temp;
	int i;

	while(1){
		cin>>n>>m;
		if(n+m==0) break;

		memset(y,0,sizeof(y));
		fill((int*)sum,(int*)(sum+lengthof(sum)),INT_MAX);

		for(i1=0;i1<m;i1++){
			cin>>c[i1];
			y[0][i1]=128;
			sum[0][i1]=0;
		}
		for(i1=0;i1<n;i1++){
			cin>>x[i1];
			
		}

		
		for(i1=1;i1<=n;i1++){
			for(i2=0;i2<m;i2++){
				minn=INT_MAX;
				for(i3=0;i3<m;i3++){
					temp=y[i1-1][i3]+c[i2];
					if(temp<0) temp=0;
					if(255<temp) temp=255;
					
					if(minn>=dif(x[i1-1],temp)) {
						minn=dif(x[i1-1],temp);
						y[i1][i2]=temp;
						sum[i1][i2]=min(sum[i1][i2],sum[i1-1][i3]+minn);
						//cout<<i1<<" "<<i2<<" "<<i3<<" "<<sum[i1][i2]<<" "<<endl;

					}
				}
				
			}
		}

		minn=sum[n][0];
		for(i1=0;i1<m;i1++){
			if(minn>sum[n][i1]) minn=sum[n][i1];
		}

		cout<<minn<<endl;
	}
	return 0;
}

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

int n, m;
int c[20] = {0};
int x[20005] = {0};
// dp[i][j][k] x[i], use c[j] minans
int dp[20005][300] = {0};

int solve();

int main() {
  int i, j, k;
  while(1) {
    cin >> n >> m;
    if(n + m == 0) break;
    for(i = 0; i < m; ++i) cin >> c[i];
    for(i = 0; i < n; ++i) cin >> x[i];
    for(i = 0; i < 300; ++i)
      for(j = 0; j < n; ++j) dp[j][i] = inf;
    cout << solve() << endl;
  }
  return 0;
}

int solve() {
  int i, j, k, now, ans = inf, bf;
  for(i = 0; i < n; ++i) {
    for(j = 1; j < 255; ++j)
      for(k = 0; k < m; ++k) {
        now = j - c[k];
        if(now < 0 || now > 255 ||
           (i != 0 && dp[i - 1][now] == inf) ||
           (i == 0 && now != 128))
          continue;
        if(i == 0)
          bf = 0;
        else
          bf = dp[i - 1][now];
        dp[i][j] =
            min(dp[i][j], bf + (j - x[i]) * (j - x[i]));
        if(i == n - 1) ans = min(ans, dp[i][j]);
      }
    for(k = 0; k < m; ++k)
      for(j = 0; j < 256; ++j) {
        now = j + c[k];
        if(now <= 0) {
          if(i == 0 && j != 128) continue;
          if(i == 0)
            bf = 0;
          else
            bf = dp[i - 1][j];
          dp[i][0] = min(dp[i][0], bf + x[i] * x[i]);
          if(i == n - 1) ans = min(dp[i][0], ans);
        }
        else if(now >= 255) {
          if(i == 0 && j != 128) continue;
          if(i == 0)
            bf = 0;
          else
            bf = dp[i - 1][j];
          dp[i][255] = min(
              dp[i][255], bf + (255 - x[i]) * (255 - x[i]));
          if(i == n - 1) ans = min(ans, dp[i][255]);
        }
      }
  }

  return ans;
}