akhauriyash/Code-Regression huggingface dataset:
#include <bits/stdc++.h>

using namespace std;

using ll = long long;
constexpr ll MOD = 1e9 + 7;
constexpr ll INF = 1e18;

int main() {
    ll n, m, i, j, k;
    string s;
    cin >> s;
    cin >> k;
    string t = "";
    for(i = 0; i < 103 - s.size(); i++) {
        t += '0';
    }
    t += s;
    ll cnt;
    n = t.size();
    ll dp[104][2][4 + 1];
    for(i = 0; i <= 103; i++) {
        for(j = 0; j < 2; j++) {
            for(m = 0; m < 5; m++) {
                dp[i][j][m] = 0;
            }
        }
    }
    //cout << t[102] << endl;
    cnt = 0;
    dp[0][0][0] = 1;
    for(i = 1; i <= 103; i++) {
        if(t[i - 1] != '0') {
            cnt++;
            if(cnt < k + 1) {
                dp[i][0][cnt] += dp[i - 1][0][cnt - 1];
                dp[i][1][cnt] += max(0ll, (ll)(t[i - 1] - '0' - 1)) * dp[i - 1][0][cnt - 1];
                dp[i][1][cnt - 1] += dp[i - 1][0][cnt - 1];
            }
        } else {
            if(cnt <= k) {
                dp[i][0][cnt] += dp[i - 1][0][cnt];
            }
        }
        dp[i][1][0] += dp[i - 1][1][0];
        dp[i][1][1] += dp[i - 1][1][1];
        dp[i][1][1] += dp[i - 1][1][0] * 9;
        dp[i][1][2] += dp[i - 1][1][2];
        dp[i][1][2] += dp[i - 1][1][1] * 9;
        dp[i][1][3] += dp[i - 1][1][3];
        dp[i][1][3] += dp[i - 1][1][2] * 9;
        
        for(j=0;j<min(cnt,k+1);j++){
            dp[i][0][j]+=dp[i-1][0][j];
        }
    }
    cout << dp[103][0][k] + dp[103][1][k] << endl;
    return 0;
}

Measured memory_bytes: 256
import java.util.Scanner;

public class Main {
	public static void main(String[] args) {

		Scanner sc = new Scanner(System.in);

		String s = sc.next();
		String t = sc.next();

		int ans = 0;
		for (int i=0; i<s.length(); i++){
			if (s.charAt(i) != t.charAt(i)){
				ans++;
			}
		}
		System.out.println(ans);
	}

}

Measured memory_bytes: 41184
#define  _USE_MATH_DEFINES
#define  _USE_MATH_DEFINES

#define  rep(i, n) for(int i = 0; i < n; i++)
#define  repx(i, a, n) for(int i = a; i < n; i++)
#define  loop while(1)
#define  lli long long int
#define  aryout(ary) rep(i, ary.size()) cout << ary[i] << (i < ary.size() - 1 ? " " : "\n");
 
#include <iostream>
#include <algorithm>
#include <cmath>
#include <numeric>
#include <string>
#include <vector>
#include <stack>
#include <queue>
#include <list>
#include <map>
#include <utility>
#include <set>

using namespace std;

void dfs (vector<vector<int>> &umi, int h, int w, int i, int j) {
  umi[i][j] = 2;
  for (int di = -1; di <= 1; di++)
    for (int dj = -1; dj <= 1; dj++)
      if (umi[i + di][j + dj] == 1)
        dfs (umi, h, w, i + di, j + dj);
}

int bfs (vector<vector<int>> &umi, int h, int w) {
  int cnt = 0;
  stack<vector<int>> gnd;
  vector<int> tmp0(2, 0);
  vector<int> tmp1(2, 0);

  repx (i, 1, h + 1) repx (j, 1, w + 1) {
    if (umi[i][j] == 1) {
      umi[i][j] = 2;
      cnt += 1;
      tmp0[0] = i; tmp0[1] = j;
      gnd.push(tmp0);

      while (gnd.size() > 0) {
        tmp0[0] = gnd.top()[0];
        tmp0[1] = gnd.top()[1];
        gnd.pop();

        //cout << "cnt: " << cnt << endl;
        //repx (i, 0, h + 2) repx (j, 0, w + 2)
          //cout << umi[i][j] << (j < w + 1 ? " " : "\n");

        umi[tmp0[0]][tmp0[1]] = 2;
        for (int di = -1; di <= 1; di++) {
          for (int dj = -1; dj <= 1; dj++) {
            tmp1[0] = tmp0[0] + di; tmp1[1] = tmp0[1] + dj;
            if (umi[tmp1[0]][tmp1[1]] == 1) {
              gnd.push(tmp1);
              umi[tmp1[0]][tmp1[1]] = 2;
              //cout << tmp1[0] << " " << tmp1[1] << " (" << umi[tmp1[0]][tmp1[1]] << ") " <<  "has pushed." << endl;
            }
          }
        }
      }
    }
  }
  return cnt;
}


int main() {

  int w, h;
  
  loop {
    cin >> w >> h;
    if (w + h == 0) break;

    vector<vector<int>> umi(h + 2, vector<int>(w + 2, 0));
    rep (i, h) rep (j, w) cin >> umi[i + 1][j + 1];

    /*
    int cnt_by_DFS = 0;
    repx (i, 1, h + 1) repx (j, 1, w + 1) {
      if (umi[i][j] == 1) {
        cnt_by_DFS += 1;
        dfs (umi, h, w, i, j);
      }
    }
    cout << cnt_by_DFS << endl;
    */
    cout << bfs (umi, h, w) << endl;
  }

  return 0;
}



Measured memory_bytes: 3148
/* author: Tarek */ 
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, int> pli;
typedef pair<ll, ll> pll;
typedef long double ld;
#define mp make_pair
int main()
{
// freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
int x,y,z;
cin>>x>>y>>z;
if((x+y+z)>=22)cout<<"bust"<<endl;
else cout<<"win"<<endl;
return 0;
}



Measured memory_bytes: 256
#include<cstdio>
#include<bits/stdc++.h>
typedef long long int ll;
#define REP(i, n) for(int i = 0; i < (n); i++)
#define FOR_IN(i, a, b) for(int i = (a); i < (b); i++)
#define BETWEEN(x, a, b) ((x) >= (a) && (x) <= (b))
#define BIT(b, i) (((b) >> (i)) & 1)
#define LOG_F 1
#define LOG(...) if(LOG_F) fprintf(stderr, __VA_ARGS__)

using namespace std;

ll pow(int x, int n){
  return n == 0 ? 1 : x * pow(x, n - 1);
}

struct union_find{
  vector<int> par;
  vector<int> rank;
  union_find(int n){
    rank = vector<int>(n,0);
    for(int i=0; i<n; i++){
      par.push_back(i);
    }
  }

  int root(int x){
    if(par[x]==x){
      return x;
    } else {
      return par[x] = root(par[x]);
    }
  }

  void unite(int x, int y){
    int rx = root(x);
    int ry = root(y);

    if(rx==ry)
      return;

    if(rank[rx]<rank[ry]){
      par[rx] = ry;
    } else {
      par[ry] = rx;
      if(rank[rx]==rank[ry])
        rank[rx]++;
    }
  }

  bool equiv(int x, int y){
    return root(x)==root(y);
  }
};

/*
x y

1 3 5 7 8 10 12
4 6 9 11
2
 */
