akhauriyash/Code-Regression huggingface dataset:
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define ll long long
#define P pair<int,int>
#define fi first
#define se second
#define rep(i,n) for(int i=0;i<n;i++)
#define all(v) v.begin(),v.end()
#define pb push_back
template<class T>void chmax(T &a,T b){if(a<b)a=b;}
template<class T>void chmin(T &a,T b){if(a>b)a=b;}
constexpr int INF=1000000000000000000;
constexpr int mod=1000000007;
int dx[]={0,1,0,-1},dy[]={1,0,-1,0};
int kaijo[2000010];
int gcd(int a,int b){
    if(b==0)return a;
    return gcd(b,a%b);
}
int lcm(int a,int b){
    return a/gcd(a,b)*b;
}
bool prime(int a){
    if(a==1)return false;
    for(int i=2;i*i<=a;i++){
        if(a%i==0)return false;
    }
    return true;
}
void init_fact(){
    kaijo[0]=1;
    for(int i=1;i<=2000000;i++){
        kaijo[i]=kaijo[i-1]*i;
        kaijo[i]%=mod;
    }
}
int modpow(int a,int b){
    if(b==0)return 1;
    if(b%2)return modpow(a,b-1)*a%mod;
    int memo=modpow(a,b/2);
    return memo*memo%mod;
}
int comb(int a,int b){
    if(!kaijo[0])init_fact();
    return kaijo[a]*modpow(kaijo[a-b],mod-2)%mod*modpow(kaijo[b],mod-2)%mod;
}
int inv(int x){
    x=modpow(x,mod-2);
    return x;
}
bool kosa(double ax,double ay,double bx,double by,double cx,double cy,double dx,double dy){
    double ta=(cx-dx)*(ay-cy)+(cy-dy)*(cx-ax);
    double tb=(cx-dx)*(by-cy)+(cy-dy)*(cx-bx);
    double tc=(ax-bx)*(cy-ay)+(ay-by)*(ax-cx);
    double td=(ax-bx)*(dy-ay)+(ay-by)*(ax-dx);
    return tc*td<0&&ta*tb<0;
}
int h,w;
signed main(){
    cin>>h>>w;
    int s=h*w;
    if(h==1||w==1)cout<<1<<endl;
    else if(s%2==0)cout<<s/2<<endl;
    else cout<<s/2+1<<endl;
    return 0;
}

Measured memory_bytes: 256
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <cstdlib>

using namespace std;

int main(){
	ios_base::sync_with_stdio(false);
	int n, z, w;
	cin >> n >> z >> w;
	vector<int> a(n);
	for(int i = 0; i < n; ++i){ cin >> a[i]; }
	if(n == 1){
		cout << abs(a.back() - w) << endl;
		return 0;
	}
	vector<int> x(n + 1, 2000000000), y(n + 1, 0);
	for(int i = n - 1; i >= 0; --i){
		const int xp = (i == 0 ? z : a[i - 1]);
		const int yp = (i == 0 ? w : a[i - 1]);
		x[i] = abs(yp - a.back());
		y[i] = abs(xp - a.back());
		for(int j = i + 1; j <= n; ++j){
			x[i] = max(x[i], y[j]);
			y[i] = min(y[i], x[j]);
		}
	}
	cout << x.front() << endl;
	return 0;
}


Measured memory_bytes: 256
import java.util.Scanner;

class Main {
    public static void main(String[] args) {
        Scanner s = new Scanner(System.in);
        String str = s.nextLine();
        String[] array = str.split(" ");
        int a = Integer.parseInt(array[0]), b = Integer.parseInt(array[1]);
        System.out.println(a * b + " " + (a * 2 + b * 2));
    }

}

Measured memory_bytes: 26160
#include <iostream>
#include <iomanip>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <algorithm>
#include <cstdio>
#include <utility>
#include <string>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <numeric>

using namespace std;

typedef uint64_t u64;
typedef int64_t s64;
typedef uint32_t u32;
typedef int32_t s32;
typedef vector<s32> vs32;
typedef vector<u32> vu32;
typedef vector<s64> vs64;
typedef vector<u64> vu64;

const double PI=3.14159265358979323846;

#define MAX(x, y) ((x) < (y) ? (y) : (x))
#define MIN(x, y) ((x) > (y) ? (y) : (x))

#define rep(i, N) for(int i = 0; i < N; ++i)

#define CEIL(x, y) (((x) + (y) - 1) / (y))
#define MOD 1000000007ULL

#define IN(l, r, x) ((l) <= (x) && (x) < (r))

vector< vs32 > g;
vs32 color;

const int WHITE = 0;
const int GRAY = 1;
const int BLACK = 2;

inline int enc(int i, int j)
{
    if (i < j) swap(i, j);
    return i * (i - 1) / 2 + j + 1;
}

int d[1000 * 1000];
int dfs(int u)
{
    if (color[u] == BLACK) return d[u];
    color[u] = GRAY;

    bool found = false;
    for (auto v : g[u])
    {
        if (color[v] == GRAY)
        {
            return -1;
        }
        int temp = dfs(v);
        if (temp == -1)
        {
            return -1;
        }
        found = true;
        d[u] = max(d[u], temp + 1);
    }
    color[u] = BLACK;
    return d[u];
}

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

    int n;
    cin >> n;

    int a;;
    color.resize(n * (n - 1) / 2 + 1, WHITE);
    g.resize(n * (n - 1) / 2 + 1);
    rep (i, n)
    {
        int u = 0;
        rep (j, n - 1)
        {
            cin >> a;
            --a;
            int v = enc(i, a);
            g[u].push_back(v);
            u = v;
        }
    }

    cout << dfs(0) << "\n";
    return 0;
}



Measured memory_bytes: 31616
#create date: 2020-03-22 21:03

import sys
stdin = sys.stdin

def ns(): return stdin.readline().rstrip()
def ni(): return int(ns())
def na(): return list(map(int, stdin.readline().split()))

