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

int main(){
        string s,t;
        cin >> s;
        t=s[0];
        int sum=0;
        //2^3回ループ
        for (int bit=0; bit<(1<<3); bit++){
                int a;
                //文字列をintに変換
                sum=s[0]-'0';
                t=s[0];
                for (int i=0; i<3; i++){
                        a=s[i+1]-'0';
                        //bitが立ってたら+
                        if(bit & (1<<i)) {
                                sum+=a;
                                t=t+'+'+s[i+1];
                        }else{  //bitが立っていなかったら-
                                sum-=a;
                                t=t+'-'+s[i+1];
                        }
                }
                //和が7なら文字列を出力
                if(sum==7) {
                        cout << t << "=7" << endl;
                        break;
                }
        }
        return 0;
}

Measured memory_bytes: 256
#include<bits/stdc++.h>
using namespace std;
typedef pair<int,int> pii;

int costs[10001];

int main(){
  int n,t,k,case_num = 0;
  while(scanf("%d%d%d",&n,&t,&k), n){
    case_num++;

    memset(costs,0,sizeof(costs));
    vector< vector<pii> > tree(n);

    for(int i=0;i<n-1;i++){
      int a,b,c;
      scanf("%d%d%d",&a,&b,&c);
      a--; b--;
      tree[a].push_back(pii(c,b));
      tree[b].push_back(pii(c,a));
      costs[c]++;
    }

    vector<int> base(t), use(n,0);
    for(int i=0;i<t;i++){
      scanf("%d",&base[i]);
      base[i]--;
      use[base[i]] = 1;
    }

    priority_queue<pii> q;
    for(int v : base){
      for(pii u : tree[v]){
	if(!use[u.second])q.push(u);
      }
    }

    while(q.size()){
      pii p = q.top(); q.pop();
      if(use[p.second])continue;
      use[p.second] = 1;
      costs[p.first]--;

      for(pii u : tree[p.second]){
	if(!use[u.second])q.push(u);
      }
    }

    int p = 0;
    int res = 0;
    while(k){
      int r = min(k,costs[p]);
      res += p*r; k-=r; p++;
    }
    printf("Case %d: %d\n",case_num, res);
  }
}

Measured memory_bytes: 1852
#define Debug
#include<bits/stdc++.h>
#pragma GCC optimize ("Ofast")
#define ll long long int
#define FOR(i,a,b) for(int (i)=(a);i<(int)(b);i++)
#define rep(i,n) FOR(i,0,n)
#define rep1(i,n) FOR(i,1,n)
#define outi(a) printf("%lld\n",(a))
using namespace std;
ll read()
{
  char c = getchar();
  while((c < '0' || c > '9') && c != '-')
  c = getchar();
  ll isk = 1;
  if(c == '-') isk = -1,c = getchar();
  ll sum = 0;
  while(c >= '0' && c <= '9')
  sum = sum*10 + c - '0', c = getchar();
  return sum*isk;
}
int main()
{
  //freopen("input.txt", "r", stdin);
  int arr[3];
   arr[0] = read(), arr[1] = read() , arr[2]= read();
   sort(arr, arr+3);
   ll sum = 0;
   rep1(i,3)
   sum += arr[i] - arr[i-1];
   cout << sum << endl;

}


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

typedef long long ll;

const int INF = 1e9;
const int MOD = 1e9+7;
const ll LLINF = 1e18;

#define YES(n) cout << ((n) ? "YES" : "NO"  ) << endl
#define Yes(n) cout << ((n) ? "Yes" : "No"  ) << endl

#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define FORR(i,a,b) for(int i=(a);i>=(b);i--)

#define ALL(obj) (obj).begin(),(obj).end()

#define pii pair<int,int>

#define pb(a) push_back(a)
#define mp make_pair

ll gcd(ll a, ll b)
{
	if(a % b == 0)return b;
	return gcd(b, a % b);
}

int main()
{
	int n;
	cin >> n;
	ll a[n];
	FOR(i, 0, n)
		cin >> a[i];
	ll g = gcd(a[0], a[1]);
	FOR(i, 2, n)
		g = gcd(g, a[i]);
	cout << g << "\n";
	return 0;
}


Measured memory_bytes: 1024
#include <iostream>
#include <vector>
using namespace std;

int main() {
    int N; cin >> N;
    vector<int> H(N);
    for(int i = 0; i < N; i++) cin >> H[i];

    bool ngFlg = true;
    int rst = 0;
    for(int i = 1; i < N; i++) {
        for(int j = 0; j < i; j++) {
            if(H[i] < H[j]) {
                ngFlg = false;
                break;
            }
        }
        if(ngFlg) rst++;
        ngFlg = true;
    }
    cout << rst+1 << endl;
}

Measured memory_bytes: 256
import numpy as np
MOD = 10**9+7
n = int(input())
dp = np.zeros((n+1, 4, 4, 4), dtype=np.int64)
dp[0, 3, 3, 3] = 1
for i in range(n):
  for a in range(4):
    for b in range(4):
      for c in range(4):
        for d in range(4):
          if d==1:
            if (a==0 and b==2) or (a==0 and c==2) or (b==0 and c==2) or (c==0 and b==2):
              continue
          if b==0 and d==2 and c==1:
            continue
          dp[i+1, b, c, d] += dp[i, a, b, c]
          dp[i+1, b, c, d] %= MOD
print(dp[n].sum()%MOD)

Measured memory_bytes: 12468
using System;
using System.Collections.Generic;

class Program {
    static int Main(string[] args) {
        List<int[]> list = new List<int[]>();

        while (true) {
            string v = Console.ReadLine();

            int H = int.Parse(v.Split(' ')[0]);
            int W = int.Parse(v.Split(' ')[1]);
            if ((H == 0) && (W == 0)) { break; }

            list.Add(new int[] { H, W });
        }

        for (int i = 0; i < list.Count; i++) {
            for (int h = 0; h < list[i][0]; h++) {
                Console.Write(new string('#', list[i][1]));
                Console.WriteLine("");
            }

            Console.WriteLine("");
        }

        return 0;
    }
}

Measured memory_bytes: 9064
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll dp[407][407];
ll arr[407];
ll sum[407];
ll f(ll i,ll j)
{
	if(i==j)
		return 0;
	if(dp[i][j]!=-1)
		return dp[i][j];
	ll a=1e17L;
	for(ll k=i;k<j;k++)
		a=min(a,f(i,k)+f(k+1,j)+sum[j+1]-sum[i]);
	return dp[i][j]=a;
}
int main()
{
	ll n,i;
	cin>>n;
	for(i=0;i<n;i++)
	{
		cin>>arr[i];
		sum[i+1]=arr[i]+sum[i];
	}
	memset(dp,-1,sizeof(dp));
	cout<<f(0,n-1);
	return 0;
}

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

using namespace std;

typedef long long ll;
typedef pair<int, int> ii;
typedef pair<ll, ll> pll;
typedef vector<ii> vii;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vll;
typedef vector<vll> vvll;

#define getBit(x,i) (x&(1<<i))
#define setBit(x,i) (x|(1<<i))
#define mem(A,N) memset(A,N,sizeof(A))
#define pb push_back
#define pf push_front
#define ppb pop_back
#define ppf pop_front
#define mp make_pair
#define rep(i, x, y)  for(int i = x; i < y; ++i)
#define Rep(i, x, y)  for(int i = x; i <= y; ++i)
#define srep(i, x, y) for(int i = x; i > y; --i)
#define sRep(i, x, y) for(int i = x; i >= y; --i)
#define all(v) v.begin(),v.end()
#define Sort(x) sort(x.begin(), x.end())
#define f1(i,n) for (int i=1; i<=n; i++)
#define f0(i,n) for (int i=0; i<n; i++)
#define sz(x) ((int)(x).size())
#define len(x) ((int)(x).length())
#define fi first
#define se second