int x,y;

int main(){
  union_find uf(12);
  uf.unite(1,3);
  uf.unite(1,5);
  uf.unite(1,7);  
  uf.unite(1,8);
  uf.unite(1,10);
  uf.unite(1,12);
  uf.unite(4,6);
  uf.unite(4,9);
  uf.unite(4,11);

  cin>>x>>y;
  if(uf.equiv(x,y)){
    cout<<"Yes\n";
  } else {
    cout<<"No\n";
  }

  return 0;
}


Measured memory_bytes: 256
def min(x, y)
  return (x < y ? x : y)
end

N, Ma, Mb = gets.split.map(&:to_i)
a, b, c = [], [], []
N.times do
  tmp = gets.split.map(&:to_i)
  a << tmp[0]
  b << tmp[1]
  c << tmp[2]
end
INF = Float::INFINITY
MAX = 40 * 10 + 1
dp = Array.new(N + 1) { Array.new(MAX) { Array.new(MAX, INF) } }
dp[0][0][0] = 0
N.times do |i|
  MAX.times do |ca|
    MAX.times do |cb|
      next if dp[i][ca][cb] == INF
      dp[i + 1][ca][cb] = min(dp[i + 1][ca][cb], dp[i][ca][cb])
      dp[i + 1][ca + a[i]][cb + b[i]] = min(dp[i + 1][ca + a[i]][cb + b[i]], dp[i][ca][cb] + c[i])
    end
  end
end
ans = INF
(1...MAX).each do |ca|
  (1...MAX).each do |cb|
    if ca * Mb == cb * Ma
      ans = min(ans, dp[N][ca][cb])
    end
  end
end
p (ans == INF ? -1 : ans)


Measured memory_bytes: 54140
import sys


def dist(a, b):
    return abs(a[0] - b[0]) + abs(a[1] - b[1])


N = int(input())
q = []
for i in range(N):
    q.append(tuple(list(map(int, input().split()))))
q = sorted(q)

tmp = (0, 0, 0)
for query in q:
    if (query[0] - tmp[0] < dist(tmp[1:], query[1:]) or (query[1] - tmp[1] + query[2] - tmp[2]) % 2 != (query[0] - tmp[0]) % 2):
        print('No')
        sys.exit()
else:
    print('Yes')


Measured memory_bytes: 18204
#include <iostream>
#include <algorithm>
#include <vector>
#include <deque>
#include <queue>
#include <map>
#include <iomanip>
#include <cmath>
#include <set>
#include <numeric>
#include <boost/multiprecision/cpp_int.hpp>
using namespace std;
using ll = long long;
namespace mp = boost::multiprecision;

int main()
{
    // 整数の入力
    ll N;
    cin >> N;
    vector<ll> A(N);
    for (size_t i = 0; i < N; i++)
    {
        scanf("%lld",&A[i]);
    }
    vector<pair<ll,ll>> money_kabus;
    money_kabus.push_back({1000,0});
    for (size_t i = 0; i < N; i++)
    {
        vector<pair<ll,ll>> next_money_kabus(money_kabus);
        for(auto &p:money_kabus){
            next_money_kabus.push_back({p.first + p.second * A[i], 0});
            next_money_kabus.push_back({p.first % A[i], p.second + p.first/A[i]});
        }
        sort(next_money_kabus.begin(),next_money_kabus.end());
        money_kabus.clear();
        for(auto &p:next_money_kabus){
            if (money_kabus.empty()){
                money_kabus.push_back(p);
            }else{
                if (money_kabus.back().first <= p.first && money_kabus.back().second <= p.second){
                    money_kabus.pop_back();
                    money_kabus.push_back(p);
                }else{
                    money_kabus.push_back(p);
                }
            }
        }
    }
    cout << money_kabus.back().first << endl;
    return 0;
}

Measured memory_bytes: 3704
import sys
def input():
  return sys.stdin.readline().rstrip()

S=input()
T=input()

ok=True
a,b={},{}
for i in range(len(S)):
  s=S[i]
  t=T[i]
  if s in a:
    if a[s]!=t:
      ok=False
  if t in b:
    if b[t]!=s:
      ok=False
  a[s]=t
  b[t]=s

print("Yes" if ok else "No")


Measured memory_bytes: 68660
n = int(input())
A = [list(int(x) - 1 for x in input().split()) for _ in range(n)]
B = [0] * n

ans = 1
stack = [i for i in range(n)]
while True:
    day_set = set()
    next_stack = []
    while stack:
        i = stack.pop()
        if i in day_set or B[i] >= n - 1:
            continue
        j = A[i][B[i]]
        if j in day_set or B[j] >= n - 1:
            continue
        if A[j][B[j]] == i:
            B[i] += 1
            B[j] += 1
            day_set.add(i)
            day_set.add(j)
            next_stack += [i, j]
            if all(b >= n - 1 for b in B):
                print(ans)
                exit()

    if not day_set:
        print(-1)
        exit()
    ans += 1
    stack = next_stack


Measured memory_bytes: 84720
#include<bits/stdc++.h>
#define ll long long
using namespace std;
int main()
{
    int n;
    cin>>n;
    int a[n+1];
    a[0] = -1;
    for(int i=1; i<=n; i++)
        cin>>a[i];
    int b[n+1], c[n+1];
    for(int i=1; i<=n; i++)
        cin>>b[i];
    memset(c, 0, sizeof(c));
    for(int i=1; i<n; i++)
        cin>>c[i];
        
    int sum = 0;
    
    for(int i=1; i<=n; i++)
    {
        sum += b[a[i]];
        if(a[i-1] + 1 == a[i])
            sum += c[a[i-1]];
    }
    cout<<sum;
}

Measured memory_bytes: 256
import java.util.Scanner;

public class Main {

	static long ans = 0;

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int N = Integer.parseInt(sc.next());
		int M = Integer.parseInt(sc.next());
		int Q = Integer.parseInt(sc.next());
		int[] a = new int[Q];
		int[] b = new int[Q];
		int[] c = new int[Q];
		int[] d = new int[Q];
		for(int i = 0; i < Q; i++) {
			a[i] = Integer.parseInt(sc.next());
			b[i] = Integer.parseInt(sc.next());
			c[i] = Integer.parseInt(sc.next());
			d[i] = Integer.parseInt(sc.next());
		}
		sc.close();
		int[] e = new int [N + 1];
		dfs(1, N, 1, e, a, b, c, d, Q, M);
        System.out.println(ans);

	}

	private static void dfs(int v, int n, int num, int[] e , int[] a, int[] b, int[] c, int[] d, int q, int m) {
		if(n < v) {
			long tmp = 0;
			for(int i = 0; i < q; i++) {
				if(e[b[i]] - e[a[i]] == c[i]) {
					tmp += d[i];
				}
			}
			ans = Math.max(ans, tmp);
			return;
		}
		for (int i = num; i <= m; i++) {
            e[v] = i;
            dfs(v + 1, n, i, e, a, b, c, d, q, m);
        }
	}

}


Measured memory_bytes: 36804
#include <cstdio>
#include <cctype>
#include <utility>
#include <vector>
#include <cmath>
#include <algorithm>
#include <set>
#include <climits>
using namespace std;