def main():
    s = ns()
    n = len(s)
    s1 = s[:(n-1)//2]
    s2 = s[(n+3)//2-1:]
    ans = "Yes"
    if s == s[::-1] and s1 == s1[::-1] and s2 == s2[::-1]:
        print("Yes")
    else:
        print("No")

if __name__ == "__main__":
    main()

Measured memory_bytes: 3064
#include <algorithm>
#include <cctype>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cassert>
#include <functional>
#include <iostream>
#include <iomanip>
#include <iterator>
#include <map>
#include <queue>
#include <sstream>
#include <utility>
#include <vector>

using namespace std;

typedef long long Long;
#define whole(xs) xs.begin(), xs.end()

struct RangeMinimumQuery {
    int N;
    int* v;
    static const int INF = (1LL << 31) - 1;
    static int FindPow2(int x) {
        int x1 = 1;
        while (x1 < x) {
            x1 *= 2;
        }
        return x1;
    }
    RangeMinimumQuery(int N_, int init = 0) : N(FindPow2(N_)) {
        v = new int[N * 2];
        fill(v, v + N * 2, init);
    }
    ~RangeMinimumQuery() {
        delete[] v;
    }
    int dfs(int s, int t, int a, int b, int k) {
        if (s <= a && b <= t) return v[k];
        if (t <= a || b <= s) return INF;
        int m = (a + b) / 2;
        return min(dfs(s, t, a, m, k * 2), dfs(s, t, m, b, k * 2 + 1));
    }
    int query(int s, int t) {
        return dfs(s, t, 0, N, 1);
    }
    void update(int i, int x) {
        i += N;
        v[i] = x;
        while (i > 0) {
            i /= 2;
            v[i] = min(v[i * 2], v[i * 2 + 1]);
        }
    }
};

int main() {
    int n, q;
    cin >> n >> q;
    RangeMinimumQuery rmq(n, RangeMinimumQuery::INF);
    for (int i = 0; i < q; i++) {
        int c, x, y;
        cin >> c >> x >> y;
        if (c == 0) {
            rmq.update(x, y);
        } else {
            cout << rmq.query(x, y + 1) << endl;
        }
    }
    return 0;
}

Measured memory_bytes: 2060
#pragma gcc optimize("Ofast")
#include <bits/stdc++.h>
#define ff first
#define ss second
#define pf push_front
#define pb push_back
using namespace std;
using ll = long long; using ld = long double;
using pii = pair<int,int>; using pil = pair<int,ll>;
using pli = pair<ll,int>; using pll = pair<ll,ll>;
const int MOD = 1000000007, MOD2 = 998244353;
const int INF = 0x3f3f3f3f, INF2 = 0x3f3f3f37;
const ll LINF = 0x3f3f3f3f3f3f3f3f, LINF2 = 0x3f3f3f3f3f3f3f3d;

string s;

int main() {
    ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    cin >> s;
    for (int i = 0; i < s.size(); ++i) {
        if ((i + 1) & 1) {
            if (s[i] == 'L') return cout << "No\n", 0;
        }
        else if (s[i] == 'R') return cout << "No\n", 0;
    }
    cout << "Yes\n";
}

Measured memory_bytes: 256
import Data.ByteString.Char8 as BS

main :: IO ()
main = do
  _ <- BS.getLine
  s <- BS.getLine :: IO BS.ByteString
  print $ calc s 0 0

calc :: BS.ByteString -> Int -> Int -> Int
calc xs n m
  | xs == mempty = m 
  | BS.head xs == 'I' = calc t ninc (max ninc m)
  | otherwise         = calc t (n-1) m
    where t = BS.tail xs
          ninc = n+1



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

int main(){
    int n,count=0,l,r;
    cin >> n;
    vector<int> h(n);
    vector<int> e(n,0);
    for(int i=0; i<n; i++) cin >> h.at(i);

    //1 2 2 1
    
    while(h!=e){
        //find r
        for(int i=0; i<n; i++){
            if(e.at(i)!=h.at(i)){
                r=i;
                break;
            }
        }

        //find l
        l=n-1;
        for(int i=r+1; i<n; i++){
            if(e.at(i)==h.at(i)){
                l=i-1;
                break;
            } 
        }

        if(r==l){
            e.at(r)++;
        }else{
            for(int i=r; i<=l; i++){
                e.at(i)++;
            }
        }
        //cout << "r=" << r << " " << "l=" << l << endl;
        //for(int x : e) cout << x << ' ' << endl;
        //cout << endl;
        count++;
    }
    cout << count << endl;
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
#include <cstdlib>  // abs() for integer
#include <cmath>    // abs() for float, and fabs()
#include <algorithm>
using namespace std;

int main() {
  int a,b,x;
  cin >> a >> b >> x;
  if (a<=x && x<=a+b){
    cout << "YES" << endl;
  }
  else {
    cout << "NO" << endl;
  }
}


Measured memory_bytes: 256
n = int(input())
a = list(map(int, input().split()))

money = 1000
stock = 0
for i in range(n):
    # 売る
    money += stock * a[i]
    stock = 0

    # 買う
    if i != n - 1 and a[i] < a[i + 1]:
        stock = money // a[i]
        money %= a[i]

print(money)


Measured memory_bytes: 8876
#include <bits/stdc++.h>
using namespace std;
int main() {
    #ifdef DEBUG
    std::ifstream in("/home/share/inputf.in");
    std::cin.rdbuf(in.rdbuf());
    #endif
    int N;
    cin >> N;
    vector<int> L(N);
    int sum = 0;
    for(int i = 0; i < N; i++) {
        cin >> L[i];
        sum += L[i];
    }
    for(int i = 0; i < N; i++) {
        if(sum <= 2 * L[i]) {
            cout << "No" << endl;
            return 0;
        }
    }
    cout << "Yes" << endl;
    return 0;
}

Measured memory_bytes: 256
# -*- coding: utf-8 -*-

def gcd(a, b):
    while a % b:
        a, b = b, a % b
    return b

def maximum_d(cities, x):
    distances = set()
    for city in cities:
        distances.add(abs(x - city))
    if 0 in distances:
        distances.remove(0)

    result = None
    for distance in distances:
        if result:
            result = gcd(result, distance)
        else:
            result = distance
    return result

def main():
    n, x = [int(s) for s in input().split()]
    cities = [int(s) for s in input().split()]

    print(maximum_d(cities, x))

main()


Measured memory_bytes: 16812
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        try (Scanner sc = new Scanner(System.in);) {
            solve(sc);
        }
    }

    public static void solve(Scanner sc) {
        int a = sc.nextInt();
        int b = sc.nextInt();
        int c = sc.nextInt();

        int max = Math.max(a, b);
        max = Math.max(max, c);
        
        boolean ans = false;
        if (max == a) {
            ans = a == b + c;
        } else if (max == b) {
            ans = b == a + c;
        } else {
            ans = c == a + b;
        }

        System.out.println(ans ? "Yes" : "No");
    }
}

Measured memory_bytes: 22996
#include <stdio.h>
int main() {
  int N, M, a, b, c, i, j;

  a = b = c = 0;
  
  scanf("%d", &N);
  scanf("%d", &M);
  
  int K[M + 1];
  
  for(i = 1; i < M + 1; ++i) {
    K[i] = 0;
  }
  
  for(i = 0; i < N; ++i) {
    scanf("%d", &a);
    for(j = 0; j < a; ++j) {
      scanf("%d", &b);
      ++K[b];
    }
  }
  
  for(i = 1; i < M + 1; ++i) {
    if(K[i] == N) {
      c += 1;
    }
  }
  
  printf("%d\n", c);
  
  return 0;
}

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

long long int digitsum(long long int n) {
    long long int ret = 0;
    while(n) {
        ret += n%10;
        n /= 10;
    } 
    return ret;
}

int main() {
    long long int n;
    cin >> n;
    if(n%digitsum(n) == 0) {
        cout << "Yes" << '\n';
    } else {
        cout << "No" << '\n';
    }

    return 0;
}


Measured memory_bytes: 256
import java.util.Scanner;
class Main{
    public static void main(String[] args){
        Scanner scanner = new Scanner(System.in);

        int a, b, c;
        a = scanner.nextInt();
        b = scanner.nextInt();
        c = scanner.nextInt();

        int counter = 0;
        for(int i = a; i <= b; i++){
            if(c%i == 0)counter++;
        }
        System.out.println(counter);
    }
}



Measured memory_bytes: 26316
from statistics import median
#import collections
#aa = collections.Counter(a) # list to list || .most_common(2)で最大の2個とりだせるお a[0][0]
from fractions import gcd
from itertools import combinations # (string,3) 3回
from collections import deque
from collections import defaultdict
import bisect
#
#    d = m - k[i] - k[j]
#    if kk[bisect.bisect_right(kk,d) - 1] == d:
#
#
#
# pythonで無理なときは、pypyでやると正解するかも！！
#
#

import sys
sys.setrecursionlimit(10000000)
mod = 10**9 + 7

def readInts():
  return list(map(int,input().split()))
def main():
    q,h,s,d = readInts()
    n = int(input())
    # 2A < B なら 0.25 のみ
    # 2A > B なら　0.5 のみ。
    # "入力は整数であるから、小数点を考える必要がない"
    # よって、0.25 -> 1, 0.5 -> 1 として、かつ1と比較する
    one = min(q*4, h*2, s)
    # そして、1gだけで行けるか、
    # 2gだけで買えるだけ買って、余った分を1gでやる
    print(min(one*n, d*(n//2) + one * (n%2)))#n//2 は 2g ずつなので、 n%2 は1gするか

if __name__ == '__main__':
  main()


Measured memory_bytes: 5148
import sys
from collections import deque, Counter, defaultdict
from itertools import permutations
import heapq


def resolve():
    S = list(sys.stdin.readline().strip())
    T = list(sys.stdin.readline().strip())
    left = len(S) - len(T)
    right = len(S) - 1
    restorable = True
    while left >= 0:
        for cur in range(left, right + 1):
            if S[cur] != T[cur-left] and S[cur] != '?':
                break
        else:
            # found T
            for cur in range(left, right + 1):
                if S[cur] == '?':
                    S[cur] = T[cur-left]
            break
        left -= 1
        right -= 1
    else:
        # not found T
        restorable = False

    if restorable:
        for i in range(len(S)):
            if S[i] == '?':
                S[i] = 'a'
        print(''.join(S))
    else:
        print('UNRESTORABLE')


if __name__ == "__main__":
    resolve()


Measured memory_bytes: 3316
import sys
sys.setrecursionlimit(2147483647)
INF=float("inf")
MOD=10**9+7
input=lambda :sys.stdin.readline().rstrip()
from collections import Counter
def resolve():
    S=input()
    C=Counter(S)
    g=C['g']; p=C['p']
    n=g+p
    print(g-(n+1)//2)
resolve()

Measured memory_bytes: 39408
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define reprev(i, n) for (int i = (int)(n)-1; i >= 0; i--)
#define rep2(i, s, n) for (int i = (s); i < (int)(n); i++)

int main()
{
    int A, B, C;
    cin >> A >> B >> C;

    if (A == B && B == C)
    {
        cout << "No" << endl;
    }
    else if (A == B || B == C || C == A)
    {
        cout << "Yes" << endl;
    }
    else
    {
        cout << "No" << endl;
    }
}

Measured memory_bytes: 256
#include <iostream>
    #include <sstream>
    #include <cstdio>
    #include <cstdlib>
    #include <cmath>
    #include <ctime>
    #include <cstring>
    #include <string>
    #include <vector>
    #include <stack>
    #include <queue>
    #include <deque>
    #include <map>
    #include <set>
    #include <bitset>
    #include <numeric>
    #include <utility>
    #include <iomanip>
    #include <algorithm>
    #include <functional>
    using namespace std;
     
    #define COUT(x) cout << #x << " = " << (x) << " (L" << __LINE__ << ")" << endl
    #define EACH(i, s) for (__typeof__((s).begin()) i = (s).begin(); i != (s).end(); ++i)
     
    template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
    template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
    template<class T1, class T2> ostream& operator << (ostream &s, pair<T1,T2> P)
    { return s << '<' << P.first << ", " << P.second << '>'; }
    template<class T> ostream& operator << (ostream &s, vector<T> P)
    { for (int i = 0; i < P.size(); ++i) { if (i > 0) { s << " "; } s << P[i]; } return s; }
    template<class T> ostream& operator << (ostream &s, vector<vector<T> > P)
    { for (int i = 0; i < P.size(); ++i) { s << endl << P[i]; } return s << endl; }
    template<class T1, class T2> ostream& operator << (ostream &s, map<T1,T2> P)
    { EACH(it, P) { s << "<" << it->first << "->" << it->second << "> "; } return s << endl; }
     
     
    string str;
    int main() {
      cin >> str;
      cout << "ABC" << str <<endl;
    }

Measured memory_bytes: 256
#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
using namespace std;
signed main() {
  ios::sync_with_stdio(false); cin.tie(0);
  int n;
  cin >> n;
  string s;
  cin >> s;
  int r = 0;
  for (int i = 0; i < n; i++) {
    r += s[i] == 'R';
  }
  if (r > n / 2) cout << "Yes" << endl;
  else cout << "No" << endl;
  return 0;
}

Measured memory_bytes: 256
#include <string>
#include <iostream>
using namespace std;

int main() {
  string s;
  cin >> s;
  bool isHitachi = true;
  if (s.size() % 2 != 0)isHitachi = false;
  for (int i = 0; i < s.size(); i++) {
    if (i % 2 == 0 && s[i] != 'h')
      isHitachi = false;
    if (i % 2 == 1 && s[i] != 'i')
      isHitachi = false;
  }
  if (isHitachi)
    cout << "Yes" << endl;
  else
    cout << "No" << endl;
  return 0;
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
#define rep(a0,i,n) for(int i=a0; i<n; i++)
using ll = long long;

int main(){
    int h,a; cin>>h>>a;
    int count = 0;
    if(h%a!=0){
        count = h/a+1;
    }else{
        count = h/a;
    }

    cout << count << endl;
    
}


Measured memory_bytes: 256
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define BUF_SIZE 52

int main(void) {
  FILE *fp = stdin;
  char str1[BUF_SIZE];
  char str2[BUF_SIZE];

  if(!fgets(str1, BUF_SIZE, fp)) return 0;
  if(!fgets(str2, BUF_SIZE, fp)) return 0;

  char *tmp1, *tmp2;
  for(tmp1 = str1, tmp2 = str2; *tmp1 != '\n'; tmp1++, tmp2++) {
    putchar(*tmp1);
    putchar(*tmp2);
  }
  return 0;
}

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

#define st first
#define nd second
#define pb push_back
#define mp make_pair
typedef pair< int ,int > pii;
typedef vector< pii > vii;
typedef long long ll;
typedef map<int,int> mii;
typedef set<pii> sii;
typedef set<int> si;
typedef vector< int > vi;

const ll MOD = 119 << 23 | 1;

ll gcd(ll x, ll y){
    if(!y)
        return x;
    return gcd(y,x%y);
}

ll power(ll x, ll y)
{
    ll res = 1ll;     // Initialize result
    x%=MOD;
    if(x==0)
        return 0;
    while (y > 0)
    {
        if (y & 1)
            res = (res*x) % MOD;

        // y must be even now
        y = y>>1ll; // y = y/2
        x = (x*x) % MOD;
    }
    return res;
}

ll inv(ll x) {
    ll r = 1ll, t = x, k = MOD - 2ll;
    while (k) {
        if (k & 1ll) r = (long long) r * t % MOD;
        t = (long long) t * t % MOD;
        k >>= 1;
    }
    return r;
}

const int nax = 2e5+13;

void read(){
    int n,m,a,sum=0;
    vi t;
    cin >> n>>m;
    for(int i=0; i<n; i++){
        cin >>a;
        sum+=a;
        t.pb(a);
    }
    sort(t.begin(),t.end());
    for(int i=n-1; i>n-1-m; i--){
        if(4*m*t[i]<sum){
            cout << "No\n";
            return;
        }
    }
    cout <<"Yes\n";
}

void solve(){

}

int main() {

    ios_base::sync_with_stdio(false);
    cin.tie();
    cout.tie();
    int T=1;
    //cin >> T;
    for(int i=1; i<=T; i++){

        read();
        //cout << "Case #"<<i<<": ";
        solve();
    }

    return 0;
}


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

int main() {
  string s, t;
  cin >> s >> t;
  
  string u = t.erase((int)s.size());
  if (u == s)
    cout << "Yes" << endl;
  else
    cout << "No" << endl;
  
  return 0;
}

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

int main() {
  int n = 0;
  long long timetowait = 0;
  while(cin >> n && n != 0){
  vector<int> vec(n,0);
  
  for(int i = 0; i < n; i++)	cin >> vec.at(i);
  
  sort(vec.begin(), vec.end());
  
  for(int i = 0; i < n; i++){
    timetowait += vec.at(i) * (n - 1 - i);
  }
  cout << timetowait << endl;
    timetowait = 0;
  }
}


Measured memory_bytes: 3192
k, n = list(map(int, input().split()))
a = list(map(int, input().split()))
i = 1
max_diff = 0
while i < n:
    if a[i] - a[i - 1] > max_diff:
        max_diff = a[i] - a[i - 1]
    i += 1
if k - a[n - 1] + a[0]> max_diff:
    max_diff = k - a[n - 1] + a[0]
print(k - max_diff)


Measured memory_bytes: 26420
#include<bits/stdc++.h>
using namespace std;
#define ll long long
map<pair<string,ll>,ll> mp,visit1;
ll cost[52],flag,n;
string ar[52];
pair<string,ll> compSuff(string s1,string s2)
{
	ll i,j,f=1,k;
	i=0;
	j=s2.size()-1;
	while(i<s1.size() && j>=0)
	{
		if(s1[i]!=s2[j])
		{
			f=0;
			break;
		}
		i++;
		j--;
	}
	string str="";
	if(i==s1.size())
	{
		for(k=j;k>=0;k--)
		{
			str+=s2[k];
		}
	}
	else
	{
		for(k=i;k<s1.size();k++)
		{
			str+=s1[k];
		}
	}
	pair<string,ll> p={str,f};
	return p;
}
pair<string,ll> compPref(string s1,string s2)
{
	ll i,j,k,f=1;
	i=0;
	j=0;
	while(i<s1.size() && j<s2.size())
	{
		if(s1[i]!=s2[j])
		{
			f=0;
			break;
		}
		i++;
		j++;
	}
	string str="";
	if(i==s1.size())
	{
		for(k=j;k<s2.size();k++)
		{
			str+=s2[k];
		}
	}
	else
	{
		for(k=i;k<s1.size();k++)
		{
			str+=s1[k];
		}
	}
	pair<string,ll> p={str,f};
	return p;
}
ll checkPal(string s)
{
	string str="";
	ll j;
	for(j=s.size()-1;j>=0;j--)
	{
		str+=s[j];
	}
	if(str==s)
		return 1;
	else
		return 0;
}
ll rec(string str,ll side)
{
	pair<string,ll> p= {str,side};
	ll z=checkPal(str);
	if(z)
	{
		flag=1;
		return 0;
	}
	if(mp[p]!=0)
		return mp[p];
	if(visit1[p]==1)
		return 1e17;
	visit1[p]=1;
	ll min1=1e17;
	for(ll i=1;i<=n;i++)
	{
		pair<string,ll> z;
		if(side==0)
			z=compSuff(str,ar[i]);
		else
			z=compPref(str,ar[i]);
		if(z.second==1)
		{
			if(str.size()>=ar[i].size())
				min1=min(min1,cost[i]+rec(z.first,side));
			else
				min1=min(min1,cost[i]+rec(z.first,!side));
		}
	}
	return mp[p]=min1;
}
int main()
{
	ll i,j;
	cin>>n;
	for(i=1;i<=n;i++)
	{
		cin>>ar[i]>>cost[i];
	}
	ll min1=1e17;
	for(i=1;i<=n;i++)
	{
		min1=min(min1,cost[i]+rec(ar[i],0));
	}
	if(flag==0)
		cout<<-1;
	else
		cout<<min1;

}

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

#define DUMP(x) cerr << #x << "=" << x << endl
#define DUMP2(x, y) cerr<<"("<<#x<<", "<<#y<<") = ("<<x<<", "<<y<<")"<< endl
#define BINARY(x) static_cast<bitset<16> >(x)

#define rep(i,n) for(int i=0;i<(int)(n);i++)
#define REP(i,m,n) for (int i=m;i<(int)(n);i++)

#define in_range(x, y, w, h) (0<=(int)(x) && (int)(x)<(int)(w) && 0<=(int)(y) && (int)(y)<(int)(h))
#define ALL(a) (a).begin(),(a).end()

typedef long long ll;
const int INF   = 1e9;
const ll  INFLL = 1e18;
typedef pair<int, int> PII;
int dx[4]={0, -1, 1, 0}, dy[4]={-1, 0, 0, 1};

struct Node { int id, x, y; };
bool cmp1(const Node &lhs, const Node &rhs) { return lhs.x < rhs.x; }
bool cmp2(const Node &lhs, const Node &rhs) { return lhs.y < rhs.y; }

int N;

struct Edge {
    int u, v, cost;
    Edge(int _u, int _v, int _c) : u(_u), v(_v), cost(_c) {}
    bool operator<(const Edge &rhs) const { return cost < rhs.cost; }
};

const int MAX_N = 100000;
class UnionFind {
private:
    int union_size[MAX_N];
    int rank[MAX_N];

public:
    int par[MAX_N];
    
    void init(int n)
    {
        for (int i=0; i<n; i++) {
            par[i]=i;
            rank[i]=0;
            union_size[i] = 1;
        }
    }
    
    int find(int x)
    {
        if (par[x] == x) {
            return x;
        } else {
            return par[x] = find(par[x]);
        }
    }

    void merge(int x, int y)
    {
        x = find(x);
        y = find(y);
        if (x == y) return;

        union_size[x] += union_size[y];
        union_size[y] = union_size[x];

        if (rank[x] < rank[y]) {
            par[x] = y;
        } else {
            par[y] = x;
            if (rank[x] == rank[y]) rank[x]++;
        }
    }
    
    bool same(int x, int y)
    {
        return find(x) == find(y);
    }
    
    int getUnionSize(int x)
    {
        return union_size[find(x)];
    }
};

vector<Edge> G;
ll kruskal()
{
    UnionFind UF;
    sort(G.begin(), G.end());
    UF.init(N);
    ll res=0;
    for (Edge e : G) {
        if (!UF.same(e.u, e.v)) {
            UF.merge(e.u, e.v);
            res += e.cost;
        }
    }
    return res;
}

int main()
{
    ios::sync_with_stdio(false);

    cin >> N;
    vector<Node> VS(N);
    rep(i, N) {
        int X, Y;
        cin >> X >> Y;
        VS[i] = {i, X, Y};
    }

    sort(VS.begin(), VS.end(), cmp1);
    rep(i, N-1) {
        int cost = min(abs(VS[i].x - VS[i+1].x), abs(VS[i].y - VS[i+1].y)); 
        G.push_back(Edge(VS[i].id, VS[i+1].id, cost));
    }

    sort(VS.begin(), VS.end(), cmp2);
    rep(i, N-1) {
        int cost = min(abs(VS[i].x - VS[i+1].x), abs(VS[i].y - VS[i+1].y)); 
        G.push_back(Edge(VS[i].id, VS[i+1].id, cost));
    }

    cout << kruskal() << endl;
}

Measured memory_bytes: 6132
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(k,i,n) for(ll i=k;i<n;++i)
int main(void){
    // Your code here!
    ll N,K;
    cin>>N>>K;
    string S;
    cin>>S;
    ll uc=0;
    string s[N];
    rep(0,i,N)s[i]=S.substr(i,1);
    rep(0,i,N){
        if(i==0 && s[i]=="L")++uc;
        else if(i==N-1 && s[i]=="R")++uc;
        else{
            if(s[i]=="L"){if(s[i-1]=="R")++uc;}
            if(s[i]=="R"){if(s[i+1]=="L")++uc;}
        }
    }
    while(uc!=1 && K!=0){
        if(uc!=2)uc-=2;
        else --uc;
        --K;
    }
    cout<<N-uc<<endl;
}


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

int main(){
	int w,h,x,y;
	cin>>w>>h>>x>>y;
	double cnt=(double)(w);
	double ans=(double)(h);
	cout<<cnt*ans/2<<" ";
	if((w%2==0&&w/2==x&&h%2==0&&h/2==y)||(h%2==0&&h/2==y&&h==w&&x==y)||(w%2==0&&w/2==x&&h==w&&x==y)){
		cout<<1<<endl;
	}
	else cout<<0<<endl;
	return 0;
}

Measured memory_bytes: 256
#include<iostream>
#include<string>
#include<cstdio>
#include<algorithm>
#include<cstdlib>
#include<cmath>
#include<vector>
#include<utility>
#include<queue>
#include<stack>
#include<set>
#include<map>
#include<unordered_set>
#include<unordered_map>
#include<bitset>
using namespace std;
#define INF 2147483647
#define LINF 9223372036854775807
#define MOD 1000000007
#define int long long
#define rep(i,n) for(int i=0;i<n;i++)
#define MODE 1
#ifdef MODE
#define DEB(X) cout<< #X <<": "<<X<<" ";
#define ARDEB(i,X) cout<< #X <<"["<<i<<"]: "<<X[i]<<" ";
#define END cout<<endl;
#else
#define DEB(X) {}
#define ARDEB(i,X) {}
#define END {}
#endif
//typedef long long int ll;
 

signed main(){
   
   int x,y;
   cin>>x>>y;
   
   if(x==0 || y==0){
       int a=0;
       if(x==0 && y<0)a=1;
       if(y==0 && x>0)a=1;
       cout<<abs(x-y)+a<<endl;
       return 0;
   }
   
   int ans=abs(abs(x)-abs(y));
   if(x<0){
       if(y<x){
           ans+=2;
       }
       else if(y>0){
           ans++;
       }
   }
   else{
       if(y<0){
           ans++;
       }
       else if(y<x){
           ans+=2;
       }
   }
   cout<<ans<<endl;
   
}


Measured memory_bytes: 256
# -*- coding: utf-8 -*-

## Library

import sys
from fractions import gcd
import math
from math import ceil,floor
import collections
from collections import Counter
import itertools
import copy

## input

# n=int(input())
# A,B,C,D=map(int, input().split())
# string = input()
# yoko = list(map(int, input().split()))
# tate = [int(input()) for _ in range(N)]
# N, M = map(int,input().split()) 
#    P = [list(map(int,input().split())) for i in range(M)]

N,X=map(int, input().split())
A= list(map(int, input().split()))

A.sort()

ans = 0
for i in range(N):
    if i == N-1:
        if A[i] == X:
            ans += 1
    elif A[i] <= X:
        ans += 1
        X = X - A[i]
    else:
        break

print(ans)

Measured memory_bytes: 77500
#include<stdio.h>

int main()
{
	int A;
	int B,C,D;
	int P;
	int costa,costb;
	
	scanf("%d",&A);
	scanf("%d",&B);
	scanf("%d",&C);
	scanf("%d",&D);
	scanf("%d",&P);
	
	costa=A*P;
	if(P>=C)costb=B+(P-C)*D;
	else costb=B;
	
	if(costa>costb)printf("%d\n",costb);
	else printf("%d\n",costa);
	return 0;
}

Measured memory_bytes: 596
#define _USE_MATH_DEFINES
#define _CRT_SECURE_NO_WARNINGS

#include <iostream>
#include <string>
#include <sstream>
#include <iomanip>
#include <cmath>
#include <cstring>
#include <cstdlib>
#include <algorithm>
#include <stack>
#include <queue>
#include <vector>
#include <list>
#include <set>
#include <map>
#include <bitset>
#include <utility>
#include <numeric>
#include <iterator>

using namespace std;

using ll = long long;
using ull = unsigned long long;
using ldouble = long double;

const ll inf = 1ll << 60;
const ll mod = (ll)1e9 + 7;

#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()

#define print(s) cout << s;
#define println(s) cout << s << endl;
#define printd(s) cout << fixed << setprecision(10) << s << endl;


int main()
{
	char a;
	cin >> a;

	println((char)(a + 1));
}

Measured memory_bytes: 3592
import java.util.Scanner;
public class Main {
	static int calcExpectation(int[][] memo, int dis, int money) {
		for(int r = 0; r < memo.length; r++) {
			int max = memo[0][1];
			int minusDis = memo[0][0], idx = 0;
			
			for(int c = 1; c < memo.length; c++) {
				if(memo[c][1] > max) {
					max = memo[c][1];
					minusDis = memo[c][0];
					idx = c;
				}
			}
			
			if(money >= minusDis) {
				money -= minusDis;
				memo[idx][0] = 0;
				memo[idx][1] = 0;
			} else {
				memo[idx][0] -= money;
				money = 0;
			}
			if(money == 0) break;
		}
		
		int ret = 0;
		for(int r = 0; r < memo.length; r++) {
			int num = memo[r][0] * memo[r][1];
			if(num > 0)
				ret += num;
		}
		return(ret);
	}
	
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner stdIn = new Scanner(System.in);
		
		while(true) {
			int n = stdIn.nextInt();
			int m = stdIn.nextInt();
			if(n + m == 0) break;
			int[][] memo = new int[n][2];
			int sumDistance = 0;
			for(int r = 0; r < n; r++) {
				memo[r][0] = stdIn.nextInt(); //距離
				memo[r][1] = stdIn.nextInt(); //期待値
				sumDistance += memo[r][0];
			}
			System.out.println(calcExpectation(memo, sumDistance, m));
		}
	}
}

Measured memory_bytes: 63316
#include <iostream>
#include <algorithm>
using namespace std;
 
int main(){
	int N;
	cin >> N;
	int L[N * 2];
	for(int i = 0; i < N * 2; i++){
		cin >> L[i];
	}
	sort(L, L + N * 2);
	int ans = 0;
	for(int i = 0; i < N; i++){
		ans += L[i * 2];
	}
	cout << ans << endl;
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i=0;i<(n);i++)
#define rep2(i,n) for(int i=1;i<(n);i++)
typedef long long ll;

int main(){
    ll L,R; cin >> L >> R;
    int l=L%2019, r=R%2019;
    if(R-L>=2019 || l>=r){
        cout << 0 << endl;
        return 0;
    }
    int ans=2018;
    for(int i=l;i<r;i++){
        for(int j=i+1;j<=r;j++){
            ans=min(ans,(i*j)%2019);
            // if(ans==2){
            //     cout << ans << endl;
            //     return 0;
            // }
        }
    }
    cout << ans << endl;
}

Measured memory_bytes: 256
//http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=2664
#include<vector>
#include<string>
#include<iostream>
#include<algorithm>
#include<functional>
using namespace std;
#define MAX_N 5001
int par[MAX_N], r[MAX_N];//rank


void init(int N)
{
	int i;
	for (i = 0; i < N; i++)
	{
		r[i] = 0;
		par[i] = i;
	}
}

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

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

}

void unite(int x, int y)
{
	x = root(x);
	y = root(y);
	if (x == y)
	{
		return;
	}
	if (r[x] < r[y])
	{
		par[x] = y;
	}
	else if (r[x]>r[y])
	{
		par[y] = x;
	}
	else if (r[x] == r[y])
	{
		par[x] = y;
		r[y]++;
	}

}
struct _cost {
	string material;
	int material_number;
	int material_cost;
	bool operator<(const _cost& other)const {
		return material_cost < other.material_cost;
	}
};



int main()
{
	int n;//材料の数 ユニオンファインド木の頂点の数
	cin >> n;
	init(n);
	vector<_cost>v(n);//材料とコストの対応を管理
	for (int i = 0; i < n; i++)
	{
		_cost c;
		string str;
		cin >> str;
		c.material = str;
		c.material_number = i;
		int cost;
		cin >> cost;
		c.material_cost = cost;
		v[i] = c;

	}
	sort(v.begin(), v.end());//材料をcostが安い順に並べ替えた
	int m;
	cin >> m;//魔法の種類 辺の数
	vector<vector<int>>edge(m,vector<int>(2));
	for (int i = 0; i < m; i++)
	{
		string str1;
		cin >> str1;
		
		
			for (int j = 0; j < n; j++)
			{
				if (str1 == v[j].material)
				{
					edge[i][0] = v[j].material_number;
		  		}
			}
		
		
		string str2;
		cin >> str2;

		for (int j = 0; j < n; j++)
		{
			if (str2 == v[j].material)
			{
				edge[i][1] = v[j].material_number;
			}
		}

		
	}
	

	for (int i = 0; i < m; i++)
	{
		unite(edge[i][0], edge[i][1]);
	}

	vector<int>check(n,0);//各辺の実質的なコストを管理する
	vector<bool>ok(n, false);
	for (int i = 0; i < n; i++)
	{
		for (int j = 0; j < n; j++)
		{
			if (same(v[i].material_number, j)&&(ok[j]==false))
			{
				check[j] = v[i].material_cost;
				ok[j] = true;
			}
		}
	}
	int ans = 0;
	for (int i = 0; i < n; i++)
	{
		ans += check[i];
	}
	cout << ans << endl;

	return 0;
}


Measured memory_bytes: 3508
#include<bits/stdc++.h>
using namespace std;
typedef  long long ll;
#define pb push_back
#define eb emplace_back
const  int MAX=1000000;

int main()
{
   ll t;

   t=1;

   while(t--)
   {
     ll n,k;

     cin>>n>>k;


     ll ara[n+1];

     for(ll i=0;i<n;i++)
     {
         cin>>ara[i];
     }


     sort(ara,ara+n);

      ll sum=0;


      for(ll i=0;i<k;i++)sum+=ara[i];

      cout<<sum<<endl;




   }

    }











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

int main(){
    //input
    int n,m;
    map<string,int> s;
    map<string,int> t;
    cin >> n;
    for(int i=0;i<n;i++){
        string str;
        cin >> str;
        s[str]++;
    }
    cin >> m;
    for(int i=0;i<m;i++){
        string str;
        cin >> str;
        t[str]++;
    }

    int ma = 0;
    for (auto itr = s.begin(); itr != s.end(); ++itr) {
        int blue = itr->second;
        int red = t[itr->first];
        ma = max(ma,blue-red);
    }

    cout << ma << endl;

    return 0;
}

Measured memory_bytes: 256
// #define _GLIBCXX_DEBUG // for STL debug (optional)
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <string>
#include <cstring>
#include <deque>
#include <list>
#include <queue>
#include <stack>
#include <vector>
#include <utility>
#include <algorithm>
#include <map>
#include <set>
#include <complex>
#include <cmath>
#include <limits>
#include <cfloat>
#include <climits>
#include <ctime>
#include <cassert>
#include <numeric>
#include <fstream>
#include <functional>
#include <bitset>
using namespace std;
using ll = long long int;
using int64 = long long int;
 
template<typename T> void chmax(T &a, T b) {a = max(a, b);}
template<typename T> void chmin(T &a, T b) {a = min(a, b);}
template<typename T> void chadd(T &a, T b) {a = a + b;}
 
int dx[] = {0, 0, 1, -1};
int dy[] = {1, -1, 0, 0};
const int INF = 1LL << 29;
const ll LONGINF = 1LL << 60;
const ll MOD = 1000000007LL;

int A[110][110];
int main() {
    int N, M, D; cin >> N >> M >> D;

    for(int i=0; i<N; i++) {
        for(int j=0; j<M; j++) {
            char c; cin >> c;
            A[i+1][j+1] = (c == '#' ? 1 : 0);
            A[i+1][j+1] += A[i+1][j] + A[i][j+1] - A[i][j];
        }
    }

    int ans = 0;
    for(int i=0; i<N; i++) {
        for(int j=0; j<M; j++) {
            if(i+D<=N and j+1<=M) {
                int s = A[i+D][j+1] - A[i+D][j] - A[i][j+1] + A[i][j];
                if(!s) ans++;
            }
            if(i+1<=N and j+D<=M) {
                int s = A[i+1][j+D] - A[i+1][j] - A[i][j+D] + A[i][j];
                if(!s) ans++;
            }
        }
    }
    cout << ans << endl;
    return 0;
}



Measured memory_bytes: 3068
L, R = gets.split.map(&:to_i)

min = 10**10
L.upto(R-1) do | i |
  (i+1).upto(R) do | j |
    min = min < (i*j)%2019 ? min : (i*j)%2019
    if min == 0
      p 0
      exit
    end
  end
end
p min

Measured memory_bytes: 1788
#include<bits/stdc++.h>
#define N 3
using namespace std;
typedef unsigned long long ull;
bool im[1010][1010],pa[110][110];
int w,h,p,ans;
vector<ull> used;

void ppp(){
 ull a=0;
 ull roll[1001][1001]={};
 for(int i=0;i<p;i++)
   for(int j=0;j<p;j++)a=a*N+pa[i][j];
 for(int i=0;i<used.size();i++)
   if(used[i]==a)return;
 used.push_back(a);
 for(int j=0;j<h;j++){
   ull b=0,g=1;
   for(int i=0;i<p;i++)b=b*N+im[j][i],g*=N;
   roll[j][p-1]=b;
   for(int i=p;i<w;i++)
     roll[j][i]=b=b*N+im[j][i]-im[j][i-p]*g;
 }
 for(int j=p-1;j<w;j++){
   ull b=0,g=1,n=1;
   for(int i=0;i<p;i++)n*=N;
   for(int i=0;i<p;i++)b=b*n+roll[i][j],g*=n;
   if(b==a)ans++;
   for(int i=p;i<h;i++){
     b=b*n+roll[i][j]-roll[i-p][j]*g; 
     if(b==a)ans++;
   }
 }
}

void ch(){
 bool npa[110][110];
 for(int i=0;i<p;i++)
   for(int j=0;j<p;j++)npa[i][j]=pa[j][p-i-1];
 for(int i=0;i<p;i++)
   for(int j=0;j<p;j++)pa[i][j]=npa[i][j];
}

void ch1(){
 for(int i=0;i<p;i++)
   for(int j=0;j<p/2;j++)swap(pa[i][j],pa[i][p-j-1]);
}

int main(){
 int D[256];
 for(int i='A';i<='Z';i++)D[i]=i-'A';
 for(int i='a';i<='z';i++)D[i]=i-'a'+26;
 for(int i='0';i<='9';i++)D[i]=i-'0'+52;
 D['+']=62,D['/']=63;
 while(cin>>w>>h>>p,w){
   string s;
   used.clear();
   for(int i=0;i<h;i++){
     cin>>s;
     for(int j=0;j<s.size();j++){
       int t=D[s[j]],a=6;
	while(a--)im[i][j*6+a]=t%2,t/=2;
     }
   }
   for(int i=0;i<p;i++){
     cin>>s;
     for(int j=0;j<s.size();j++){
       int t=D[s[j]],a=6;
	while(a--)pa[i][j*6+a]=t%2,t/=2;
     }
   }
   ans=0;
    for(int i=0;i<4;i++){
     ppp();
     ch1();
     ppp();
     ch1();
     ch();
   }
   cout<<ans<<endl;
 }
 return 0;
}

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

#define EPS (1e-10)
#define rep(i, a, b) for (int i = a; i < (int)(b); ++i)
#define rrep(i, a, b) for (int i = b - 1; i >= (int)(a); --i)
#define all(a) a.begin(), a.end()

using namespace std;
using ll = long long;
using vi = vector<int>;
using vb = vector<bool>;
using vll = vector<ll>;
using vd = vector<double>;
using vvi = vector<vi>;
using vvb = vector<vb>;

constexpr int MOD = 1000000007;

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

    int a, b, c;
    cin >> a >> b >> c;
    cout << max(0, c - (a - b)) << endl;

    return 0;
}

Measured memory_bytes: 256
// finish date: 2018/01/21
#include <iostream>
#include <cmath>
#include <vector>
#include <bitset>
#include <algorithm>
#include <stack>
#include <map>
#include <climits>

using namespace std;

#define FOR(i, a, b) for(int (i)=a;(i)<(b);(i)++)
#define rep(i, n)  FOR(i,0,n)
#define ll long long
#define bigmod 1000000007
#define INF 500000000


int main() {
    int N, K;
    cin >> N >> K;
    int num = 1;
    rep(i, N) num = min(num + K, num * 2);
    cout << num << endl;

    return 0;
}


Measured memory_bytes: 256
count = {}
("a".."z").each{|c|
	count[c] = 51
}
n = gets.to_i
n.times{|i|
	s = gets.chomp
	("a".."z").each{|c|
		count[c] = [s.count(c), count[c]].min
	}
}
("a".."z").each{|c|
	count[c].times{|i|
		print c
	}
}
puts
	

Measured memory_bytes: 1916
import java.util.*;
public class Main{

    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        int x = sc.nextInt();
        int cnt = 1; 
        int num =0;
        int max=0;
        
        for(int i =1;i<40;i++){
            for(int j =2;j<40;j++){

                num = (int)Math.pow(i,j);
                if(max<num && num<=x){
                    max=num;
                }
            }
        }
        System.out.println(max);

    }
}






Measured memory_bytes: 21076
### ----------------
### ここから
### ----------------

import sys
from io import StringIO
import unittest

def yn(b):
    print("Yes" if b==1 else "No")
    return

def resolve():
    readline=sys.stdin.readline

    x,k,d=map(int, readline().rstrip().split())
    if x < 0:
        x*= -1
    
    if x==0:
        if k%2==0:
            print(0)
        else:
            print(d)
        return
    
    aa = x // d

    if k <= aa:
        print(x-k*d)
        return
    x-=aa*d
    k-=aa
    if k%2==0:
        print(x)
    else:
        print(abs(x-d))


    #arr=list(map(int, readline().rstrip().split()))
    #n=int(readline())
    #ss=readline().rstrip()
    #yn(1)

    return

if 'doTest' not in globals():
    resolve()
    sys.exit()

### ----------------
### ここまで 
### ----------------

Measured memory_bytes: 16224
//#include<bits/stdc++.h>
#include<iostream>
#include<cmath>
#include<cstdio>
#include<vector>
#include<set>
#include<map>
#include<stack>
#include<queue>
#include<utility>
#include<algorithm>
#include<cstring>
#include<sstream>
#include <iomanip>

using namespace std;

typedef long long ll ;
typedef double db;
typedef vector<int> vi;
typedef pair<int,int> pii;
typedef vector< pair<int,int> > vii;
const double pi = 2*acos(0) ;

#define pf		  	printf
#define sf			  scanf
#define pb(a)         push_back(a)
#define mp			make_pair
#define fi			 first
#define se			 second
#define for0(i,n)        for(int i=0;i<n;i++)
#define for1(i,n)        for(int i=1;i<=n;i++)
#define forab(i,a,b)       for(int i=a;i<=b;i++)
#define lcm(a, b)     ((a)*((b)/gcd(a,b)))
#define sq(a)		   (a)*(a)
#define nw				"\n"
#define abs(x)		fabs(x)
#define pcase(z,x)    cout<<"Case "<<z<<": "<<x<<"\n"
int main()
{
	ll x,y,c=1;
	cin>>x>>y;
	while(x<=y/2){
		x *= 2;
		c++;
		}
	cout<<c<<nw;
	
	
	return 0;
}

Measured memory_bytes: 256
#include<bits/stdc++.h>
using namespace std;
#define ll long long
string s[1000000];
ll t[1000000];
int main()
{
    ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
    int n;
    cin>>n;
    for (int i=1;i<=n;i++){
        cin>>s[i]>>t[i];
    }
    string targ;
    cin>>targ;
    ll ret=0;
    for (int i=n;i>=1;i--){
        if (s[i]==targ)break;
        ret+=t[i];
    }
    cout<<ret<<endl;
}

Measured memory_bytes: 9728
#include <stdio.h>
#include <math.h>

int main(void){
    char s[100001];
    scanf("%s", &s);
    int count = 0;
    int i = 1;
    while(s[i] != '\0'){
        int j = i-1;
        while(s[j] == 'a' && j > 0)j--;
        if(s[i] != s[j] && s[j] != 'a'){
            count += 2;
            s[i] = 'a';
            s[j] = 'a';
        }
        i++;
    }
    printf("%d\n", count);
    return 0;
}

Measured memory_bytes: 256
import fractions

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

def gcd(a,b):
    return fractions.gcd(a,b)

x = [0]
y = [0]

for i in range(n):
    x.append(gcd(x[i], a[i]))
    y.append(gcd(y[i], a[-(i+1)]))

y.reverse()

ans = []
for i in range(n):
    ans.append(gcd(x[i], y[i+1]))

print(max(ans))





Measured memory_bytes: 16140
#include <cstdio>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
using namespace std;
const int N=805;
int v[N][N],a[N][N];
char tmp[N];
int d[4][2]={1,0,-1,0,0,1,0,-1};
struct A{int x,y;}t;
queue<A> q;
int n,m,k;
int updiv(int x,int y){return (x-1)/y+1;}
int get(int x,int y){
    int t=234567;
    if(x==1||x==n||y==1||y==m) return 0;
    t=min(t,updiv(x-1,k));
    t=min(t,updiv(n-x,k));
    t=min(t,updiv(m-y,k));
    t=min(t,updiv(y-1,k));
    return t;
}
int main(){
    int i,j,sx,sy,dx,dy,ans=234567;
    scanf("%d %d %d",&n,&m,&k);
    for(i=1;i<=n;i++){
        scanf("%s",tmp+1);
        for(j=1;j<=m;j++){
            if(tmp[j]=='#') a[i][j]=1;
            else a[i][j]=0;
            if(tmp[j]=='S') sx=i,sy=j;
        }
    }
    memset(v,-1,sizeof(v));
    q.push((A){sx,sy});v[sx][sy]=0;
    while(!q.empty()){
        t=q.front();q.pop();
        for(i=0;i<4;i++){
            dx=t.x+d[i][0];
            dy=t.y+d[i][1];
            if(1<=dx&&dx<=n&&1<=dy&&dy<=m&&a[dx][dy]==0&&v[dx][dy]==-1){
                q.push((A){dx,dy});
                v[dx][dy]=v[t.x][t.y]+1;
            }
        }
    }
    for(i=1;i<=n;i++)
        for(j=1;j<=m;j++)
            if(v[i][j]>=0&&v[i][j]<=k)
                ans=min(ans,get(i,j)+1);
    printf("%d\n",ans);
    return 0;
}


Measured memory_bytes: 5376
import sys
sys.setrecursionlimit(10**7)
input = sys.stdin.buffer.readline
n,m,q=map(int,input().split())
Q=[list(map(int,input().split())) for _ in range(q)]
ans=0
def dfs(i,tab,ma):
	global ans
	if i==n:
		t=0
		for a,b,c,d in Q:
			if tab[b-1]-tab[a-1]==c:
				t+=d
		if t>ans:
			ans=t
		return
	for j in range(ma,m+1):
		dfs(i+1,tab+[j],j)
dfs(0,[],1)
print(ans)

Measured memory_bytes: 9284
O = input()
E = input()
password = ""
if len(O) == len(E):
    for _ in range(len(O)):
        password += O[_] + E[_]
else:
    for _ in range(len(O) - 1):
        password += O[_] + E[_]
    password += O[-1]
print(password)

Measured memory_bytes: 3060
#include <iostream>
#include <climits>
#include <cmath>
#include <algorithm>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <string>
#include <deque>
#define INF_INT (INT_MAX / 2)
#define INF_LONG (LONG_MAX / 2)
//#define DEBUG true
#define DEBUG false
using namespace std;

const int MAX = 100001;
const int MOD = 1000000007;

typedef long long ll;
typedef pair< int, int > pii;
typedef pair< ll, ll > pll;
typedef pair< ll, int > pli;
typedef pair< int, ll > pil;

int ceil(int x, int y){
  return (x % y == 0) ? x / y : x / y + 1;
}

int gcd(int x, int y){
  return y ? gcd(y, x % y) : x;
}

int lcm(int x, int y){
  return x / gcd(x, y) * y;
}

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

  int n;
  cin >> n;
  string s;
  cin >> s;

  int r = 0;
  for(char c: s){
    if(c == 'R') ++r;
  }

  if(r > (n - r)) cout << "Yes" << endl;
  else cout << "No" << endl;

  return 0;
}


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

#define rep(i,n) for(ll i = 0; i < n; i++)
#define per(i,n) for(ll i = n-1; i >= 0; i--)
#define REP(i,n) for(ll i = 1; i < n; i++)
#define PER(i,n) for(ll i = n; i >= 1; i--)
#define sz(x) int(x.size())
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
#define YesorNo(a) printf(a ? "Yes\n" : "No\n")
#define endl '\n'
#define fi first
#define se second
using ll = long long;
using P = pair<int,int>;
using Pl = pair<ll,ll>;
using vi = vector<int>;
using vii = vector<vi>;
using vl = vector<ll>;
using vll = vector<vl>;
using vs = vector<string>;
using vb = vector<bool>;
using vc = vector<char>;
using vcc = vector<vc>;
using vP = vector<P>;
using vPP = vector<vP>;
using vPl = vector<Pl>;
const int dx[] = {0,1,0,-1,1,1,-1,-1};
const int dy[] = {1,0,-1,0,1,-1,-1,1};
const int inf = (1<<30)-1;
const ll infll = (1LL<<62)-1;
ll ceil(const ll &a, const ll &b){return ((a)+(b)-1)/b;}
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }



int main(){
	string s;
	cin >> s;
	cout <<  s[0] << s[1] << s[2] << endl;
}

Measured memory_bytes: 3596
// ABC147_A.cpp : このファイルには 'main' 関数が含まれています。プログラム実行の開始と終了がそこで行われます。
//

#include <iostream>
using namespace std;
int main()
{
	int a1, a2, a3;
	cin >> a1 >> a2 >> a3;
	if (a1 + a2 + a3 > 21) cout << "bust" << endl;
	else cout << "win" << endl;
	return 0;
}

// プログラムの実行: Ctrl + F5 または [デバッグ] > [デバッグなしで開始] メニュー
// プログラムのデバッグ: F5 または [デバッグ] > [デバッグの開始] メニュー

// 作業を開始するためのヒント: 
//    1. ソリューション エクスプローラー ウィンドウを使用してファイルを追加/管理します 
//   2. チーム エクスプローラー ウィンドウを使用してソース管理に接続します
//   3. 出力ウィンドウを使用して、ビルド出力とその他のメッセージを表示します
//   4. エラー一覧ウィンドウを使用してエラーを表示します
//   5. [プロジェクト] > [新しい項目の追加] と移動して新しいコード ファイルを作成するか、[プロジェクト] > [既存の項目の追加] と移動して既存のコード ファイルをプロジェクトに追加します
//   6. 後ほどこのプロジェクトを再び開く場合、[ファイル] > [開く] > [プロジェクト] と移動して .sln ファイルを選択します


Measured memory_bytes: 256
#include <stdio.h>
#include <stdlib.h>
#include <math.h>


int main(void){
	int i,n;
	int x[1000], y[1000];
	int max =0;
	int d;
	long sum =0;

	scanf("%d", &n);
	for(i=0; i<n; i++)
		scanf("%d", &x[i]);
	for(i=0; i<n; i++)
		scanf("%d", &y[i]);

	for(i=0; i<n; i++){
		sum += abs(x[i]-y[i]);
	}
	printf("%ld\n", sum);
	sum=0;
	for(i=0; i<n; i++){
		sum += (x[i]-y[i])*(x[i]-y[i]);
	}
	printf("%f\n", sqrt(sum));
	sum=0;
	for(i=0; i<n; i++){
		d = abs((x[i]-y[i]));
		sum += d*d*d;
	}
	printf("%f\n", pow(sum, 1.0/3.0));
	for(i=0; i<n; i++){
		if(max < abs(x[i]-y[i]))
			max = abs(x[i]-y[i]);
	}
	printf("%d\n", max);
	return 0;
}

Measured memory_bytes: 680