#define linf 0x7FFFFFFFFFFFFFFF
#define inf 0x7FFFFFFF
#define mod 1000000007
#define eps 1e-9
#define Pi 3.14159265358979323846

//debug
#define dump(x)  cerr << #x << " = " << (x) << endl
#define write(x) cerr << #x << " = " << (x) << ", "
#define writeI0 cerr << "i = " << 0 << ", "
#define dumpI0 cerr << "i = " << 0 << endl
#define printSet(x) for(auto it = s.begin(); it!=s.end(); it++) (it==s.begin())? cout << *it : cout << ' ' << *it; cout << endl;
#define printVector(x) for(auto it = x.begin(); it!=x.end(); it++) (it==x.begin())? cout << *it : cout << ' ' << *it; cout << endl;
#define printArray(a,x,y) for(auto it = a+x; it != a+y; it++) (it==a+x)? cout << *it : cout << ' ' << *it; cout << endl;
#define PrintArray(a,x,y) for(auto it = a+x; it != a+y+1; it++) (it==a+x)? cout << *it : cout << ' ' << *it; cout << endl;

// ***************** End Of Template ********************

int main() {
    ios_base::sync_with_stdio(false); cin.tie(NULL);
    int n; cin >> n;
    int x = n;
    int s = 0;
    while(x != 0) {
        s += x%10;
        x /= 10;
    }
    if(n % s == 0)
        cout << "Yes";
    else
        cout << "No";
    return 0;
}


Measured memory_bytes: 256
from functools import reduce
import math

def main():
    # 文字列の2進数を数値にする
    # '101' → '5'
    # 文字列の頭に'0b'をつけてint()にわたす
    # binary = int('0b'+'101',0)

    # 2進数で立っているbitを数える
    # 101(0x5) → 2
    # cnt_bit = bin(5).count('1')
    
    # N! を求める
    # f = math.factorial(N)

    # N の逆元
    # N_inv = pow(N,MOD-2,MOD)

    # nCr
    # Nの階乗 * rの階乗の逆元 * n-rの階乗の逆元
    
    # 切り捨て
    # 4 // 3
    # 切り上げ
    #-(-4 // 3)
    
    # 初期値用:十分大きい数(100億)
    INF = float("inf")

    # 大きな素数
    MOD = 10**9+7
    
    # 1文字のみを読み込み
    # 入力:2
    # a = input().rstrip()
    # 変数:a='2'
    
    # スペース区切りで標準入力を配列として読み込み
    # 入力:2 4 5 7
    # a, b, c, d = (int(_) for _ in input().split())  
    # 変数:a=2 b=4 c=5 d =7
    
    # 1文字ずつ標準入力を配列として読み込み
    # 入力:2 4 5 7
    # a = list(int(_) for _ in input().split())
    # 変数:a = [2, 4, 5, 7]    

    # 1文字ずつ標準入力を配列として読み込み
    # 入力:2457
    # a = list(int(_) for _ in input())
    # 変数:a = [2, 4, 5, 7]    
    N = int(input())
    ans = 0
    for i in range(1,N+1,2):
        num_of_y = 0
        for j in range(1,N+1):
            if i % j == 0:
                num_of_y += 1
        if num_of_y == 8:
            ans += 1
    print(ans)
    
if __name__ == '__main__':
    main()


Measured memory_bytes: 3572
import java.util.*;
import java.util.Arrays;
import java.util.ArrayList;
import java.util.Set;

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();
        System.out.println((a+b)*h/2);
}
        
        
    }


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

#define REP(i, n) for(int i = 0; i < n; i++)
#define REPR(i, n) for(int i = n; i >= 0; i--)
#define FOR(i, m, n) for(int i = m; i < n; i++)

#define ALL(v) v.begin(), v.end()

int main() {
  std::cout << std::fixed << std::setprecision(20);
  FOR(i,1,6){
    int x;
    cin >> x;
    if(x == 0){
      cout << i << endl;
      return 0;
    }
  }
  return 0;
}

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

#define REP(i, x, n) for (int i = x; i < (int)(n); i++)
#define rep(i, n) REP(i, 0, x);
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()

using namespace std;

int gcd(int a, int b)
{
    if (b == 0)
        return a;
    return gcd(b, a % b);
}

int lcm(int a, int b) { return a * b / gcd(a, b); }

int main(void)
{
    int x, a, b;
    cin >> x >> a >> b;
    if (a - b >= 0)
    {
        puts("delicious");
    }
    else if (b - a <= x)
    {
        puts("safe");
    }
    else
    {
        puts("dangerous");
    }
    return 0;
}

Measured memory_bytes: 256
#include <iostream>
#include <string>
#include <algorithm>
#include <utility>
#include <iomanip>
#include <functional>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <vector>
#include <cmath>
#include <cctype>
#include <cstdio>
#include <bitset>
#include <complex>
#define ll long long
#define INF 1000000000000000001
#define MOD 998244353
using namespace std;


int main(){
    int m1,d1,m2,d2;
    cin >> m1 >> d1 >> m2 >> d2;
    if(m1!=m2){
        cout<<1<<endl;
    }else{
        cout<<0<<endl;
    }
    return 0;
    
}


Measured memory_bytes: 256
def main():
	H, W, A, B = map(int, input().split())
	H, W= H-1, W-1
	DIV = 10**9+7
	size = H+W+1

	fact = [0]*size
	inverse = [0]*size
	inv_cal = [0]*size
	fact[:2] = [1, 1]
	inverse[:2] = [1, 1]
	inv_cal[:2] = [0, 1]

	for i in range(2, size):
		fact[i] = (fact[i-1]*i%DIV)
		inv_cal[i] = (-inv_cal[DIV%i]*(DIV//i))%DIV
		inverse[i] = inverse[i-1]*inv_cal[i]%DIV

	def C(n, r):
		ans = fact[n]*inverse[r]%DIV
		return ans*inverse[n-r]%DIV

	ans = 0
	for x in range(B, W+1):
		y = H-A
		tmp = (C(x+y, x)*C(H+W-x-y-1, W-x))%DIV
		ans += tmp
		ans %= DIV

	print(ans)


if __name__ == '__main__':
	main()

Measured memory_bytes: 26740
#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <sstream>
#include <unordered_map>
#include <vector>


#define INF 4000000000000000000LL
#define MOD 1000000007
#define ALL(x) std::begin(x), std::end(x)


int main(int argc, char** argv)
{
  std::cin.tie(0);
  std::ios_base::sync_with_stdio(0);

  std::cout << std::fixed << std::setprecision(6);
  std::cerr << std::fixed << std::setprecision(6);

  std::string S;

  std::cin >> S;

  std::cout << (S == "ABC" ? "ARC" : "ABC") << std::endl;

  return 0;
}


Measured memory_bytes: 3612
#include "bits/stdc++.h"
using namespace std;
typedef long long ll;

ll abs_diff(ll a, ll k)
{
    if (a > k)
    {
        return a % k;
    }
    else
    {
        return min(a, k - a);
    }
}

int main()
{
    ll n, k;
    cin >> n >> k;

    ll mod = n % k;

    ll cur_min = min(n, k);
    set<ll> history = {n};

    while (true)
    {
        auto next = abs_diff(n, k);
        if (next == 0)
        {
            cout << 0 << endl;
            return 0;
        }

        // ループしていた, または0になれば終了
        if (history.count(next) > 0)
        {
            cout << cur_min << endl;
            return 0;
        }

        if (next <= cur_min)
        {
            cur_min = next;
        }
        history.insert(next);
        n = next;
    }

    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);
		int n = sc.nextInt();
		int[] a = new int[n + 1];
		for(int i = 0; i <= n; i++) a[i] = i;
		for(int i = 6; i <= n; i*= 6) {
			for(int j = i; j <= n; j++) {
				if(a[j] > a[j - i] + 1) a[j] = a[j - i] + 1;
			}
		}
		for(int i = 9; i <= n; i*= 9) {
			for(int j = i; j <= n; j++) {
				if(a[j] > a[j - i] + 1) a[j] = a[j - i] + 1;
			}
		}
		System.out.println(a[n]);
	}
}