#define gcu getchar_unlocked
#define pcu putchar_unlocked
#define _T template <typename T>
#define _HT template <typename H, typename... T>
#define _il inline
#define _in _il int in
#define _sc _il bool scan
_T T in(int c){T n=0;bool m=false;while(isspace(c)){c=gcu();}if(c=='-')m=true,c=gcu();
	do{n=10*n+(c-'0'),c=gcu();}while(c>='0'&&c<='9');return m?-n:n;}
_in() {return in<int>(gcu());}
_T T scan(T &n){int c=gcu();return c==EOF?false:(n=in<T>(c),true);}
_sc(char &c){c=gcu();gcu();return c!=EOF;}
#ifdef _GLIBCXX_STRING
_sc(string &s){int c;s="";
	for(;;){c=gcu();if(c=='\n'||c==' ')return true;else if(c==EOF)return false;s+=c;}}
#endif
_HT _sc(H &h, T&&... t){return scan(h)&&scan(t...);}
#define _vo _il void out
#define _vl _il void outl
_vo(bool b) {pcu('0'+b);}
_vo(const char *s){while(*s)pcu(*s++);}
_vo(char c){pcu(c);}
#ifdef _GLIBCXX_STRING
_vo(string s){for(char c:s)pcu(c);}
#endif
_T _vo(T n){static char buf[20];char *p=buf;
	if(n<0)pcu('-'),n*=-1;if(!n)*p++='0';else while(n)*p++=n%10+'0',n/=10;
	while (p!=buf)pcu(*--p);}
_vl(){out('\n');}
#ifdef _GLIBCXX_VECTOR
_T _vo(vector<T> &v){for(T &x:v)out(&x == &v[0]?"":" "),out(x);outl();}
#endif
_HT _vo(H&& h, T&&... t){out(h);out(move(t)...);}
template <typename... T> _vl(T&&... t){out(move(t)...);outl();}

vector<bool> sieve;
vector<int> primes;

void make_primes(int n) {
	sieve = vector<bool>(n, true);
	sieve[1] = false;
	for (int i = 4; i < n; i += 2)
		sieve[i] = false;
	for (int i = 9; i < n; i += 6)
		sieve[i] = false;
	for (int i = 6, e = sqrt(n); i <= e; i += 6) {
		for (int j : {i - 1, i + 1})
			if (sieve[j])
				for (int k = j * j; k < n; k += j)
					sieve[k] = false;
	}
	primes = {2, 3};
	for (int i = 6 ; i < n; i += 6)
		for (int j : {i - 1, i + 1})
			if (sieve[j])
				primes.push_back(j);
}

struct min_list {
	int max_size, size = 0;
	multiset<int> s;
	min_list(int s) {
		max_size = s;
	}
	bool insert(int n) {
		if (size < max_size) {
			s.insert(n);
			size++;
			return true;
		}
		auto r = max_element(s.begin(), s.end());
		if (n <= *r) {
			s.erase(r);
			s.insert(n);
			return true;
		} else
			return false;
	}
	int max() {
		return s.empty() ? INT_MAX : *s.rbegin();
	}
};

int main() {
	make_primes(110000);
	for (int n, p; n = in(), p = in(), n != -1;) {
		auto s = upper_bound(primes.begin(), primes.end(), n);
		min_list l(p);
		for (auto i = s; l.insert(*i + *i); i++)
			for (auto j = i + 1; l.insert(*i + *j) ;j++)
				;
		outl(l.max());
	}
}


Measured memory_bytes: 2620
#include <bits/stdc++.h>
#define repd(i,a,b) for (int i=(a);i<(b);i++)
#define rep(i,n) repd(i,0,n)
#define int long long
#define double long double
#define PI 3.141592653589793
typedef long long ll;
const int MOD = 1000000007;
const int INF = LLONG_MAX;
const double EPS = 1e-10;
using namespace std;
const pair<int,int> fd[] = {make_pair(1,0),make_pair(-1,0),make_pair(0,1),make_pair(0,-1)}; 

vector<int> divisor(int n){
  vector<int> ret;
  for(int i = 1;i*i <= n;i++){
    if(n%i == 0){
      ret.push_back(i);
      if(i*i != n)ret.push_back(n/i);
    }
  }
  sort(ret.begin(), ret.end());
  return ret;
}

int n,m;vector<pair<int,int>> v[110000];
int inds[110000];

bool dfs(int ind, int node){
  if(inds[node] != INF)return (inds[node] == ind);
  inds[node] = ind;
  bool ret = true;
  rep(i,v[node].size()){
    ret = ret && dfs(ind+v[node][i].second, v[node][i].first);
  }
  return ret;
}

signed main(){
  cin>>n>>m;
  rep(i,m){
    int l,r,d;cin>>l>>r>>d;
    v[l].push_back(make_pair(r,d));
    v[r].push_back(make_pair(l,-d));
  }
  rep(i,110000)inds[i] = INF;
  bool ans = true;
  repd(i,1,n+1){
    if(inds[i] == INF)ans = ans && dfs(0,i);
  }
  cout << (ans ? "Yes" : "No") << endl;
}



Measured memory_bytes: 16768
#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

int main()
{
#ifdef ONPC
    freopen("a.in", "r", stdin);
    //freopen("a.out", "w", stdout);
#else
    //freopen("a.in", "r", stdin);
    //freopen("a.out", "w", stdout);
#endif
    int n;
    cin >> n;
    vector <int> a(n);
    for (int i = 0; i < n; i++)
    {
        cin >> a[i];
    }
    sort(a.begin(), a.end());
    set <int> pos;
    ll sum = 0;
    for (int i = 0; i < n; i++)
    {
        if (a[i] > 2 * sum)
        {
            pos.insert(i);
        }
        sum += a[i];
    }
    int ans = 0;
    for (int i = 0; i < n; i++)
    {
        ans += (pos.upper_bound(i) == pos.end());
    }
    cout << ans << '\n';
}



Measured memory_bytes: 640
/* include */
#include <bits/stdc++.h>

/* using */
using namespace std;

/* main constructor */
struct Fast
{
    Fast()
    {
        std::cin.tie(0);
        ios::sync_with_stdio(0);
        //cout << fixed << setprecision(12);
    }
} fast;

/* template */
template <class T>
bool chmax(T &a, const T &b) { return (a < b) ? (a = b, 1) : 0; }
template <class T>
bool chmin(T &a, const T &b) { return (b < a) ? (a = b, 1) : 0; }

/* define */
#define REP(i, x, n) for (int i = x; i < (int)(n); ++i)
#define RREP(i, x, n) for (int i = x - 1; i >= (int)(n); --i)
#define rep(i, n) REP(i, 0, n)
#define reps(i, n) REP(i, 1, n + 1)
#define rrep(i, n) RREP(i, n, 0)
#define each(i, n) for (auto &i : n)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define eunique(x) \
    sort(all(x));  \
    (x).erase(unique(all(x)), (x).end())
#define int long long
#define mp make_pair

/* function */
int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }
int lcm(int a, int b) { return a * b / gcd(a, b); }

/* const */
const int inf = 1e9 + 7;
const int dxl[8] = {1, 0, -1, 0, 1, -1, -1, 1};
const int dyl[8] = {0, 1, 0, -1, 1, 1, -1, -1};
const int dxs[4] = {1, 0, -1, 0};
const int dys[4] = {0, 1, 0, -1};

/* main */
signed main(void)
{
    int n, m;
    string tmp;
    cin >> n;
    vector<int> num = {0};
    vector<string> str;
    unordered_multiset<string> s(n);
    rep(i, n)
    {
        cin >> tmp;
        str.emplace_back(tmp);
        s.emplace(tmp);
    }
    cin >> m;
    unordered_multiset<string> t;
    rep(i, m)
    {
        cin >> tmp;
        t.emplace(tmp);
    }
    rep(i, n)
    {
        num.push_back(s.count(str[i]) - t.count(str[i]));
    }
    cout << *max_element(all(num)) << endl;
    return 0;
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
#include <unordered_map>
#include <queue>
#define MOD 1000000007
#define REP(i,st,num)for(int i=(st);i<(num);++i)
#define ll long long
using namespace std;

int main() {
    ios::sync_with_stdio(false);
    int N,K,t;
    cin>>N>>K;
    cout<<1 + ceil((double)(N-K)/(K-1));
    return 0;
}
//7 3 1 8 4 6 2 5
//1 1 1 1 1 6 2 5 9
//1 1 1 1 1 1 1 1 9

Measured memory_bytes: 256
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int a = sc.nextInt();
        int b = sc.nextInt();
        int h = sc.nextInt();
        int m = sc.nextInt();
        sc.close();

        double hDegree = (h*60+m)*0.5;
        double mDegree = m*6;
        double degree = Math.abs(hDegree-mDegree);

        double ans = Math.sqrt(Math.pow(a,2)+Math.pow(b,2)-2*a*b*Math.cos(Math.toRadians(degree)));
        System.out.println(ans);

    }
}


Measured memory_bytes: 35628
#include <iostream>
#include <vector>
#include <queue>

using namespace std;

int main() {
    int n,k;
    cin >> n >> k;
    vector v(n,0L);
    for (int i=0; i<n; i++) cin >> v[i];

    long ans = 0;
    for (int i=0; i<=k; i++) {
        for (int j=0; j<=k; j++) {
            if (i+j>n || i+j>k) continue;
            long sum = 0;
            priority_queue<long, vector<long>, greater<>> pq;

            for (int l=0; l<i; l++) {
                sum += v[l];
                pq.emplace(v[l]);
            }

            for (int l=0; l<j; l++) {
                sum += v[n-1-l];
                pq.emplace(v[n-1-l]);
            }

            ans = max(ans, sum);
            
            for (int l=k; l>i+j; l--) {
                if (pq.empty()) break;
                sum -= pq.top(); pq.pop();
                ans = max(ans, sum);
            }

        }
    }

    cout << ans << endl;
}


Measured memory_bytes: 3196
#include<iostream>
using namespace std;
int dx[]={0,1,0,-1},dy[]={1,0,-1,0};
int main(){
	int m,n,k; char b[9];
	while(cin>>m>>n,m){
		int x=1,y=1,d=0;
		for(;cin>>b,b[0]!='S';){
			k=0;
			if(b[0]=='F'||b[0]=='B')cin>>k;
			if(b[0]=='B')k=-k;
			if(k)x+=k*dx[d],y+=k*dy[d];
			else if(b[0]=='R')d=d+1&3;
			else d=d+3&3;
			x>0?x<=m?0:x=m:x=1;y>0?y<=n?0:y=n:y=1;
		}
		cout<<x<<" "<<y<<endl;
	}
	return 0;
}

Measured memory_bytes: 872
#include <iostream>
#include <iomanip>
#include <vector>
#include <utility>
#include <map>
#include <algorithm>
#include <queue>
#include <cmath>
#include <numeric>

using namespace std;
struct aaa{aaa(){cin.tie(nullptr); ios::sync_with_stdio(false); cout<<fixed<<setprecision(20);};}aaa;

typedef long long int64;


int main() {
    char c;
    cin >> c;
    cout << (char)(c+1) << endl;
}

Measured memory_bytes: 888
import java.util.*;
import java.io.*;
import java.awt.geom.*;
import java.math.*;

public class Main {

	static final Scanner in = new Scanner(System.in);
	static final PrintWriter out = new PrintWriter(System.out,false);

	static void solve() {
		out.println(in.nextLine().toUpperCase());
	}

	public static void main(String[] args) {
		long start = System.currentTimeMillis();

		solve();
		out.flush();

		long end = System.currentTimeMillis();
		//trace(end-start + "ms");
		in.close();
		out.close();
	}

	static void trace(Object... o) { System.out.println(Arrays.deepToString(o));}
}

Measured memory_bytes: 25896
#include<bits/stdc++.h>
using namespace std;
#define ALL(x) x.begin(),x.end()
#define rep(i,n) for(int i=0;i<n;i++)
#define debug(v) cout<<#v<<":";for(auto x:v){cout<<x<<' ';}cout<<endl; 
#define INF 1000000000
#define mod 1000000007
typedef long long ll;
const ll LINF = 1001002003004005006ll;
int dx[]={1,0,-1,0};
int dy[]={0,1,0,-1};
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; }

////////////////////
template< typename T>
struct SparseTable{
    vector<vector<T> > st;
    vector<int> lookup;
    T op(T a,T b){
        //max , min or etc...
        return max(a,b);
    }
 
    SparseTable(const vector<T> &v){
        int b=0;
        while((1<<b)<=v.size()) b++;
        //resize  st[k][i]:iの2^k先までの範囲の
        st.assign(b,vector<T>(1<<b));
        for(int i=0;i<v.size();i++) st[0][i]=v[i];
        for(int i=1;i<b;i++){
            for(int j=0;j+(1<<i)<=(1<<b);j++){
                st[i][j]=op(st[i-1][j],st[i-1][j+(1<<(i-1))]);
            }
        }
        lookup.resize(v.size()+1);
        for(int i=2;i<lookup.size();i++){
            lookup[i]=lookup[i>>1]+1;
        }
    }
    //[)
    T f(int l,int r){
        assert(l<=r);
        //a[l~l+2^k],a[r-2^k~r]が区間を覆うように
        int b=lookup[r-l];
        return op(st[b][l],st[b][r-(1<<b)]);
    }
};
//////////////

/*片方ある位置までいって
もう片方へmaxのとこまで行くときに俺はSparseTable使ったけど
これ累積MAXをとっておけばO(NlogN)じゃなくてO(N)での解答が可能
なんでSparseTableじゃなくて累積MAXで十分かというと
範囲の最大値を求めるんだけど左端は0で固定だからだよね
*/

signed main(){
    cin.tie(0);
    ios::sync_with_stdio(0);

    ll n,c;cin>>n>>c;
    vector<ll> x(n),v(n);
    rep(i,n) cin>>x[i]>>v[i];
    ll a1=0,a2=0,a3=0,a4=0;
    ll ans=0;
    //右回りだけ
    vector<ll> vv;
    ll cal=0;
    for(int i=0;i<n;i++){
        cal+=v[i];
        vv.push_back(cal-x[i]);
        a1=max(a1,cal-x[i]);
    }
    //左回りだけ
    vector<ll> vvv;
    cal=0;
    for(int i=n-1;i>=0;i--){
        cal+=v[i];
        vvv.push_back(cal-(c-x[i]));
        a2=max(a2,cal-(c-x[i]));
    }

    //折り返す。
    //それぞれ左にいったとこから右の最大まで行く
    SparseTable<ll> sp(vv);
    cal=0;
    for(int i=n-1;i>0;i--){
        ll dis=c-x[i];
        cal+=v[i];
        ll tmp=cal-2*dis+sp.f(0,i);
        a3=max(a3,tmp);
    }


    //右に行って左の最大までいく
    SparseTable<ll> st(vvv);
    cal=0;
    for(int i=0;i<n-1;i++){
        ll dis=x[i];
        cal+=v[i];
        ll tmp=cal-2*dis+st.f(0,n-i-1);
        a4=max(a4,tmp);
    }

    // cout<<a1<<endl;
    // cout<<a2<<endl;
    // cout<<a3<<endl;
    // cout<<a4<<endl;

    ans=max({a1,a2,a3,a4});
    cout<<ans<<endl;
    return 0;
}