Measured memory_bytes: 21972
N, K = map(int,input().split())
L = []
for _ in range(N):
    L.append(list(map(int,input().split())))
L.sort()
for i in range(1,len(L)):
    L[i][1] = L[i][1] + L[i-1][1]
for x in L:
    if K <= x[1]:
        print(x[0])
        break


Measured memory_bytes: 30996
using System;

namespace agc022_a
{
    class Program
    {
        static void Main(string[] args)
        {
            string s = Console.ReadLine();
            if (s == "zyxwvutsrqponmlkjihgfedcba") Console.WriteLine(-1);
            else if (s.Length == 26)
            {
                bool found = false;
                for (int i = s.Length - 1; !found; i--)
                {
                    char j = (char)(s[i] + 1);
                    for (; j <= 'z'; j++)
                    {
                        if (s.Substring(0,i).IndexOf(j) < 0) break;
                    }
                    if (j <= 'z' && s.Substring(0,i).IndexOf(j) < 0)
                    {
                        found = true;
                        Console.WriteLine(s.Substring(0,i) + j);
                    }
                }
            }
            else
            {
                bool[] words = new bool[26];
                for (int i = 0; i < s.Length; i++)
                {
                    words[s[i] - 'a'] = true;
                }
                Console.Write(s);
                int a = 0;
                while (words[a]) a++;
                Console.WriteLine((char)('a' + a));
            }
        }
    }
}

Measured memory_bytes: 11092
x,y,z,k = map(int,input().split())

a = list(map(int,input().split()))

b = list(map(int,input().split()))

c = list(map(int,input().split()))

mid = []

for i in range(x):
    for j in range(y):
        mid.append(a[i]+b[j])

mid.sort(reverse=True)

ans = []

for i in range(min(k,len(mid))):
    for j in range(z):
        ans.append(mid[i]+c[j])

ans.sort(reverse=True)

for i in range(k):
    print(ans[i])

Measured memory_bytes: 214712
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> P;
int main(){
  ll n,k,a[200010],s[200010],i;
  cin >> n >> k;
  for(i=0;i<n;i++){
    cin >> a[i];
  }
  s[0]=0;
  for(i=0;i<n;i++){
    s[i+1]=s[i]+a[i];
    s[i+1]=s[i+1]%k;
  }
  ll x[200010];
  map<ll,ll> mp;
  for(i=0;i<n+1;i++){
    x[i]=s[i]+n-i;
    x[i]=x[i]%k;
    mp[x[i]]++;
  }
  ll ans=0;
  for(i=0;i<n;i++){
    if(mp[x[i]]!=0){
      ans+=mp[x[i]]*(mp[x[i]]-1)/2;
      mp[x[i]]=0;
    }
  }
  ll ans1=0;
  map<ll,ll> mp1;
  for(i=k;i<n+1;i++){
    mp1[x[i]]++;
  }
  for(i=0;i<=n-k;i++){
    if(mp1[x[i]]!=0){
      ans1+=mp1[x[i]];
    }
    mp1[x[i+k]]--;
  }
  cout << ans-ans1 << endl;
}

Measured memory_bytes: 20792
#include <bits/stdc++.h>
#define f first
#define s second
#define fore(i,a,b) for(int i = (a), ThxMK = (b); i < ThxMK; ++i)
#define pb push_back
#define all(s) begin(s), end(s)
#define _ ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define sz(s) int(s.size())
#define ENDL '\n'
using namespace std;
typedef long double ld;
typedef long long lli;
typedef pair<int,int> ii;
typedef vector<int> vi;
typedef vector<lli> vl;
typedef vector<ii> vii;
typedef vector<vi> vvi;
typedef vector<bool> vb;
#define deb(x) cout << #x": " << (x) << endl;

const int N = 1e3+5;
const lli INF = 1e18;
const lli MOD = 1e9+7;

bool mm[N];
vi primes(1,2);
ld nk[N][N];
void sieve(){
	for(int i = 4; i<N; i+=2)mm[i]=1;
	for(int i = 3; i*i<=N; i+=2)if(!mm[i])for(int j = i*i; j<N; j+=i)mm[j]=1;
	for(int i = 3; i<N; i+=2)if(!mm[i])primes.pb(i);
}

void pascal(){
	fore(i,0,N)nk[i][0]=nk[i][i]=1.0;
	fore(i,1,N)fore(j,1,i)nk[i][j]=nk[i-1][j-1]+nk[i-1][j];
}

lli gcd(lli a, lli b){return (b?gcd(b,a%b):a);}

lli lcm(lli a, lli b){
	if(a<b)swap(a,b);
	lli c = gcd(a,b);
	a/=c;
	return a*b;
}

int popcount(lli x) { return __builtin_popcountll(x); }

lli poww(lli a, lli b){
	lli res =1;
	while(b){
		if(b&1) res = res * a;
		a = a*a;
		b/=2;
	}
	return res;
}

lli powm(lli a, lli b){
	lli res =1;
	while(b){
		if(b&1) res = (res * a)%MOD;
		a = (a*a)%MOD;
		b/=2;
	}
	return res;
}

bool isPrime(lli x){
	if(x==1)return 0;
	for(auto i:primes){
		if(i*i>x)return 1;
		if(x%i==0)return 0;
	}
	return 1;
}

// ---- コーディングはここから！ ('-')7

int main(){_
int x,n,a; cin>>x>>n;
vi v (105,0);
fore(i,0,n){cin>>a; v[a]++;}
	fore(i,0,x+1){
		if(x-i>=0 and !v[x-i]){cout<<x-i<<ENDL; return 0;}
		if(!v[x+i]){cout<<x+i<<ENDL; return 0;}
	}
}


Measured memory_bytes: 3660
#include<bits/stdc++.h>
using namespace std;
using ll = long long;

int main(){
        cout << fixed << setprecision(10);
        ll n; cin >> n;
        vector<double> p(n);
        for(auto &m : p) cin >> m;
        vector<vector<double>> dp(n+1, vector<double>(n+1));
        dp[0][0] = 1;
        for(ll i = 1; i <= n; i++) dp[i][0] = dp[i-1][0] * (1-p[i-1]);
        for(ll i = 1; i <= n; i++) for(ll j = 1; j <= i; j++){
                dp[i][j] = p[i-1]*dp[i-1][j-1] + (1-p[i-1])*dp[i-1][j];
        }
        double res = 0;
        for(ll i = n; 2*i > n; i--){
                res += dp[n][i];
        }
        cout << res << endl;
}