Measured memory_bytes: 40144
#include<iostream>
#include<strstream>
#include<stack>
#include<utility>
#include<cstdio>
#include<algorithm>
#include<vector>
#include<cmath>
#include<climits>
#include<map>
#include<string>
#include<sstream>

using namespace std;

#define mp make_pair
#define rep(i,n) for( int i = 0; i < n; i++ )
typedef long long int ll;

int main()
{
	int n;
	while( cin >> n, n )
	{
		vector<int> v(n);
		rep(i,n)
		{
			cin >> v[i];
		}
		string s;
		cin >> s;

		int v_i = 0;
		for( int i = 0; i < s.size(); i++)
		{
			if( v_i > n-1 ){
				v_i = 0;
			}
			rep(j,v[v_i])
			{
				s[i]--;
				if( s[i] == 64 ){
					s[i] = 'z';
				}
				if( s[i] == 96 ){
					s[i] = 'Z';
				}
			}
			v_i++;
		}
		cout << s << endl;
	}
	return 0;
}

Measured memory_bytes: 1212
#include <bits/stdc++.h>
using namespace std;
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }
using ll = long long;
using P = pair<ll, ll>;
const long double PI = acos(-1.0L);
ll GCD(ll a, ll b) { return b?GCD(b, a%b):a; }
ll LCM(ll a, ll b) { return a/GCD(a, b)*b; }

int n;
ll k;

bool isOK(ll key, vector<ll> &xvec) {
    int xlen = xvec.size();
    int cnt = 0;
    for(int i = 0; i < xlen; ++i) {
        if(xvec[i] > key) {
            cnt += (xvec[i]-1)/key;
        }
    }
    if(k >= cnt) return true;
    else return false;
}

ll binary_search(ll Amax, vector<ll> &xvec) {
    ll ok = Amax; ll ng = 0;
    while(abs(ok-ng) > 1) {
        ll mid = (ok+ng) / 2;
        if(isOK(mid, xvec)) ok = mid;
        else ng = mid;
    }
    return ok;
}

int main() {
    cin >> n >> k;
    vector<ll> Avec(n, 0);
    ll AA = 0;
    for(int i = 0; i < n; ++i) {
        cin >> Avec[i];
        chmax(AA, Avec[i]);
    }

    ll ans = binary_search(AA, Avec);
    cout << ans << endl;
}

Measured memory_bytes: 4856
mod=10**9+7
import sys
input=sys.stdin.readline
n=int(input())
c=[int(input())for i in range(n)]
last=[-1]*max(c)
last[c[0]-1]=0
dp=[0]*n
dp[0]=1
for i in range(1,n):
	dp[i]=dp[i-1]
	if c[i-1]!=c[i] and last[c[i]-1]!=-1:
		dp[i]+=dp[last[c[i]-1]]
		dp[i]%=mod
	last[c[i]-1]=i
print(dp[-1])

Measured memory_bytes: 26256
import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int k = sc.nextInt();
        int a = sc.nextInt();
        int b = sc.nextInt();
        if ((a - 1) / k == b / k) {
            System.out.println("NG");
        } else {
            System.out.println("OK");
        }
    }
}

Measured memory_bytes: 35772
#include <bits/stdc++.h>

					using namespace std;
					typedef long long ll;
					int main()
					{
						int n;
						cin >> n;
						for(int i = 1; i <= n; i++)
						{
							int k;
							cin >> k;
							if(k % 2 == 0)
								if(k % 3 != 0 && k % 5 != 0)
									return cout << "DENIED", 0;
						}
						cout << "APPROVED";
					}

Measured memory_bytes: 256
/* 
i段目に行く方法＝i-1段目に行く方法＋i-2段目に行く方法
dpで方法の数を保持しながらリストをなめていく
もしi段目の床が壊れているなら足さない 
*/

#include <stdio.h>
#include <iostream>
#include <string>
#include <algorithm>
#include <numeric>
#include <cmath>
#include <queue>
#include <vector>
#include <iomanip>
using namespace std;

const long long mod = 1e9+7;

int main(){
    // input
    int N, M; cin >> N >> M; 
    // 床が壊れている場合をfalse，それ以外をtrueとする
    vector<int> oks(N+1, true); // 変数名(要素数，初期値)
    for (int i = 0; i < M; i++){
        int a; cin >> a;
        oks[a] = false;
    }
    
    // compute
    vector<long long int> dp(N+1); // i段目に行く方法の数を保持
    dp[0] = 1;
    for(int i = 0; i < N; i++){
        for(int j = i + 1; j <= min(N, i+2); j++){ 
            // 1段先，2段先に行ける場合はそれまでの方法の数を足す
            if(oks[j]){
                dp[j] += dp[i];
                dp[j] %= mod;  
            }
        }
    }
    
    // output
    cout << dp[N] << endl;
}



Measured memory_bytes: 1408
#include <iostream>
#include <vector>
#include <algorithm>
#include <queue>
#include <utility>
using namespace std;
const int inf = 1e9;
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
struct info{
    int y,x,c;
    info(int y, int x, int c):y(y),x(x),c(c){}
};

int main(){
    int h,w;
    cin >> h >> w;
    vector<vector<char> > field(h+2, vector<char>(w+2));
    int wcount=0;
    for(int i=1; i<=h; i++){
        for(int j=1; j<=w; j++){
            cin >> field[i][j];
            if(field[i][j]=='.') wcount++;
        }
    }

    queue<info> wait;
    wait.push(info(1,1,1));
    vector<vector<int> > mindist(h+2, vector<int>(w+2, 1e9));
    mindist[1][1] = 1;
    while(!wait.empty()){
        int y = wait.front().y;
        int x = wait.front().x;
        int c = wait.front().c;
        wait.pop();
        for(int i=0; i<4; i++){
            int ny = y+dy[i];
            int nx = x+dx[i];
            if(c+1 < mindist[ny][nx] && field[ny][nx]=='.'){
                mindist[ny][nx] = c+1;
                wait.push(info(ny, nx, c+1));
            }
        }
    }
    if(mindist[h][w] == inf){
        cout << -1 << endl;
    }else{
        cout << wcount -mindist[h][w] << endl;
    }
    return 0;
}


Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
#define _GLIBCXX_DEBUG //GCC5.4.1で配列の範囲外アクセスをエラーにする

//cmdで g++ test.cpp とするとコンパイルできる
//制限1秒なら10^6は余裕 10^7は多分間に合う 10^8はよほど単純でないと厳しい

#define ALL(x) x.begin(),x.end()
typedef long long ll; //long long
typedef pair<int,int> pii; //pair<int,int>
typedef vector<int> vi; //vector<int>
typedef vector<vector<int>> vvi; //vector<vector<int>>
typedef vector<ll> vll; //vector<long long>
typedef vector<vector<ll>> vvll; //vector<vector<long long>>
typedef priority_queue<int, vector<int>, greater<int> > gpq; //逆順priority_queue
typedef priority_queue<int> pq;

//const int MOD=998244353;
//const int MOD=1000000007; //10^9+7
const double PI=3.1415926535;

int main(){
    int N,M;
    cin>>N>>M;
    vvi nei(N+1,vi(0));
    for(int i=0;i<M;i++){
        int a,b;
        cin>>a>>b;
        nei[a].push_back(b);
        nei[b].push_back(a);
    }
    vi direction(N+1,-1);
    direction[1]=0;
    queue<int> q;
    q.push(1);
    while(!q.empty()){
        int qt=q.front();
        q.pop();
        int L=nei[qt].size();
        for(int i=0;i<L;i++){
            if(direction[nei[qt][i]]==-1){
                direction[nei[qt][i]]=qt;
                q.push(nei[qt][i]);
            }
        }
    }
    cout<<"Yes"<<endl;
    for(int i=2;i<=N;i++){
        cout<<direction[i]<<endl;
    }
}


Measured memory_bytes: 10104
#include<bits/stdc++.h>
#define all(x) (x).begin(),(x).end()
#define ll long long
#define rep(i,n) for(int i = 0; i < int(n); i++)
#define vi vector<int>
using namespace std;
const int INF = 1001001001;
const int MOD = 1e9+7;
const int dx[] = {-1, 0, 1, 0};
const int dy[] = {0, 1, 0, -1};
template<class T> inline bool chmax(T &a, const T &b){ if(a<b) { a=b; return 1; } return 0; }
template<class T> inline bool chmin(T &a, const T &b){ if(b<a) { a=b; return 1; } return 0; }

int main(){
  cin.tie(0), ios::sync_with_stdio(false);
  int n; cin >> n;
  vi l(n); rep(i,n) cin >> l[i];
  sort(all(l));
  int ans = 0;
  rep(i,n-2){
    for(int j = i+1; j < n; j++){
      auto tmp = lower_bound(all(l),l[i]+l[j])-1;
      if(j < tmp-l.begin()) ans += tmp-l.begin()-j;
    }
  }
  cout << ans;
  cout << "\n";
  return 0;
}


Measured memory_bytes: 3664
#include<iostream>
#include<string.h>
#include<cstdio>
#include<algorithm>
#include<stack>
#include<queue>
#include<vector>
#include<cmath>
#include<utility>
#define ll long long int
#define ld long double
#define INF 1000000000
#define EPS 0.0000000001
#define rep(i,n) for(i=0;i<n;i++)
using namespace std;
typedef pair<int, int> P;

string str;

int main()
{
  double xa1,ya1,xa2,ya2,xb1,yb1,xb2,yb2;

  while(scanf("%lf %lf %lf %lf %lf %lf %lf %lf",
	      &xa1,&ya1,&xa2,&ya2,&xb1,&yb1,&xb2,&yb2)!=EOF){

    if(xb2<xa1 || xa2<xb1 || yb2<ya1 || ya2<yb1)
      cout<<"NO"<<endl;
    else
      cout<<"YES"<<endl;

  }
}

Measured memory_bytes: 1232
#include <iostream>
#include <algorithm>
#include <vector>
#include <deque>
#include <queue>
#include <set>
#include <map>
#include <limits>
#include <cmath>
#include <iomanip>
#include <functional>
#include <random>
#include <boost/multiprecision/cpp_int.hpp>

using namespace std;
using ll = long long;

int main()
{
    // 整数の入力
    ll a;
    cin >> a;
    if (a >= 30){
        cout << "Yes" << endl;
    }else{
        cout << "No" << endl;
    }
    return 0;
}

Measured memory_bytes: 3628
/* * * * * * * * * * * **
*                      *
*	   saurabh8522	   *
*	  I will handle	   *
*		   it.		   *
*                      *
* * * * * * * * * * * **/
#include<bits/stdc++.h>
//#include <ext/pb_ds/assoc_container.hpp>
#define mp(a,b) make_pair(a,b)
#define pb push_back
#define FastRead ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define endl '\n'
#define ld long double
#define zero(a) memset((a),0,sizeof((a)))
#define one(a) memset((a),1,sizeof((a)))
#define minus(a) memset((a),-1,sizeof((a)))
#define all(g) g.begin(),g.end()
#define ppb pop_back
using namespace std;
//using namespace __gnu_pbds;
//typedef tree<int, null_type, less_equal<int>, rb_tree_tag, tree_order_statistics_node_update> //indexed_set;
typedef long long int ll;
#define MOD 1000000007
ll extgcd(ll a,ll b,ll& x,ll& y){if(b==0){x=1;y=0;return a;}else{int g=extgcd(b,a%b,y,x);y-=a/b*x;return g;}}
ll modpow(ll a,ll b,ll m) {ll res=1;a%=m;for(;b;b>>=1){if(b&1)res=res*a%m;a=a*a%m;}return res;}
ll numdigit(ll n){return floor(log10(n)) + 1;}
bool isPowerTwo (ll x) { return x && (!(x&(x-1))); } 
int  main(){
	FastRead;
	ll t=1;
	// cin>>t;
	while(t--){
		ll n,k;
		cin>>n>>k;
		ll l=n/k;
		l*=k;
		ll r=(n+k-1)/k;
		r*=k;
		cout<<min(n-l,r-n)<<endl;
	}
}

Measured memory_bytes: 256
import math


def is_integer_num(n):
    if isinstance(n, int):
        return True
    if isinstance(n, float):
        return n.is_integer()
    return False


N, D = map(int, input().split())

li = []
count = 0

for _ in range(N):
    li.append(list(map(int, input().split())))

for i in range(N - 1):
    for j in range(i + 1, N):
        target = 0
        if D != 1:
            for k in range(D):
                target += (li[i][k] - li[j][k]) ** 2
            if is_integer_num(math.sqrt(target)):
                count += 1
        else:
            target += (li[i][0] - li[j][0]) ** 2
            if is_integer_num(math.sqrt(target)):
                count += 1


print(count)





Measured memory_bytes: 3064
import math
from math import gcd,pi,sqrt
INF = float("inf")
MOD = 10**9 + 7

import sys
sys.setrecursionlimit(10**6)
import itertools
import bisect
from collections import Counter,deque
def i_input(): return int(input())
def i_map(): return map(int, input().split())
def i_list(): return list(i_map())
def i_row(N): return [i_input() for _ in range(N)]
def i_row_list(N): return [i_list() for _ in range(N)]
def s_input(): return input()
def s_map(): return input().split()
def s_list(): return list(s_map())
def s_row(N): return [s_input for _ in range(N)]
def s_row_str(N): return [s_list() for _ in range(N)]
def s_row_list(N): return [list(s_input()) for _ in range(N)]


def main():
    n = i_input()
    a = i_list()

    dp = [0]*(10**5+10)
    for i in a:
        dp[i] += 1
        dp[i+1] += 1
        dp[i+2] += 1
    print(max(dp))







if __name__=="__main__":
    main()


Measured memory_bytes: 86304
#include <iostream>
#include <algorithm>
#include <string>
#define flush fflush(stdout)
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
const int mod = 1e9 + 7, INF = 1e9;