Measured memory_bytes: 70784
#include "bits/stdc++.h"

#ifdef WINT_MIN
#define __MAI
#endif

using namespace std;
typedef unsigned int uint;
typedef long long int ll;
typedef unsigned long long int ull;

#define debugv(v) printf("L%d %s => ",__LINE__,#v);for(auto e:v){cout<<e<<" ";}cout<<endl;
#define debugm(m) printf("L%d %s is..\n",__LINE__,#m);for(auto v:m){for(auto e:v){cout<<e<<" ";}cout<<endl;}
#define debuga(m,w) printf("L%d %s is => ",__LINE__,#m);for(int x=0;x<(w);x++){cout<<(m)[x]<<" ";}cout<<endl;
#define debugaa(m,w,h) printf("L%d %s is..\n",__LINE__,#m);for(int y=0;y<(h);y++){for(int x=0;x<(w);x++){cout<<(m)[x][y]<<" ";}cout<<endl;}
#define debugaar(m,w,h) printf("L%d %s is..\n",__LINE__,#m);for(int y=0;y<(h);y++){for(int x=0;x<(w);x++){cout<<(m)[y][x]<<" ";}cout<<endl;}
#define ALL(v) (v).begin(),(v).end()
#define repeat(l) for(auto cnt=0;cnt<(l);++cnt)
#define BIGINT 0x7FFFFFFF
#define E107 1000000007ll
void printbit(int u) { if (u == 0)cout << 0; else { int s = 0, k = 0; for (; 0<u; u >>= 1, k++)s = (s << 1) | (u & 1); for (; 0<k--; s >>= 1)cout << (s & 1); } }template<typename T1, typename T2>
    ostream& operator <<(ostream &o, const pair<T1, T2> p) { o << "(" << p.first << ":" << p.second << ")"; return o; }

#define TIME chrono::system_clock::now()
#define MILLISEC(t) (chrono::duration_cast<chrono::milliseconds>(t).count())

namespace {
    std::chrono::system_clock::time_point t;
    void tic() { t = TIME; }
    void toc() { fprintf(stderr, "TIME : %lldms\n", MILLISEC(TIME - t)); }
    std::chrono::system_clock::time_point tle = TIME;
#ifdef __MAI
    void safe_tle(int msec) { assert(MILLISEC(TIME - tle) < msec); }
#else
#define safe_tle(k) ;
#endif
}

#ifndef __MAI 
namespace {
    class MaiScanner {
    public:
        template<typename T>
        void input_integer(T& var) {
            var = 0;
            T sign = 1;
            int cc = getchar_unlocked();
            for (; cc<'0' || '9'<cc; cc = getchar_unlocked())
                if (cc == '-') sign = -1;
            for (; '0' <= cc&&cc <= '9'; cc = getchar_unlocked())
                var = (var << 3) + (var << 1) + cc - '0';
            var = var*sign;
        }
        void ign() { getchar_unlocked(); }
        MaiScanner& operator>>(int& var) {
            input_integer<int>(var);
            return *this;
        }
        MaiScanner& operator>>(long long& var) {
            input_integer<long long>(var);
            return *this;
        }
    };
}
MaiScanner scanner;
#else
#define scanner cin
#endif

int width, height;
int m, n;

int main() {
    int i, j, k;
    int x, y, a, b;

    scanner >> n;

    int result = 1;
    x = 0;
    b = 0;
    repeat(n) {
        scanner >> a;
        if (cnt == 0) b = a;
        if (x == 1) {
            if (b > a) { ++result; x = 0; }
        }
        else if (x == -1) {
            if (b < a) { ++result; x = 0; }
        }
        else {
            if (b < a) x = 1;
            if (b > a) x = -1;
        }
        b = a;
    }
    cout << result << endl;

    return 0;
}

Measured memory_bytes: 256
#include<iostream>
#include<string.h>
#include<algorithm>

#define MAX 5000
using namespace std;

bool table[MAX + 1][MAX + 1];
int x[MAX + 1], y[MAX + 1];
int n;

int square();
bool cheek(int,int);
int input();

int main() {
	while (input() != -1) {

		cout << square() << endl;
		//cin >> n;
	}
	return 0;

}

int input() {
	if (cin >> n, n == 0) return -1;
	memset(table, false, sizeof(table));
	for (int i = 0; i < n; i++) {
		cin >> x[i] >> y[i];
		table[x[i]][y[i]] = true;
	}
}

int square() {
	int ans = 0;
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < n; j++) {
			int xx = x[i] - x[j];
			int yy = y[i] - y[j];
			if (cheek(x[i] + yy, y[i] - xx) == true && cheek(x[j] + yy, y[j] - xx) == true) {
				ans = max(ans, xx*xx + yy * yy);
			}
		}

	}
	return ans;
}

bool cheek(int x,int y) {
	if (x >= 0 && x <= 5000 && y >= 0 && y <= 5000) {
		if (table[x][y] == true) return true;
	}
	return false;
}




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

class Block{
public:
  char block[2][2];
  Block(char a,char b,char c,char d){
    block[0][0]=a;
    block[0][1]=b;
    block[1][0]=c;
    block[1][1]=d;
  }
  bool empty(){
    if(block[0][0]=='.' && block[0][1]=='.' && 
       block[1][0]=='.' && block[1][1]=='.' )return true;
    return false;
  }
  bool full(){
    if(block[0][0]=='#' && block[0][1]=='#' && 
       block[1][0]=='#' && block[1][1]=='#' )return true;
    return false;
  }
};

int h,n,ans;
vector<Block> N;
int X[9]={0,1,0,-1,0,1,1,-1,-1};
int Y[9]={-1,0,1,0,0,1,-1,1,-1};

bool in(int a,int b){
  if(a<0 || b<0 || 2<=a || 2<=b)return false;
  return true;
}

Block move(Block B,int k){
  if(k==4)return B;
  Block res('.','.','.','.');
  for(int i=0;i<2;i++){
    for(int j=0;j<2;j++){
      int a=i+Y[k],b=j+X[k];
      if(in(a,b) && B.block[i][j]=='#')res.block[a][b]='#';
    }
  }
  return res;
}

vector<Block> fall(vector<Block> H,Block f,Block s){
  H.push_back(Block('.','.','.','.'));
  for(int i=H.size()-1;i>0;i--){
    bool flag=false;
    for(int j=0;j<2;j++){
      for(int k=0;k<2;k++){
	if(f.block[j][k]=='#' && H[i-1].block[j][k]=='#')flag=true;
        if(s.block[j][k]=='#' && H[i].block[j][k]=='#')flag=true;
      }
    }
    if(flag){
      if(i==H.size()-1){
	H.pop_back();
	H.push_back(f);
	H.push_back(s);
      }
      else {
	for(int j=0;j<2;j++){
	  for(int k=0;k<2;k++){
	    if(f.block[j][k]=='#')H[i].block[j][k]='#';
	    if(s.block[j][k]=='#')H[i+1].block[j][k]='#';
	  }
	}
      }
      break;
    }
  }
  return H;
}