int main(void) {
    int h, w, n, si, sj, i, pi, pj;
    string s, t;

    scanf("%d%d%d%d%d", &h, &w, &n, &si, &sj);
    cin >> s >> t;
    
    pj = sj;
    for (i = 0; i < n; i++) {
        if (s[i] == 'L') pj--;
        if (pj == 0) {
            printf("NO\n");
            return 0;
        }
        if (t[i] == 'R') {
            pj = min(w, pj + 1);
        }
    }

    pj = sj;
    for (i = 0; i < n; i++) {
        if (s[i] == 'R') pj++;
        if (pj == w + 1) {
            printf("NO\n");
            return 0;
        }
        if (t[i] == 'L') {
            pj = max(1, pj - 1);
        }
    }

    pi = si;
    for (i = 0; i < n; i++) {
        if (s[i] == 'U') pi--;
        if (pi == 0) {
            printf("NO\n");
            return 0;
        }
        if (t[i] == 'D') {
            pi = min(h, pi + 1);
        }
    }

    pi = si;
    for (i = 0; i < n; i++) {
        if (s[i] == 'D') pi++;
        if (pi == h + 1) {
            printf("NO\n");
            return 0;
        }
        if (t[i] == 'U') {
            pi = max(1, pi - 1);
        }
    }

    printf("YES\n");

    return 0;
}

Measured memory_bytes: 776
#ifdef LOCAL
#define _GLIBCXX_DEBUG
#endif
#include <bits/stdc++.h>
#define rep(i,n) for(int i=0;i<n;++i)
#define rrep(i,n) for(int i=n-1;i>=0;--i)
#define reps(i,a,b) for(int i=a;i<b;++i)
#define fi first
#define se second
#define pb push_back
#define eb emplace_back
#define sz(x) (int)(x).size()
#define all(v) v.begin(),v.end()
#define rall(v) v.rbegin(),v.rend()
using namespace std;
using ll = long long;
using P = pair<int, int>;
template<typename T> inline bool chmax(T &a,T b){if(a<b){a=b;return 1;}return 0;}
template<typename T> inline bool chmin(T &a,T b){if(b<a){a=b;return 1;}return 0;}
template<typename T> vector<T> make_vec(size_t a){return vector<T>(a);}
template<typename T,typename... Ts>
auto make_vec(size_t a,Ts... ts){return vector<decltype(make_vec<T>(ts...))>(a,make_vec<T>(ts...));}
template<typename T,typename U,typename... V>
typename enable_if<is_same<T,U>::value>::type fill_v(U &u,const V... v){u=U(v...);}
template<typename T,typename U,typename... V>
typename enable_if<!is_same<T,U>::value>::type fill_v(U &u,const V... v){for(auto &e:u)fill_v<T>(e,v...);}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int h, w;
    while (cin >> h >> w, h != 0) {
        rep(i, h) cout << string(w, '#') << endl;
        cout << endl;
    }
}



Measured memory_bytes: 3200
#include<iostream>
#include<vector>
#include<algorithm>
#include<set>
#include<map>
#include<unordered_map>
#include<stack>
#include<queue>
#include<iomanip>
#include<math.h>
#include<bitset>
#include<cassert>
#include<random>
#include<time.h>
#include<functional>
using namespace std;
using ll=long long;
using ld=long double;
using pint=pair<int,int>;
using pll=pair<ll,ll>;
#define MOD 1000000007LL
#define INF 1000000000LL
#define EPS 1e-10
#define FOR(i,n,m) for(ll i=n;i<(ll)m;i++)
#define REP(i,n) FOR(i,0,n)
#define DUMP(a) REP(d,a.size()){cout<<a[d];if(d!=a.size()-1)cout<<" ";else cout<<endl;}
#define ALL(v) v.begin(),v.end()
#define UNIQUE(v)  sort(ALL(v));v.erase(unique(ALL(v)),v.end());
#define pb push_back

int main() {
	cin.tie(0);
	ios::sync_with_stdio(false);
	ll n, m;
	cin >> n >> m;
	vector<vector<ll>> g(n);
	vector<vector<ll>> revg(n);
	vector<ll> cnt(n);
	REP(i, n - 1 + m) {
		ll a, b;
		cin >> a >> b;
		a--; b--;
		g[a].pb(b);
		cnt[a]++;
		revg[b].pb(a);
	}
	vector<ll> ans(n, -1);
	queue<ll> q;
	REP(i, n) if(g[i].empty()) q.push(i);
	while(!q.empty()) {
		ll p = q.front();
		q.pop();
		REP(i, revg[p].size()) {
			ll pp = revg[p][i];
			cnt[pp]--;
			if(cnt[pp] == 0) {
				REP(j, g[pp].size()) {
					if(ans[g[pp][j]] == -1) {
						ans[g[pp][j]] = pp + 1;
					}
				}
				q.push(pp);
			}
		}
	}
	REP(i, n) if(ans[i] == -1) ans[i] = 0;
	REP(i, n) cout << ans[i] << endl;
	return 0;
}

/* --------------------------------------- */


Measured memory_bytes: 13312
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <climits>
#include <iostream>
#include <vector>
#include <string>
#include <sstream>
#include <algorithm>
#include <utility>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <deque>
#include <functional>

using namespace std;

#define fst first
#define scd second
#define PB push_back
#define MP make_pair
#define int long long
#define all(a) a.begin(),a.end()
#define rall(a) a.rbegin(),a.rend()
#define omajinai ios::sync_with_stdio(false);cin.tie(0)

typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int, int> pii;
typedef vector<pii> vpii;

template<typename T>T& max(T&a,T&b){if(a>=b)return a;return b;}
template<typename T>T& min(T&a,T&b){if(a<b)return a;return b;}
template<typename T>bool chmax(T&a,T b){if(a<b){a=b;return true;}return false;}
template<typename T>bool chmin(T&a,T b){if(a>b){a=b;return true;}return false;}
template<typename T>T get(){T a;cin>>a;return a;}
template<typename T>T rev(T a){reverse(all(a));return a;}
template<typename T>vector<T>&sort(vector<T>&a){sort(all(a));return a;}

const int inf = 1e9;
const ll linf = 3e18;
const double eps = 1e-9;

int N, M;
int c[16], x[20001];

int dp[20001][256];

signed main()
{
    while (cin >> N >> M, N) {
        for (int i = 0; i <= 20000; ++i) for (int j = 0; j < 256; ++j) dp[i][j] = inf;

        for (int i = 0; i < M; ++i) cin >> c[i];
        for (int i = 1; i <= N; ++i) cin >> x[i];

        dp[0][128] = 0;

        for (int i = 1; i <= N; ++i) {
            for (int j = 0; j < 256; ++j) {
                if (dp[i - 1][j] != inf) {
                    for (int k = 0; k < M; ++k) {
                        int v = (c[k] < 0 ? max(j + c[k], 0ll) : min(j + c[k], 255ll));
                        int w = abs(v - x[i]);
                        chmin(dp[i][v], dp[i - 1][j] + w * w);
                    }
                }
            }
        }

        cout << *min_element(dp[N], dp[N] + 256) << endl;
    }
}

Measured memory_bytes: 41312
import java.util.Scanner;

class Main{

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while(sc.hasNext()){
            int n = sc.nextInt();
            if(n == 0)break;
            int[] array = new int[n];
            for(int i = 0; i < n; i++){
            	array[i] = sc.nextInt();
            }
            int cnt = 0;
            while(true){
                int[] next = new int[n];
                int[] count = new int[999];
                for(int i : array)count[i]++;
                boolean f = true;
                for(int i = 0; i < n; i++){
                    next[i] = count[array[i]];
                    if(next[i] != array[i])f = false;
                }
                if(f){
                	System.out.println(cnt);
                	for(int i = 0; i < array.length -1; i++){
                		System.out.print(array[i] + " ");
                	}
                	System.out.println(array[array.length - 1]);
                    break;
                }
                cnt++;
                array = next;
            }
        }
    }
}


Measured memory_bytes: 32720
#include <bits/stdc++.h>
using namespace std;


using ll = long long;
const int MaxN = 2e5;
const int Mod = 998244353;
int N;
vector<pair<int, int>> vxd;


void solve()
{
    sort(vxd.begin(), vxd.end());
    stack<pair<int, int>> st;
    vector<ll> f(N + 1);
    f[N] = 1;
    for (int i = N - 1; i >= 0; i--) {
        int k = i + 1;
        int x = vxd[i].first + vxd[i].second;
        while (!st.empty() && x > vxd[st.top().first].first) {
            k = st.top().second;
            st.pop();
        }
        st.push(make_pair(i, k));
        f[i] = (f[i + 1] + f[k]) % Mod;
    }
    cout << f[0] << endl;
}


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

    cin >> N;
    vxd.resize(N);
    for (int i = 0; i < N; i++) cin >> vxd[i].first >> vxd[i].second;
    solve();
    return 0;
}


Measured memory_bytes: 4224
import std.stdio, std.algorithm, std.conv, std.array, std.string, std.math, std.typecons, std.numeric;

long[10^^5] AS, BS, DS;

void main()
{
    auto N = readln.chomp.to!int;
    foreach (i, a; readln.split.to!(long[])) AS[i] = a;
    foreach (i, b; readln.split.to!(long[])) BS[i] = b;

    long O, L, cnt;
    foreach (i; 0..N) {
        auto d = AS[i] - BS[i];
        DS[i] = d;
        if (d > 0) {
            O += d;
        } else if (d < 0) {
            L -= d;
            ++cnt;
        }
    }
    if (L > O) {
        writeln("-1");
        return;
    }
    sort!"a > b"(DS[0..N]);
    foreach (d; DS) {
        if (L <= 0) break;
        L -= d;
        ++cnt;
    }
    writeln(cnt);
}

Measured memory_bytes: 8448
#include<bits/stdc++.h>
using namespace std;
#define MAX 7777777
int main(){
  int n,m,t=0;
  bool a[MAX];
  while(cin>>n>>m,n){
    t=0;
    memset(a,false,sizeof(a));
    for(int j=n;j<MAX;j++){
      if(!a[j]){
	for(int k=j;k<MAX;k+=j){
	  a[k]=true;
	}
	t++;
      }
      if(m==1)break;
      if(m!=1&&t==m)break;
    }
    for(int i=n;i<MAX;i++){
      if(!a[i]){
	cout<<i<<endl;
	break;
      }
    }
  }
  return 0;
}

Measured memory_bytes: 10644
S = input()
if S == "SUN":
    ans = 7
elif S == "MON":
    ans = 6
elif S == "TUE":
    ans = 5
elif S == "WED":
    ans = 4
elif S == "THU":
    ans = 3
elif S == "FRI":
    ans = 2
else:
    ans = 1
    
print(ans)

Measured memory_bytes: 2940
#include <iostream>
#include <vector>
#include <queue>
#include <algorithm>
using namespace std;

vector<int> D;
vector<vector<int>> G;
int dfs(int x) {
	int mx = 0;
	if (D[x]) return D[x];

	for (auto y : G[x]) {
		mx = max(mx, dfs(y));
	}

	D[x] = mx + 1;

	return mx + 1;
}

int main ()
{
	int N, M;
	cin >> N >> M;

	G.resize(N + 1);

	for (int i = 0; i < M; i++) {
		int a, b;
		cin >> a >> b;
		G[a].push_back(b);
	}

	D.resize(N + 1, 0);

	int ans = 0;

	for (int i = 1; i <= N; i++) {
		if (!D[i]) ans = max(ans, dfs(i));
	}

	cout << ans - 1 << endl;

}


Measured memory_bytes: 8192
#include <bits/stdc++.h>
using namespace std;

int main(){
	int n, y;
	cin >> n >> y;
	int aEnd = y / 10000;
	for(int a=0; a<=aEnd; a++){
		int aTemp = y - 10000*a;
		int bEnd = aTemp / 5000;
		for(int b=0; b<=bEnd; b++){
			int c = (aTemp - 5000*b)/1000;
			if(a + b + c == n){
				cout << a << " " << b << " " << c << endl;
				return 0;
			}
		}
	}
	cout << "-1 -1 -1" << endl;
	return 0;
}

Measured memory_bytes: 256
n,*b=map(int,open(0).read().split())
a=[0]*n
a[0]=b[0]
a[-1]=b[-1]
for i in range(n-2):
    if b[i+1]>=b[i]:
        a[i+1]=b[i]
    else:
        a[i+1]=b[i+1]
print(sum(a))

Measured memory_bytes: 9196
#include<bits/stdc++.h>
int main()
{
    int a,b,c;
    scanf("%d%d%d",&a,&b,&c);

        int m=b/a;
        if(m>=c&&m!=0)
       {
          printf("%d\n",c);
          
       }
       if(m<c&&m!=0)
        {printf("%d\n",m);
        //printf("He may not be able to be satisfied.\n");
        }
        if(m==0)
            printf("0\n");

}


Measured memory_bytes: 384
import scala.collection.mutable

object Main {
  def main(args: Array[String]): Unit = {
    val n: Int = io.StdIn.readInt()
    val children = Array.fill(n)(Seq(-1,-1))
    val parent: Array[Option[Int]] = Array.fill(n)(None)

    for {
      _ <- 0 until n
      l = io.StdIn.readLine()
    } {
      val Seq(i, c1, c2): Seq[Int] = l.split(" ", 3).map(_.toInt)
      children(i) = Seq(c1, c2)
      if (c1 != -1) parent(c1) = Some(i)
      if (c2 != -1) parent(c2) = Some(i)
    }

    def depth: Memoized[Int, Int] = Memoized {i =>
      parent(i) match {
        case None => 0
        case Some(x) => 1 + depth(x)
      }
    }
    def hight: Memoized[Int, Int] = Memoized {i =>
      children(i) match {
        case Seq(-1, -1) => 0
        case Seq(x,  -1) => 1 + hight(x)
        case Seq(-1,  y) => 1 + hight(y)
        case Seq(x,   y) => 1 + (hight(x) max hight(y))
      }
    }

    for (i <- 0 until n) {
      val p = parent(i).getOrElse(-1)
      val s: Int = if (p == -1) -1 else children(p).filterNot(_ == i).head
      val deg: Int = children(i).filterNot(_ == -1).length
      val t = if (p == -1) "root" else if (deg == 0) "leaf" else "internal node"

      println(s"node $i: parent = $p, sibling = $s, degree = $deg, depth = ${depth(i)}, height = ${hight(i)}, $t")
    }
  }

  case class Memoized[Arg, Ret](f: Arg => Ret) extends (Arg => Ret) {
    val cache: mutable.Map[Arg, Ret] = mutable.Map[Arg, Ret]()
    def apply(arg: Arg): Ret = cache.getOrElseUpdate(arg, f(arg))

  }
}

Measured memory_bytes: 44388