void rec(vector<Block> H, int index,int sum){
  if(n*2==index){
    /*
      cout<<sum<<endl;
      for(int i=0;i<H.size();i++){
      cout<<H[i].block[0][0]<<H[i].block[0][1]<<endl;
      cout<<H[i].block[1][0]<<H[i].block[1][1]<<endl;;
      cout<<endl;
      }*/
    ans=max(ans,sum);
    return;
  }
  else {
    for(int k=0;k<9;k++){
      bool flag=true;
      for(int i=0;i<2;i++){
	for(int j=0;j<2;j++){
	  int ni=i+Y[k],nj=j+X[k];
	  if(N[index].block[i][j]=='#' && !in(ni,nj))flag=false;
	  if(N[index+1].block[i][j]=='#' && !in(ni,nj))flag=false;
	}
      }
      if(flag){
	Block f=move(N[index],k);
	Block s=move(N[index+1],k);
	vector<Block> ch=H;
	int count=0;
	ch=fall(ch,f,s);
	int size=ch.size();
	for(int j=2;j<size;j++){
	  if(ch[j].full()){
	    ch.erase(ch.begin()+j);
	    size=ch.size();
	    j--;
	    count++;
	  }
	}
	rec(ch,index+2,sum+count);
      }
    }
  }
}

int main()
{
  string a,b;
  vector<Block> H;

  while(1){
    cin>>h>>n;
    if(h+n==0)break;
    H.clear();
    N.clear();
    ans=0;
    H.push_back(Block('#','#','#','#'));
    H.push_back(Block('#','#','#','#'));
    for(int i=0;i<h;i++){
      cin>>a>>b;
      H.push_back(Block(a[0],a[1],b[0],b[1]));
    }
    for(int i=0;i<n*2;i++){
      cin>>a>>b;
      N.push_back(Block(a[0],a[1],b[0],b[1]));
    }
    
    rec(H,0,0);
    
    cout<<ans<<endl;
  }
  return 0;
}

Measured memory_bytes: 1212
#define _CRT_SECURE_NO_WARNINGS

#pragma GCC optimize("O3")

#include <iostream>
#include <algorithm>
#include <vector>
#include <cassert>
#include <cstring>
#include <numeric>
#include <set>
#include <queue>
#include <deque>
#include <array>
#include <string>
#include <cmath>
#include <random>
#include <ctime>
#include <map>
#include <functional>
#include <bitset>
#include <complex>
#include <iomanip>
#include <unordered_map>
#include <unordered_set>

#define sz(a) (int)((a).size())
#define all(a) (a).begin(), (a).end()
#define pb push_back

using namespace std;
using ll = long long;
using vi = vector<int>;
using pii = pair<int, int>;
using ld = long double;

int main() {
	ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
	struct Seg {
		int l, r, a;
	};
	int n, m;
	cin >> n >> m;
	vector<Seg> ss(m);
	vector<vi> st(n + 1), en(n + 1);
	for(int i = 0; i < m; i++) {
		auto& s = ss[i];
		cin >> s.l >> s.r >> s.a;
		st[s.l].pb(i);
		en[s.r].pb(i);
	}
	struct SegTree {
		struct Node {
			ll mx, ad;
		};
		Node merge(const Node& a, const Node& b) {
			return { max(a.mx, b.mx), 0 };
		};
		vector<Node> t;
		SegTree(int n) : t(4 * n, { 0, 0 }) {}
		void push(int v) {
			if (t[v].ad) {
				ll x = t[v].ad;
				t[v].ad = 0;
				t[v << 1].ad += x;
				t[v << 1].mx += x;
				t[v << 1 | 1].ad += x;
				t[v << 1 | 1].mx += x;
			}
		}
		void upd(int v, int l, int r, int l1, int r1, ll x) {
			if (r < l1 || r1 < l) return;
			if (l1 <= l && r <= r1) {
				t[v].ad += x;
				t[v].mx += x;
				return;
			}
			push(v);
			int m = (l + r) >> 1;
			upd(v << 1, l, m, l1, r1, x);
			upd(v << 1 | 1, m + 1, r, l1, r1, x);
			t[v] = merge(t[v << 1], t[v << 1 | 1]);
		}
		const ll inf = 1e18 + 228;
		ll get(int v, int l, int r, int l1, int r1) {
			if (r < l1 || r1 < l) {
				return -inf;
			}
			if (l1 <= l && r <= r1) {
				return t[v].mx;
			}
			push(v);
			int m = (l + r) >> 1;
			return max(get(v << 1, l, m, l1, r1), get(v << 1 | 1, m + 1, r, l1, r1));
		}
	};
	SegTree t(n + 1);
	ll ans = 0;
	for (int i = 1; i <= n; i++) {
		for (int s : st[i]) {
			t.upd(1, 0, n, 0, i - 1, ss[s].a);
		}
		ll dp = t.get(1, 0, n, 0, i - 1);
		ans = max(ans, dp);
		t.upd(1, 0, n, i, i, dp);
		for (int s : en[i]) {
			t.upd(1, 0, n, 0, ss[s].l - 1, -ss[s].a);
		}
	}
	cout << ans;
}


Measured memory_bytes: 36992
package main

import (
	"fmt"
)

func main() {
	var n, mochi int
	fmt.Scan(&n)
	a := make([]int, n)
	b := make(map[int]int)
	c := make(map[int]int)
	for i := 0; i < n; i++ {
		fmt.Scan(&mochi)
		a[i] = mochi
	}
	for i := 0; i < n; i++ {
		fmt.Scan(&mochi)
		b[i] = mochi
	}
	for i := 0; i < n-1; i++ {
		fmt.Scan(&mochi)
		c[i] = mochi
	}
	gokei := 0
	for i, v := range a {
		gokei += b[v-1]
		if i > 0 {
			if a[i]-a[i-1] == 1 {
				gokei += c[a[i-1]-1]
			}
		}
	}
	fmt.Println(gokei)
}


Measured memory_bytes: 512
#include <iostream>
#include <vector>
#include <algorithm>
#include <numeric>

using namespace std;


int main(void)
{
    string s;
    cin >> s;

    if (s[0] == '7' || s[1] == '7' || s[2] == '7')
    {
        cout << "Yes" << endl;
    }
    else {
        cout << "No"<< endl;
    }
    
    return 0;
}


Measured memory_bytes: 3628
/*
 * A.cpp
 *
 *  Created on: 2017/05/06
 *      Author: jln
 */
#define MYDEBUG
#include <iostream>
#include <vector>
#include <algorithm>
#include <map>
#include <unordered_map>
#include <cstdio>
#include <queue>
#include <stack>

#ifdef MYDEBUG
#define debug(x) cout<<#x<<": "<<x<<endl
#else
#define debug(x)
#endif
#define ll long long
#define ull unsigned long long
#define eps 1e-14
#define all(x) x.begin(), x.end()
#define umap unordered_map
using namespace std;

template<typename T>
ostream& operator<<(ostream& out, const vector<T>& v) {
	out << "[";
	size_t last = v.size() - 1;
	for (size_t i = 0; i < v.size(); ++i) {
		out << v[i];
		if (i != last)
			out << ",";
	}
	out << "]";
	return out;
}
int n, w, d;

void solve() {
	while (cin >> n >> w >> d, (n || w || d)) {
		vector<pair<int, int> > v;
		v.push_back(make_pair(0, 0));
		v.push_back(make_pair(w, d));
		for (int i = 1; i <= n; ++i) {
			int p, s;
			cin >> p >> s;
			//v[p]??????????????????(1<=p<=i)
			int wp = v[p].first, dp = v[p].second;
			int r = s % (wp + dp);
			if (r < wp) { //??????
				int w1 = r, d1 = dp, w2 = wp - w1, d2 = dp;
				if (w1 * d1 < w2 * d2) {
					v.erase(v.begin() + p, v.begin() + p + 1); //?????±???????¶????
					v.push_back(make_pair(w1, d1));
					v.push_back(make_pair(w2, d2));
				} else {
					v.erase(v.begin() + p, v.begin() + p + 1); //?????±???????¶????
					v.push_back(make_pair(w2, d2));
					v.push_back(make_pair(w1, d1));
				}
			} else { //??????
				int w1 = wp, d1 = r - wp, w2 = wp, d2 = dp - d1;
				if (w1 * d1 < w2 * d2) {
					v.erase(v.begin() + p, v.begin() + p + 1); //?????±???????¶????
					v.push_back(make_pair(w1, d1));
					v.push_back(make_pair(w2, d2));
				} else {
					v.erase(v.begin() + p, v.begin() + p + 1); //?????±???????¶????
					v.push_back(make_pair(w2, d2));
					v.push_back(make_pair(w1, d1));
				}
			}
		}
		vector<int> ans;
		for (size_t i = 1; i < v.size(); ++i) {
			ans.push_back(v[i].first * v[i].second);
		}
		sort(all(ans));
		for (size_t i = 0; i < ans.size(); ++i) {
			cout << ans[i];
			if (i != ans.size() - 1)
				cout << " ";
		}
		cout << endl;
	}
}

int main() {
	solve();
	return 0;
}

Measured memory_bytes: 3088
# coding: utf-8

import sys
import math
import collections
import itertools
import bisect
INF = 10 ** 13
MOD = 10 ** 9 + 7
def input() : return sys.stdin.readline().strip()
def lcm(x, y) : return (x * y) // math.gcd(x, y)
def I() : return int(input())
def LI() : return [int(x) for x in input().split()]
def RI(N) : return [int(input()) for _ in range(N)]
def LRI(N) : return [[int(x) for x in input().split()] for _ in range(N)]
def LS() : return input().split()
def RS(N) : return [input() for _ in range(N)]
def LRS(N) : return [input().split() for _ in range(N)]
def PL(L) : print(*L, sep="\n")
def YesNo(B) : print("Yes" if B else "No")
def YESNO(B) : print("YES" if B else "NO")

X = I()

P = [pow(i, 5) for i in range(1000000)]

m = False
i = 0
j = 0
for q, a in enumerate(P):
    s1 = bisect.bisect_left(P, a - X)
    s2 = bisect.bisect_left(P, X - a)

    if a - X == P[s1]:
        i = q
        j = s1
        break
    if X - a == P[s2]:
        i = q
        j = s2
        m = True
        break

print(i, end=" ")
print("-" if m else "", end ="")
print(j)


Measured memory_bytes: 227744
import java.util.Scanner;

public class Main {
  final static char WHITE = '.';
  final static char BLACK = '#';

  public static void main (String[] args) {
    Scanner in = new Scanner(System.in);

    while (in.hasNext()) {
      int N = in.nextInt();
      char[] S = in.next().toCharArray();

      int[] blackBefore = new int[N];
      blackBefore[0] = 0;
      for (int i = 1; i < blackBefore.length; ++i) {
        blackBefore[i] = blackBefore[i - 1] + (S[i - 1] == BLACK ? 1 : 0);
      }

      int[] whiteAfter = new int[N];
      whiteAfter[whiteAfter.length - 1] = 0;
      for (int i = whiteAfter.length - 2; i >= 0; --i) {
        whiteAfter[i] = whiteAfter[i + 1] + (S[i + 1] == WHITE ? 1 : 0);
      }
      
      int minChange = Integer.MAX_VALUE;
      for (int i = 0; i < blackBefore.length; ++i) {
        minChange = Math.min(minChange, blackBefore[i] + whiteAfter[i]);
      }

      System.out.println(minChange);
    }
  }
}

Measured memory_bytes: 28764
#include "bits/stdc++.h"

using namespace std;

#define sp(x) cout<<setprecision(x);
#define FOR(i,a,b) for(int i=(a);i<(b);i++)
#define REP(i,n) FOR(i,0,n)
#define all(a) (a).begin(), (a).end()
#define inf 10000000
#define linf INT64_MAX*0.99
#define print(s) cout<<(s)<<endl
#define lint long long
#define yes "Yes"
#define no "No"

typedef pair<int, int> P;
 
int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    vector<int> a(3);
    cin>>a[0]>>a[1]>>a[2];
    sort(all(a));
    print(a[2]-a[0]);
    return 0;
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
signed main(){
  char a[100];
  cin>>a;
  if(a[0]=='Y' && a[1]=='A' && a[2]=='K' && a[3]=='I')
    cout<<"Yes"<<endl;
  else
    cout<<"No"<<endl;
  return(0);
}


Measured memory_bytes: 256
from collections import defaultdict
Q = int(input())
l_list = list()
r_list = list()
for _ in range(Q):
    left, right = list(map(int, input().split(' ')))
    l_list.append(left)
    r_list.append(right)
MAX_N = max(r_list)
# Make number dict for check prime or not
prime_list = list(range(2, MAX_N + 1))
prime_index = 0
n = prime_list[prime_index]
while n**2 <= MAX_N:
    prime_list = list(filter(lambda x: x == n or x % n != 0, prime_list))
    prime_index += 1
    n = prime_list[prime_index]
prime_dict = defaultdict(int)
for p in prime_list:
    prime_dict[p] = 1
# Calculate number of 2017-like-numbers.
sum_dict = defaultdict(int)
for n in range(MAX_N + 1):
    sum_dict[n] += sum_dict[n - 1]
    if n % 2 == 0:
        continue
    nn = (n + 1) // 2
    if prime_dict[n] == 1 and prime_dict[nn] == 1:
        # `n` and `nn` are 2017-like
        sum_dict[n] += 1
# Answer all queries
for left, right in zip(l_list, r_list):
    print(sum_dict[right] - sum_dict[left - 1])


Measured memory_bytes: 28856
N = int(input())
W = [input() for _ in range(N)]

if len(W) == len(set(W)):
    accept = True
    lw = W[0][-1]
    for w in W[1:]:
        if lw != w[0]:
            accept = False
            break
        else:
            lw = w[-1]
    if accept:
        print("Yes")
    else:
        print("No")
else:
    print("No")

Measured memory_bytes: 3064
//minamoto
#include<bits/stdc++.h>
#define R register
#define fp(i,a,b) for(R int i=(a),I=(b)+1;i<I;++i)
#define fd(i,a,b) for(R int i=(a),I=(b)-1;i>I;--i)
#define go(u) for(int i=head[u],v=e[i].v;i;i=e[i].nx,v=e[i].v)
template<class T>inline bool cmax(T&a,const T&b){return a<b?a=b,1:0;}
template<class T>inline bool cmin(T&a,const T&b){return a>b?a=b,1:0;}
using namespace std;
typedef long long ll;
const int N=5e5+5;
char s[N],t[N];int nxt[N][26],las[26],n,m,res;
int main(){
	scanf("%s%s",s+1,t+1),n=strlen(s+1),m=strlen(t+1);
	fd(i,n,1){
		fp(j,0,25)nxt[i][j]=las[j];
		las[s[i]-'a']=i;
	}
	fd(i,n,1){
		fp(j,0,25)nxt[i][j]=las[j];
		las[s[i]-'a']=i;
	}
	fp(j,0,25)nxt[0][j]=las[j];
	R int pos=0;
	fp(i,1,m){
		if(!nxt[pos][t[i]-'a'])return puts("-1"),0;
		res+=(nxt[pos][t[i]-'a']<=pos),pos=nxt[pos][t[i]-'a'];
	}
	printf("%lld\n",1ll*res*n+pos);
	return 0;
}

Measured memory_bytes: 12672
#include <iostream>
#include <cstdio>
#include <string>
#include <algorithm>
#include <utility>
#include <cmath>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#define rep(i, n) for(int i = 0; i < n; i++)
using namespace std;
typedef long long ll;
const ll mod = 1000000007;

int main() {
    int n, m;
    cin >> n >> m;

    if(m <= n) {
        cout << 0 << endl;
        return 0;
    }

    int x[m];
    rep(i, m) {
        cin >> x[i];
    }
    sort(x, x+m);

    int L[m-1];
    rep(i, m-1) {
        L[i] = x[i+1] - x[i];
    }
    sort(L, L+m-1);
    reverse(L, L+m-1);

    int ans = x[m-1] - x[0];
    rep(i, n-1) {
        ans -= L[i];
    }

    cout << ans << endl;
    return 0;
}

Measured memory_bytes: 1024
#include <iostream>
#include <string>
#include <string.h>
using namespace std;

int main()
{
    string S;
    cin >> S;

    int len = 0;
    int maxLen = 0;
    for (int i = 0; i < (int)S.size(); i++)
    {
        if (S[i] == 'A' || S[i] == 'C' || S[i] == 'G' || S[i] == 'T')
        {
            len++;
        }
        else
        {
            if (len > maxLen)
                maxLen = len;
            len = 0;
        }
    }

    if (len > maxLen)
        maxLen = len;

    cout << maxLen << endl;
    return 0;
}

Measured memory_bytes: 256
a, b, c = map(int, input().strip().split(" "))
m = max(a, b, c)

if (m*3) % 2 == (a+b+c) % 2:
    M = m * 3
else:
    M = (m+1) * 3

M -= (a+b+c)
print(M//2)

Measured memory_bytes: 3060
macro_rules! input {
    (source = $s:expr, $($r:tt)*) => {
        let mut iter = $s.split_whitespace();
        let mut next = || { iter.next().unwrap() };
        input_inner!{next, $($r)*}
    };
    ($($r:tt)*) => {
        let stdin = std::io::stdin();
        let mut bytes = std::io::Read::bytes(std::io::BufReader::new(stdin.lock()));
        let mut next = move || -> String{
            bytes
                .by_ref()
                .map(|r|r.unwrap() as char)
                .skip_while(|c|c.is_whitespace())
                .take_while(|c|!c.is_whitespace())
                .collect()
        };
        input_inner!{next, $($r)*}
    };
}

macro_rules! input_inner {
    ($next:expr) => {};
    ($next:expr, ) => {};

    ($next:expr, $var:ident : $t:tt $($r:tt)*) => {
        let $var = read_value!($next, $t);
        input_inner!{$next $($r)*}
    };
}

macro_rules! read_value {
    ($next:expr, ( $($t:tt),* )) => {
        ( $(read_value!($next, $t)),* )
    };

    ($next:expr, [ $t:tt ; $len:expr ]) => {
        (0..$len).map(|_| read_value!($next, $t)).collect::<Vec<_>>()
    };

    ($next:expr, chars) => {
        read_value!($next, String).chars().collect::<Vec<char>>()
    };

    ($next:expr, usize1) => {
        read_value!($next, usize) - 1
    };

    ($next:expr, $t:ty) => {
        $next().parse::<$t>().expect("Parse error")
    };
}

fn main() {
    input!{
        n: usize,
        m: usize,
    }
    if n == m {
        println!("Yes")
    } else {
        println!("No")
    }
}

Measured memory_bytes: 4352
#include <algorithm>
#include <iostream>
#include <vector>
#include <math.h>
#include <set>
#include <map>
#include <string>
#include <stack>
#include <queue>
#include <iomanip>
#include <numeric>
#include <tuple>
#include <bitset>
#include <complex>
#include <unistd.h>
#include <cassert>
#include <cctype>
#include <random>
#include <time.h>
#define _USE_MATH_DEFINES
#define _GLIBCXX_DEBUG
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> plglg;
typedef pair<double, ll> pdlg;
typedef tuple<int, int, int> tiii;
typedef tuple<ll, ll, ll> tlglglg;
typedef tuple<double, double, double> tddd;
typedef complex<double> xy_t;
typedef vector<ll> vll;
typedef vector< vector<ll> > matrix;
#define REP(i, x, y) for(ll i = (ll)x; i < (ll)y; i++)
#define DREP(i, x, y, d) for(ll i = (ll)x; i < (ll)y; i += (ll)d)
#define PER(i, x, y) for(ll i = (ll)x; i > (ll)y; i--)
#define DPER(i, x, y, d) for(ll i = (ll)x; i > (ll)y; i -= (ll)d)
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
double pi = 3.141592653589793;
ll mod = 1000000007;
int intmax = 2147483647;
int intmin = -2147483648;
ll llmax = 9223372036854775807;
ll llmin = -9223372036854775807;
int iinf = intmax / 8;
ll inf = llmax / 8;
double eps = 1e-12;

bool bo[1010][1010];

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    ll H, W, A, B;
    cin >> H >> W >> A >> B;
    ll num = H * A;
    ll ok = true;
    REP(i, 0, 1010) {
        fill(bo[i], bo[i] + 1010, false);
    }
    REP(i, 0, H) {
        REP(j, 0, W) {
            ll num = 0;
            if (i < B) {
                num++;
            }
            if (j < A) {
                num++;
            }
            if (num == 0 || num == 2) {
                bo[i][j] = true;
            } else {
                bo[i][j] = false;
            }
        }
    }
    if (!ok) {
        cout << "No" << endl;
    } else {
        REP(i, 0, H) {
            string s = "";
            REP(j, 0, W) {
                if (bo[i][j]) {
                    s += "1";
                } else {
                    s += "0";
                }
            }
            cout << s << endl;
        }
    }
}


Measured memory_bytes: 2304
#include<bits/stdc++.h>
#define ll long long int
#define mk make_pair
#define pb push_back
#define INF (ll)1e18
#define pii pair<ll,ll>
#define mod 1000000007 //998244353
#define f(i,a,b) for(ll i=a;i<b;i++)
#define fb(i,a,b) for(ll i=a;i>b;i--)
#define ff first
#define ss second
#define PI 3.141592653589793238
#define pq priority_queue<ll>
#define pqr priority_queue<ll,vector<ll>,greater<ll>()>
using namespace std;
ll pow_mod(ll a,ll b)
{
	ll res=1;
	while(b!=0)
	{
		if(b&1)
		{
			res=(res*a)%mod;
		}
		a=(a*a)%mod;
		b/=2;
	}
	return res;
}
void solve()
{
  ll x,k,d;
  
  cin>>x>>k>>d;
  
  x=abs(x);
  
  ll ans=0;
  
  ll need=(x-x%d)/d;
  
  if(need>=k){
      ans=x-k*d;
  }
  else{
      k-=need;
      
      if(k%2)
      ans=d-x%d;
      else
      ans=x%d;
  }
  
  cout<<ans<<endl;
}
int32_t main()
{    ios_base::sync_with_stdio(false);
     cin.tie(NULL);
     cout.tie(NULL);
     
//Start from Here.

  ll t;
  t=1;
 // cin>>t;
  while(t--)
  solve();
  
//Good Bye!
    return 0;
}

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

using namespace std;
typedef long long ll;
int main()
{
    double n , k ;
	cin >> n >> k;
	double arr[(int)n] , x=0;
	for(int i = 0 ; i  < n ; i++)
    {
        cin >> arr[i];
        if(arr[i]==1)
            x=i;
    }
    cout <<  ceil((n-1)/(k-1)) ;
}


Measured memory_bytes: 4504
//gauravsinghh
#include<bits/stdc++.h>
using namespace std;
#define pb push_back
#define eb emplace_back
#define ll long long int
#define sz(s) (int)s.size()
#define rep(i,n) for(ll i = 0; i < n; i++)
#define fo(i, a, b) for(int i = a; i <= b; i++)
#define IOS ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);

void io_set() {
    #ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    #endif
}

signed main(){ 
    IOS
    //io_set();
    ll n, k, m = INT_MAX;
    cin >> n >> k;
    ll a[n], sum = 0, z = 0, p = 0;
    rep(i, n){
        a[i] = k + i;
        sum += a[i];
        if(a[i] == 0){
            z = 1;
        }
        if(abs(a[i]) < abs(m)){
            m = a[i];
            if(m < 0){
                p = -1;
            }
            else{
                p = 1;
            }
        }
    }
    if(z){
        cout << sum << endl;
    }
    else{
        cout << sum - m << endl;
    }
    return 0;
}



Measured memory_bytes: 256
import math
N,K = map(int,input().split())
start = K**int(math.log(N,K))
X = []
while start !=0:
    if N - start >= 0:
        N = N-start
        X.append(1)
    else:
        X.append(0)
    start = int(start / K)
print(len(X))

Measured memory_bytes: 3060
import heapq

INF = 1e9

n = int(input())
adj_list = [[]] * n

for u in range(n):
    U = list(map(int, input().split()[2:]))
    adj_list[u] = zip(U[0::2], U[1::2]) # adj_list[u] = (v, cost)


def dijkstra(x):
    d = [INF] * x
    d[0] = 0
    isVisited = [False] * x
    hq = [(0, 0)] # (distance, v)??????????????¬??????????????????(key, ????´?)???????????????push, pop?????????
    
    while hq:
        u = heapq.heappop(hq)[1]
        isVisited[u] = True
        
        for v, c in adj_list[u]:
            if not isVisited[v] and d[v] > d[u] + c:
                d[v] = d[u] + c
                heapq.heappush(hq, (d[u] + c, v))
                
    return d

for i, di in enumerate(dijkstra(n)):
    print(i,di)

Measured memory_bytes: 42232
#include<stdio.h>
int main()
{
	int a,b,c,d;
	scanf("%d%d%d%d",&a,&b,&c,&d);
	if((a+b)>(c+d))
	{
		printf("Left\n");
	}
	else if((a+b)<(c+d))
	{
		printf("Right\n");
	}
	else if((a+b)==(c+d))
	{
		printf("Balanced\n");
	}
	return 0;
}

Measured memory_bytes: 128
N, M = map(int, input().split())
if N == 1 and M == 1:
    print(1)
elif N == 1 and M != 1:
    print(M - 2)
elif M == 1 and N != 1:
    print(N - 2)
else:
    print((N - 2) * (M - 2))


Measured memory_bytes: 2940
#include <iostream>
using namespace std;
long long num(char n[])
{
	long long res = 0;
	for (int i = 0; n[i]; i++)
	{
		if ('0' <= n[i] && n[i] <= '9')
		{
			res = res * 10 + (n[i] - '0');
		}
	}
	return res;
}
int main()
{
	long long a;
	char b[10];
	cin >> a >> b;
	long long B = num(b);
	long long ans = a * B / 100;
	cout << ans << endl;
	return 0;
}

Measured memory_bytes: 3636
#include <bits/stdc++.h>
#define N 1000005
#define ll long long int 	
#define MP make_pair
#define pb push_back
#define ppb pop_back
#define sp " "
#define endl "\n"
#define fi first
#define se second
#define ii pair<int,int>
#define lli pair<ll,ll>
#define fast cin.tie(0);cout.tie(0);ios_base::sync_with_stdio(false)
#define fast2 freopen ("badhair.gir","r",stdin);freopen ("badhair.cik","w",stdout);
#define mod 1000000007
#define fs(x,y) for(ll i=1;i<=y;i++) cin>>x[i]
#define fo(i,x,y) for(ll i=x;i<=y;i++)
#define INF 1000000000005
using namespace std;

ll n,m,ar[N],sum,t,ilu[N],ilu2[N];

stack<int> st;

void pre()
{
	for(int i=n;i>=1;i--)
	{
		while(st.size() && ar[st.top()]>ar[i])
			st.pop();
		if(!st.size())
			ilu[i]=n+1;
		else
			ilu[i]=st.top();
		st.push(i);
	}
	while(st.size())
		st.pop();
	for(int i=1;i<=n;i++)
	{
		while(st.size() && ar[st.top()]>=ar[i])
			st.pop();
		if(!st.size())
			ilu2[i]=0;
		else
			ilu2[i]=st.top();
		st.push(i);
	}
}

int main()
{
	fast;
	cin>>n;
	fs(ar,n);
	pre();
	// fo(i,1,n)
	// cout<<ilu[i]<<sp<<ilu2[i]<<endl;
	fo(i,1,n)
		sum+=(ilu[i]-i)*(i-ilu2[i])*ar[i];
	cout<<sum;

}



/*     cd onedrive\desktop\kod
cls

Sinav:21-22 aralik
Aciklama: Muhtemelen 25 aralik
*/

Measured memory_bytes: 10880
#include <bits/stdc++.h>
using namespace std;
using lint = long long;
template<class T = int> using V = vector<T>;
template<class T = int> using VV = V< V<T> >;

int main() {
  cin.tie(nullptr); ios::sync_with_stdio(false);
  int h, w; cin >> h >> w;
  V<string> s(h); for (auto&& e : s) cin >> e;
  int res = 0;
  for (int i = 0; i < h; ++i) for (int j = 0; j < w; ++j) if (s[i][j] == 'B') {
    for (int x = 0; x < h; ++x) for (int y = 0; y < w; ++y) if (s[x][y] == 'B') {
      res = max(res, abs(i - x) + abs(j - y));
    }
  }
  cout << res << '\n';
}


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

int main(){
    using namespace std;
    unsigned long N;
    cin >> N;
    vector<long> A(N);
    copy(istream_iterator<long>(cin), istream_iterator<long>(), A.begin());
    sort(A.rbegin(), A.rend());
    vector<long> sgn(N, 1);
    auto t = min<unsigned long>(N - 1, max(1L, A.rend() - lower_bound(A.rbegin(), A.rend(), 0)));
    fill(sgn.rbegin(), sgn.rend() - t, -1);
    cout << inner_product(A.begin(), A.end(), sgn.begin(), 0L) << endl;
    auto f = [](auto a, auto b){cout << a << " " << b << endl;return a - b;};
    A[t] = accumulate(A.begin() + 1, A.begin() + t, A[t], f);
    accumulate(A.begin() + t, A.end(), A[0], f);
    return 0;
}

Measured memory_bytes: 3456
#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, temp;
  vector<int> v;
  vector<int> c;

  cin >> n;

  for (int i=0;i<n;i++) {
    cin >> temp;
    v.push_back(temp);
  }

  for (int i=0;i<n;i++) {
    cin >> temp;
    c.push_back(temp);
  }

  long long result = 0;

  for (int i=0;i<n;i++) {
    if (v[i] > c[i]) {
      result += v[i] - c[i];
    }
  }

  cout << result;
}

Measured memory_bytes: 256