akhauriyash/Code-Regression huggingface dataset:
#include<bits/stdc++.h>
#include<algorithm>
#include<math.h>
#include<string>
#include<iostream>
#include<vector>
#include<cstdlib>
using namespace std;
 
template <class T> using V = vector<T>;
 
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;
}
 
const long long INF = 1LL << 60;
 
using ll = long long;
using db = double;
using st = string;
using ch = char;
using bl = bool;
using vll = V<ll>;
using vpll =V<pair<ll,ll>>;
using vst = V<st>;
using vdb = V<db>;
using vch = V<ch>;
using vbl = V<bl>;

 
#define FOR(i,a,b) for(ll i=(a);i<(b);i++)
#define rFOR(i,a,b) for(ll i=(a);i>=(b);i--)
#define oFOR(i,a,b) for(ll i=(a);i<(b);i+=2)
#define bgn begin()
#define en end()
#define SORT(a) sort((a).bgn,(a).en)
#define REV(a) reverse((a).bgn,(a).en)
#define M(a,b) max(a,b)
#define rM(a,b) min(a,b)
#define fi first
#define se second
#define sz size()
#define gcd(a,b) __gcd(a,b)
#define co(a) cout<<a<<endl;
#define ci(a) cin>>a;
  
 

ll sum(ll n) {
ll m=0;
  while(n){
    m+=n%10;
    n/=10;
  }
  return m;
}
 
 
const int MAX = 510000;
const int MOD = 1000000007;
 
long long fac[MAX], finv[MAX], inv[MAX];
 
void Comuse() {
    fac[0] = fac[1] = 1;
    finv[0] = finv[1] = 1;
    inv[1] = 1;
    for (int i = 2; i < MAX; i++){
        fac[i] = fac[i - 1] * i % MOD;
        inv[i] = MOD - inv[MOD%i] * (MOD / i) % MOD;
        finv[i] = finv[i - 1] * inv[i] % MOD;
    }
}
 
 
#define comuse Comuse()
 
ll combi(int n, int k){
    if (n < k) return 0;
    if (n < 0 || k < 0) return 0;
    return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
}
 
 
 
 
ll lcm(ll a,ll b){
  ll n;
  n=a/gcd(a,b)*b;
  return n;
}




 
/****************************************\
| Thank you for viewing my code:)        |
| Written by RedSpica a.k.a. RanseMirage |
| Twitter:@asakaakasaka                  | 
\****************************************/
 
 
signed main() {
  ll h,w;
    cin>>h>>w;
  ll ans=10000000000;

  //縦に3つ
  for(int i=1;i<h-1;i++){
    ll a=i*w;
    ll b=(h-i)/2*w;
    ll c=(h-i-(h-i)/2)*w;
    ll now=max({a,b,c})-min({a,b,c});
    chmin(ans,now);
  }
  //横に3つ
  for(int i=1;i<w-1;i++){
    ll a=i*h;
    ll b=(w-i)/2*h;
    ll c=(w-i-(w-i)/2)*h;
    ll now=max({a,b,c})-min({a,b,c});
    chmin(ans,now);
  }

  //T字
  for(int i=1;i<h;i++){
    ll a=i*w;
    ll b=(w/2)*(h-i);
    ll c=(w-w/2)*(h-i); 
    ll now=max({a,b,c})-min({a,b,c});
    chmin(ans,now);
  }

  //ト
  for(int i=1;i<w;i++){
    ll a=i*h;
    ll b=(h/2)*(w-i);
    ll c=(h-h/2)*(w-i);
    ll now=max({a,b,c})-min({a,b,c});
    chmin(ans,now);
  }
  cout<<ans<<endl;
}

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

int p=0;
Int formula(string& s,int turn);
vector<char> op[3];

Int calc(Int lhs,Int rhs,char op){
    if(op=='+') return lhs+rhs;
    if(op=='-') return lhs-rhs;
    if(op=='*') return lhs*rhs;
    throw("what calc");
}

Int num(string& s){
    Int res=0;
    int op=1;
    if(s[p]=='-'){
        op=-1;
        p++;
    }
    else if(s[p]=='+'){
        op=1;
        p++;
    }
    while(isdigit(s[p])){
        res*=10;
        res+=s[p]-'0';
        p++;
    }
    return op*res;
}

Int term(string & s){
    if(s[p]=='('){
        p++;
        Int res=formula(s,0);
        p++;
        return res;
    }
    return num(s);
}

Int formula(string& s,int turn){
    if(turn==3) return term(s);
    Int res=formula(s,turn+1);
    while(p!=s.size() && s[p]!=')'){
        bool breaker=false;
        for(int i=0;i<turn;i++){
            for(int j=0;j<op[i].size();j++){
                if(op[i][j]==s[p]){
                    breaker=true;
                }
            }
        }
        if(breaker) break;
        char ops=s[p];
        p++;
        Int next=formula(s,turn+1);
        res=calc(res,next,ops);
    }
    return res;
}
int main(){
    string s;
    cin>>s;    
    Int res=-1e20;
    for(int i=0;i<3;i++){
        for(int j=0;j<3;j++){
            for(int k=0;k<3;k++){
                op[0].clear();
                op[1].clear();
                op[2].clear();
                op[i].push_back('+');
                op[j].push_back('-');
                op[k].push_back('*');
                p=0;
                Int ret=formula(s,0);
                res=max(res,ret);
            }
        }
    }
    cout<<(long long)(res)<<endl;
    return 0;
}


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

int N,X;
vector<int> v_list;

int main() {
  cin >> N >> X;
  v_list.resize(N+1);
  v_list[0]=0;
  for(int i=1;i<N+1;i++) cin >> v_list[i];

  int boll_pos = 0;
  int ans = 0;
  for(int i=0;i<N+1;i++) {
    boll_pos += v_list[i];
    if (X >= boll_pos) {
      ans++;
    } else {
      break;
    }
  }
  cout << ans << "\n";

#if 0
  cout << N << " " << X << "\n";
  for(auto s: v_list) cout << s << "\n"; 
#endif
  return 0;
}

Measured memory_bytes: 256
A, B, C = list(map(int, input().split()))

if A == B == C:
    if A % 2 == 0:
        print('-1')
    else:
        print('0')
else:
    count = 0
    for _ in range(10 ** 9):
        if A % 2 != 0 or B % 2 != 0 or C % 2 != 0:
            print(count)
            break
        tmp_A = A
        tmp_B = B
        tmp_C = C
        A = (tmp_B + tmp_C) / 2
        B = (tmp_A + tmp_C) / 2
        C = (tmp_B + tmp_A) / 2
        count += 1


Measured memory_bytes: 9248
#include <bits/stdc++.h>
#include<cmath>
#define N 100005
#define A 1000005
#define MOD 998244353 //1000000007 // //100000000
#define INF 1000000000000000000
#define ll long long
//#define M_PI 3.141592653589793238463

using namespace std;

#define pii pair<ll, ll>
#define piii pair<ll, pii>
#define ft first
#define sd second
#define pb push_back
#define rep(i, n) for(ll i = 0; i < n; i++)
#define repr(i, n) for(ll i = n-1; i >= 0; i--)
#define itr(it, x) for(auto it = x.begin(); it != x.end(); it++)
#define mem(a, b) memset(a, (ll)b, sizeof(a))
#define all(v) v.begin(), v.end()
#define allr(v) v.rbegin(), v.rend()
#define edge(v, x, y) v[x].pb(y); v[y].pb(x);
#define popcount __builtin_popcount
#define ANS(s) {cout << s << "\n"; return;}

//~~~~~~fancy print~~~~~~~//
#define printpii(a) cout << a.ft << " " << a.sd << endl;
#define printpiii(a) cout << a.ft << " " << a.sd.ft << " " << a.sd.sd << endl;
#define print(a, n) rep(i, n) cout << a[i] << " "; cout << endl;
#define printv(v) for(auto x: v)cout << x << " "; cout << endl;
#define printm(a, n, m) rep(i, n) { rep(j, m) cout << a[i][j] << "\t"; cout << endl;}

ll dx[4] = {0, 0, 1, -1};
ll dy[4] = {1, -1, 0, 0};

void fast(){ios_base::sync_with_stdio(false);cin.tie(0);cout << setprecision(12) << fixed;}


void solve(){
    ll n, t, a;
    cin >> n >> t >> a;
    ll h[n];
    double mn = 10000000000.0; ll p = 0;
    rep(i, n) {
        cin >> h[i];
        double kk =  t - (double)h[i] * 0.006; //cout << kk <<  " ";
        kk -= a;
        kk = abs(kk);
        if(kk < mn)
        {
            mn = kk; p = i;
        }
    }
    cout << p+1;

}
main(){
    /*#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    #endif*/
    fast();
    ll t = 1; //cin >> t;
    while(t--){
        solve();
    }
}


Measured memory_bytes: 256
#include <iostream>
#include<iomanip>
#include <string>
#include<vector>
#include<algorithm>
#include<utility>
#include<queue>
#include<math.h>
#include<stack>
#include<set>
#include<map>
#define INF long long int(1e18+1)
#define rep(i,n,m) for(int i=n;i<m;i++)

using namespace std;

int main()
{
	int n, ans = 0;
	cin >> n;
	int x;
	rep(i, 0, n){
		cin >> x;
		if (i % 2 == 0 && x % 2 != 0)ans++;
	}
	cout << ans << endl;
}




Measured memory_bytes: 3192
import java.util.Scanner;

public class Main {

	public static void main(String[] args) {
		// TODO 自動生成されたメソッド・スタブ

		// Scannerで文字列を取得
		Scanner scan = new Scanner(System.in); 
		int H = scan.nextInt();
		int W = scan.nextInt();
		//HとWを使って、2次元行列を作成
		String[][] HW = new String[H][W];
		//出力用の2次元配列を作成
		int[][] newHW = new int[H][W];
		
		//H文だけ、scannerから文字列を取得する。
		//文字列を格納するための配列を準備
		String[] firstStr = new String[H];
		for(int i=0; i<H; i++) {
			firstStr[i]=scan.next();
		}
		
		//配列に格納した文字列を分割し、2次元配列に格納する。
		//分割後を格納する1次元配列を準備
		for(int i=0; i<H; i++) {
			String[] str = firstStr[i].split("",0);
			for(int j=0; j < str.length; j++) {
				HW[i][j]=str[j];
			}
		}


		//2重for文を回して、1つ1つ周りをチェックしていく。
		//0&0ならば、+-1を今の座標から行っていき、文字列をチェックする。マイナスにならないことと、(3,5)を超えないことを条件とする。
		//'#'の数をカウントしていき、新規で用意した2次元配列に格納する。
		for(int i=0; i<H; i++){
			for(int j=0; j<W; j++) {
				//y軸がマイナス1になった時の判断
				int i_a = i-1;
				if(i_a>=0) {
					if(HW[i_a][j].equals("#")) {
						newHW[i][j]+=1;
					}
					int j_a =j-1;
					if(j_a>=0) {
						if(HW[i_a][j_a].equals("#")) {
							newHW[i][j]+=1;
						}
					}
					int j_b =j+1;
					if(j_b<W) {
						if(HW[i_a][j_b].equals("#")) {
							newHW[i][j]+=1;
						}
					}
				}
				//y軸がプラス１になったときの話
				int i_b =i+1;
				if(i_b<H) {
					if(HW[i_b][j].equals("#")) {
						newHW[i][j]+=1;
					}
					int j_a =j-1;
					if(j_a>=0) {
						if(HW[i_b][j_a].equals("#")) {
							newHW[i][j]+=1;
						}
					}
					int j_b =j+1;
					if(j_b<W) {
						if(HW[i_b][j_b].equals("#")) {
							newHW[i][j]+=1;
						}
					}
				}
				//y軸が変更にならずに、x軸だけ上下するとき
				int j_a =j-1;
				if(j_a>=0) {
					if(HW[i][j_a].equals("#")) {
						newHW[i][j]+=1;
					}
				}
				int j_b =j+1;
				if(j_b<W) {
					if(HW[i][j_b].equals("#")) {
						newHW[i][j]+=1;
					}
				}
			}
		}


		//標準出力に出力する
		for(int i=0; i<H; i++) {
			for(int j=0; j<W; j++) {
				if(HW[i][j].equals("#")) {
					System.out.print("#");
				}else {
					System.out.print(newHW[i][j]);	
				}
				
				if(j==(W-1)) {
					System.out.print("\r\n");
				}
			}
		}
	}

}


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

int main(){

  long long N,ans=0,H;
  cin >> N;
  H = pow(N, 0.5);
  if((N/H)==H) H--;

  if(N==1 || N==2){
    cout << ans << endl;
    return 0;
  }

  for(long long i=1; i<=H; i++){
    if((N%i)==0 && ((N/i)-1 != i)){
      ans += (N/i)-1;
    }
  }

  cout << ans << endl;

  return 0;
}


Measured memory_bytes: 256
from fractions import gcd
def lcm(x, y):
    return (x * y) // gcd(x, y)

nl = lambda: list(map(int, input().split()))
sl = lambda: input().split()
n = lambda: int(input())
s = lambda: input()

N = n()
common = n()
for i in range(N-1):
    common = lcm(common, n())

print(common)



Measured memory_bytes: 5076
import java.util.*;
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        long[] a = new long[n];
        for (int i = 0; i < n; i++) {
            a[i] = sc.nextLong();
        }
        Arrays.sort(a);

        long res1 = 0;
        long[] k = new long[n];
        res1 = solve(k, a, n, true);
        long res2 = 0;
        res2 = solve(k, a, n, false);

        long ans = Math.max(res1, res2);
        System.out.println(ans);
    }
    public static long solve(long[] k, long[] a, int n, boolean even) {
        for (int i = 0; i < n; i++) {
            if (i == 0 || i == n - 1) {
                k[i] = 1;
            } else {
                k[i] = 2;
            }
            if (even) {
                if (i % 2 == 0) {
                    k[i] *= -1;
                } else if (i % 2 != 0) {
                    k[i] *= 1;
                }
            } else {
                if (i % 2 == 0) {
                    k[i] *= 1;
                } else if (i % 2 != 0) {
                    k[i] *= -1;
                }
            }
        }
        Arrays.sort(k);
        long res = 0;
        for (int i = 0; i < n; i++) {
            res += k[i] * a[i];
        }
        return res;
    }
}


Measured memory_bytes: 62912
#include <iostream>
#include <complex>
#include <vector>
#include <string>
#include <algorithm>
#include <cstdio>
#include <numeric>
#include <cstring>
#include <ctime>
#include <cstdlib>
#include <set>
#include <map>
#include <unordered_map>
#include <unordered_set>
#include <list>
#include <cmath>
#include <bitset>
#include <cassert>
#include <queue>
#include <stack>
#include <deque>
#include <random>
#include <iomanip>
#define rep(i,n) for(int i=0;i<n;i++)
using namespace std;
typedef long long int ll;
typedef long double ld;
typedef pair<ll,ll> P;
const ll MOD=1000000007;
const ll MAX_N=500010;
const ll INF=999999999999;

int main(){
    ll n;
    cin>>n;
    string s;
    cin>>s;
    ll white=0;
    for(ll i=0;i<n;i++){
        if(s[i]=='W'){
            white++;
        }
    }
    ll ans=0;
    for(ll i=0;i<white;i++){
        if(s[n-1-i]=='R'){
            ans++;
        }
    }
    cout<<ans<<endl;
}

Measured memory_bytes: 3752
N = int(input())
A = list(map(int, input().split()))
B = list(map(int, input().split()))
A.reverse()
B.reverse()
count = 0
for i in range(N):
  if A[i] <= B[i]  <= A[i] + A[i+1]:
    count += B[i]
    A[i], A[i+1] = 0, A[i+1] - (B[i] - A[i])
  elif B[i] > A[i] + A[i+1]:
    count += A[i] + A[i+1]
    A[i], A[i+1] = 0, 0
  elif B[i] < A[i]:
    count += B[i]
    A[i] = A[i] - B[i]
print(count)

Measured memory_bytes: 24276
#include <iostream>
#include <iomanip>
using namespace std;

int main(){
    int n, k; cin >> n >> k;
    long double ans = 0;
    for (int i = 1; i <= n; i++) {
        long double p = 1.0 / n;
        int cnt = i;
        while (1 <= cnt && cnt <= k - 1) {
            cnt *= 2;
            p /= 2;
        }
        ans += p;
    }
    cout << setprecision(12) << ans << endl;
}

Measured memory_bytes: 256
import java.util.*;
import java.lang.Character;

public class Main{
    public static void main(String args[]){
        Scanner sc = new Scanner(System.in);
        String s = sc.next();
        float k = sc.nextFloat();
        int ans = 0;
        
        int j = 0;
        while(s.charAt(j) == '1'){
            ans = 1;
            if(j >= s.length()-1 || j >= k){
                break;
            }
            j++;
        }
        if(j < k){
            ans = Character.getNumericValue(s.charAt(j));
        }
        System.out.println(ans);
    }
}

Measured memory_bytes: 23124
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cctype>
using namespace std;
inline void read(int &x)
{
    int s(0),w(1);
    char c(getchar());
    while(!isdigit(c)){if(c=='-')w=-1;c=getchar();}
    while(isdigit(c)){s=(s<<3)+(s<<1)+c-'0',c=getchar();}
    x=s*w;
}
typedef long long ll;
const int MAXN=300010;
int a[MAXN],n,b[MAXN],num[MAXN],cnt,t[MAXN<<1];
#define lowbit(x) (x&-x)
inline void insert(int x,int y)
{
    while(x<=4*n)
    {
        t[x]+=y;
        x+=lowbit(x);
    }
}
inline ll ask(int x)
{
    ll ans=0;
    while(x)
    {
        ans+=t[x];
        x-=lowbit(x);
    }
    return ans;
}
inline ll check(int val)
{
    b[0]=0;
    for(register int i=1;i<=n;i++)
        if(a[i]>=val)b[i]=b[i-1]+1;
        else b[i]=b[i-1]-1;
    ll ans=0;
    memset(t,0,sizeof(t));
    insert(n,1);
    for(register int i=1;i<=n;i++)
    {
        //cout<<i<<"   "<<b[i]<<endl;
        ans+=ask(b[i]+n);
        insert(b[i]+n,1);
    }
    return ans;
}
ll d,sum;
int main()
{
    read(n);
    sum=1LL*n*(n+1)>>1;
    for(register int i=1;i<=n;i++)
        read(a[i]),num[i]=a[i];
    sort(num+1,num+n+1);
    cnt=unique(num+1,num+n+1)-num-1;
    for(register int i=1;i<=n;i++)
        a[i]=lower_bound(num+1,num+cnt+1,a[i])-num;
    int L=1,R=cnt,ans=0;
    while(L<R)
    {
        //cout<<L<<" "<<R<<endl;
        int mid=(L+R)>>1;
        mid++;
        //cout<<num[mid]<<" "<<check(mid)<<endl;
        if((d=check(mid))>=sum/2+(sum&1))L=mid,ans=mid;
         else R=mid-1;
    }
    cout<<num[L]<<endl;
}

Measured memory_bytes: 4736
# D - .. (Double Dots)
from collections import deque


def main():
    N, M, *AB = map(int, open(0).read().split())

    graph = [[] for _ in range(N + 1)]
    for a, b in zip(*[iter(AB)] * 2):
        graph[a].append(b)
        graph[b].append(a)

    # bfs
    queue = deque([1])
    signposts = [0] * (N + 1)
    signposts[1] = 1
    while queue:
        u = queue.popleft()
        for v in graph[u]:
            if not signposts[v]:
                signposts[v] = u
                queue.append(v)

    print("Yes")
    print("\n".join(map(str, signposts[2:])))


if __name__ == "__main__":
    main()


Measured memory_bytes: 56536
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Scanner;

public class Main {

    public static final int MOD = 1000000007;

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        long a = Long.parseLong(sc.next());
        String b = sc.next();
        BigDecimal A = new BigDecimal(a);
        BigDecimal B = new BigDecimal(b);
        BigDecimal c = A.multiply(B);
        System.out.println(c.setScale(0, RoundingMode.DOWN));
    }
}

Measured memory_bytes: 36036
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#define MOD1 1000000007
#define MOD2 998244353
#define LIMIT1 200002
#define LIMIT2 500002
typedef long long ll;
typedef long double ld;
#define rep(i,n) for(i=0;i<n;i++)
#define max(a,b) ((a)>(b) ? (a) : (b))
#define min(a,b) ((a)<(b) ? (a) : (b))
#define zt(a,b) (max((a),(b))-min((a),(b)))
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll nPr(int n,int r){ll i,result=1;rep(i,r){result*=(ll)(n-i);}return result;}
ll nCr(int n,int r){ll i,result=1;if(n<r) return 0;rep(i,min(r,n-r)){result*=(ll)(n-i);result/=(ll)(i+1);}return result;}
#define fact(n) nPr((int)(n),(int)(n))
#define nHr(n,r) nCr((int)((n)+(r)+1),(int)(r))
#define sankaku(x) (((x)*((x)+1))/2)
int dx[8]={1,0,-1,0,1,-1,-1,1};
int dy[8]={0,1,0,-1,1,1,-1,-1};
int upint(const void *a, const void *b) { return *(int *)a < *(int *)b ? -1 : *(int *)a > *(int *)b ? 1 : 0; }
int downint(const void *a, const void *b) { return *(int *)a < *(int *)b ? 1 : *(int *)a > *(int *)b ? -1 : 0; }
int upchar(const void* left, const void* right) {return strcmp((char *)left,(char *)right);}
int downchar(const void* left, const void* right) {return strcmp((char *)right,(char *)left);}
ll modpow(ll a, ll n, ll mod) {
  ll res = 1;
  while (n > 0) {
    if (n & 1) res = res * a % mod;
    a = a * a % mod;
    n >>= 1;
  }
  return res;
}
void initialize(){
    
}
int main(void){
  initialize();
  ll n,m,i,j,k,result=0;
  int a[LIMIT2]={0};
  char s[LIMIT1];
  
  scanf("%lld",&n);
  for(i=n;i<=100010;i++){
    result=i;
    for(j=2;j<=sqrtl(i);j++){
      if(i%j==0){
        result=0;
        break;
      }
    }
    if(result>=2) break;
  }
  printf("%lld",result);
  return 0;
}



Measured memory_bytes: 128
n, m = map(int, input().split())
views = {i+1:[0,set()] for i in range(n)}
Hs = map(int, input().split())
for i,H in enumerate(Hs):
  views[i+1][0] = H

for _ in range(m):
  a, b = map(int, input().split())
  if a not in views[b][1]:
    views[b][1].add(a)
  if b not in views[a][1]:
    views[a][1].add(b)

ans = 0
for i, view in views.items():
  for route in view[1]:
    if views[route][0] >= view[0]:
      break
  else:
    ans += 1

print(ans)

Measured memory_bytes: 61720
#include <bits/stdc++.h>
#define maxn 1001000
char s[2020];
int main(){
	scanf("%s",s);
	int slen =strlen(s);
	if(s[slen-1]=='s'){
		printf("%ses\n",s);
	} else {
		printf("%ss\n",s);
	}
}

Measured memory_bytes: 3824
import copy

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

V = []

def dfs(node, seen):

    if seen[node]==True:
        return 0

    seen[node] = True
    if not(False in seen):
        return 1
    ret = 0
    for next_node in V[node]:
        ret += dfs(next_node, copy.deepcopy(seen))
    return ret

for _ in range(N):
    V.append(set())

for _ in range(M):
    a,b = map(int, input().split())
    V[a-1].add(b-1)
    V[b-1].add(a-1)

print(dfs(0, copy.deepcopy([False]*N)))

Measured memory_bytes: 3444
#include <algorithm>
#include <iostream>
#include <vector>

using namespace std;

#define reps(i,f,n) for(int i=f; i<n; ++i)
#define rep(i,n) reps(i,0,n)
#define maxl(a,b) a = max(a, b)

pair<int, int> dp[101][210 * 500];
const int MAX = 1001001001;

void solve(int n)
{
    vector<int> p(n);
    
    rep(i, n){
        cin >> p[i];
    }
    
    rep(i, n+1){
        rep(c, 210*500){
            dp[i][c] = {-MAX, 0};
        }
    }
    dp[0][0] = {0, 0};
    
    rep(i, n){
        rep(c, (n+10)*500){
            auto r = dp[i][c];
            maxl(dp[i+1][c], r);
            
            int pr = p[i] % 1000;
            if(pr == 0){
                if(c >= 500) maxl(dp[i+1][c - 500], make_pair(r.first+1, r.second - p[i]));
            }
            else if(pr <= 500){
                maxl(dp[i+1][c + 500 - pr], make_pair(r.first+1, r.second - p[i]));
            }
            else{
                maxl(dp[i+1][c - pr + 1000], make_pair(r.first, r.second - p[i]));
                if(c >= pr - 500) maxl(dp[i+1][c - pr + 500], make_pair(r.first+1, r.second - p[i]));
            }
        }
    }
    
    auto ans = *max_element(dp[n], dp[n] + 210*500);
    cout << ans.first << ' ' << -ans.second << endl;
}

int main()
{
    int n;
    
    while(cin >> n, n){
        solve(n);
    }
}

Measured memory_bytes: 85932
#include<iostream>
#define INFTY 2147483647
using namespace std;

int n,ST[400010];

int min(int a,int b){
    if(a<b) return a;
    else return b;
}

void init(int n_){
    n=1;
    while(n<n_){n*=2;}
    fill(ST,ST+2*n-1,INFTY);
}

void update(int k,int v){
    k+=n-1;
    ST[k]=v;
    while(k>0){
        k=(k-1)/2;
        ST[k]=min(ST[k*2+1],ST[k*2+2]);
    }
}

int find(int s,int t,int k,int l,int r){
    if(r<s || t<l){return INFTY;}
    else if(s<=l && t>=r){return ST[k];}
    else{
        int ans1=find(s,t,k*2+1,l,(l+r)/2);
        int ans2=find(s,t,k*2+2,(l+r)/2+1,r);
        return min(ans1,ans2);
    }
}

int main(){
    int q,which,argm1,argm2;
    cin>>n>>q;
    init(n);
    for(int i=0;i<q;++i){
        cin>>which;
        switch(which){
            case 0:
                cin>>argm1>>argm2;
                update(argm1,argm2);
                break;
            case 1:
                cin>>argm1>>argm2;
                cout<<find(argm1,argm2,0,0,n-1)<<"\n";
                break;
        }
    }

    /*for(int i=0;i<2*n-1;++i){
        cout<<ST[i]<<"\n";
    }*/
}

Measured memory_bytes: 4112
#include <bits/stdc++.h>
#define rep(i,n) for (int i = 0; i < (n); ++i)
#define printVec(v) printf("{"); for (const auto& i : v) { std::cout << i << ", "; } printf("}\n");
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; }
using namespace std;
using P = pair<int,int>;
using ll = long long;
const ll INF = 1LL<<60;
const double PI = 3.1415926535897932;
const int MOD = 1e9 + 7;

//cin.tie(0);ios::sync_with_stdio(false);
int main() {
  int h, n;
  cin >> h >> n;
  vector<vector<int>> a(n, vector<int>(2));
  rep (i, n) {
    rep (j, 2) cin >> a[i][j];
  }
  vector<vector<ll>> dp(1010, vector<ll>(10010, INF));
  for (int i = 1; i <= n; i++) {
    for (int j = 0; j < 10010; j++) {
      int hp = j;
      if (hp <= a[i - 1][0]) {
	dp[i][j] = a[i - 1][1];
      } else {
	dp[i][j] = dp[i][hp - a[i - 1][0]] + a[i - 1][1];
      }
      chmin(dp[i][j], dp[i - 1][j]);
    }
  }

  cout << dp[n][h] << endl;
  return 0;
}


Measured memory_bytes: 82436
#include<bits/stdc++.h>
using namespace std;
const int mx = 1005;
const int inf = 3000;
char a[mx][mx];
int n, m, up[mx][mx], ans = 0;

int main(){
    scanf("%d%d", &n, &m);
    for(int i = 1; i <= n; i++) scanf("%s", a[i]+1);
    for(int i = 0; i < 1003; i++) up[0][i] = up[i][0] = up[n+1][i] = up[i][m+1] = inf;
    for(int i = 1; i <= n; i++) for(int j = 1; j <= m; j++)
        up[i][j] = a[i][j] == '#' ? 0 : min(up[i-1][j], up[i][j-1]) + 1;
    for(int i = n; i > 0; i--) for(int j = m; j > 0; j--)
        up[i][j] = min(up[i][j], min(up[i+1][j], up[i][j+1]) + 1);
    for(int i = 1; i <= n; i++) for(int j = 1; j <= m; j++) ans = max(ans, up[i][j]);
    printf("%d\n", ans);
}


Measured memory_bytes: 5120
#include <cstdio>
using namespace std;
int N, H0, H1;
int main() {
  scanf("%d", &N);
  for(int i = 0; i < N; i++) {
    scanf("%d", &H1);
    if(i == 0) H0 = H1;
    if(H0 > H1) {printf("No"); return 0;}
    if(H0 < H1) H1--;
    H0 = H1;
  }
  printf("Yes");
}

Measured memory_bytes: 128
#include <algorithm>
#include <iostream>
#include <stack>
using namespace std;

int main() {
  stack<int> st;
  string s;

  while (cin >> s) {
    if (s.at(0) == '+') {
      int a = st.top();
      st.pop();
      int b = st.top();
      st.pop();
      st.push(a + b);
    } else if (s.at(0) == '-') {
      int b = st.top();
      st.pop();
      int a = st.top();
      st.pop();
      st.push(a - b);
    } else if (s.at(0) == '*') {
      int a = st.top();
      st.pop();
      int b = st.top();
      st.pop();
      st.push(a * b);
    } else {
      st.push(atoi(s.c_str()));
    }
    if (getchar() == '\n') {
      break;
    }
  }

  cout << st.top()<< endl;

  return 0;
}


Measured memory_bytes: 3236
#include <iostream>
#include <cstdio>
#include <vector>
#include <algorithm>
#include <string>
#include <set>
#include <map>
#include <cstring>
#define INF 1000000
using namespace std;
int n,d;
int x[200002],l[200002],r[200002];
int ans=100000000;
int main(){
  cin >> n;
  for(int i=1;i+i<=n;i++){
    int a=i,b=n-i;
    int s=0,s2=0;
    while(a>0){s+=(a%10);a/=10;}
    while(b>0){s2+=(b%10);b/=10;}
    ans=min(ans,s+s2);
    //cout << ans << endl;
  }
  cout << ans << endl;
  return 0;
}


Measured memory_bytes: 256
#include <iostream>
#include <iomanip>
#include <string>
#include <vector>
#include <cmath>
#include <algorithm>
#include <map>
#include <queue>
#include <set>
#define rep(i,n) for (ll i = 0; i < (n); ++i)
#define all(x) (x).begin(), (x).end()
//#define repitr(itr, st) for(auto itr = st.begin(); itr != st.end(); ++itr)
#define P_B push_back
#define PO_B pop_back
#define E_B emplace_back
#define M_P make_pair
#define fi first
#define se second
#define debug(x) cout << #x << ": " << x << endl
//#define print(x) printf("%d\n", x)

using namespace std;
using ll = long long;
using Pii = pair<int,int>;
using Pll = pair<ll,ll>;
using VI = vector<int>;
using VL = vector<ll>;
using VVI = vector<vector<int> >;
using VVL = vector<vector<ll> >;
//int dx[4] = {1, 0, -1, 0};
//int dy[4] = {0, 1, 0, -1};
//const ll LINF = (ll)1e18 - 1;
//const int INF = 1e9 - 1;
//int:±2*10^9くらい　ll:±9*10^18くらい



void Main(){
  int N, S;
  const int MOD = 998244353;
  cin >> N >> S;
  VI A(N);
  rep(i, N) cin >> A[i];

  VVI dp(N+1, VI (S+1, 0));

  dp[0][0] = 1;

  rep(i, N){
    rep(j, S+1){
      dp[i+1][j] += dp[i][j] * 2;
      if(dp[i+1][j] >= MOD) dp[i+1][j] -= MOD;
      if(j >= A[i]) dp[i+1][j] += dp[i][j-A[i]];
      if(dp[i+1][j] >= MOD) dp[i+1][j] -= MOD;
    }
  }

  cout << dp[N][S] << endl;



}

int main()
{
	cin.tie(nullptr);
  ios_base::sync_with_stdio(false);
	cout << fixed << setprecision(15);
	Main();

  return 0;
}


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

using namespace std;

typedef long long ll;
typedef pair<ll, ll> P;

#define each(i,a) for (auto&& i : a)
#define FOR(i,a,b) for (ll i=(a),__last_##i=(b);i<__last_##i;i++)
#define RFOR(i,a,b) for (ll i=(b)-1,__last_##i=(a);i>=__last_##i;i--)
#define REP(i,n) FOR(i,0,n)
#define RREP(i,n) RFOR(i,0,n)
#define __GET_MACRO3(_1, _2, _3, NAME, ...) NAME
#define rep(...) __GET_MACRO3(__VA_ARGS__, FOR, REP)(__VA_ARGS__)
#define rrep(...) __GET_MACRO3(__VA_ARGS__, RFOR, RREP)(__VA_ARGS__)
#define pb push_back
#define all(a) (a).begin(),(a).end()
#define chmin(x,v) x = min(x, v)
#define chmax(x,v) x = max(x, v)

const ll linf = 1e18;
const int inf = 1e9;
const double eps = 1e-12;
const double pi = acos(-1);

template<typename T>
istream& operator>>(istream& is, vector<T>& vec) {
    each(x,vec) is >> x;
    return is;
}
template<typename T>
ostream& operator<<(ostream& os, const vector<T>& vec) {
    rep(i,vec.size()) {
        if (i) os << " ";
        os << vec[i];
    }
    return os;
}
template<typename T>
ostream& operator<<(ostream& os, const vector< vector<T> >& vec) {
    rep(i,vec.size()) {
        if (i) os << endl;
        os << vec[i];
    }
    return os;
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    ll n, m; cin >> n >> m;
    vector<ll> seq(m); cin >> seq;
    rep(i, m) seq[i]--;
    vector<ll> used(n, false);
    vector<ll> ans;
    rrep(i, m) {
        if (!used[seq[i]]) {
            used[seq[i]] = true;
            ans.pb(seq[i]);
        }
    }
    rep(i, n) {
        if (!used[i]) ans.pb(i);
    }
    rep(i, n) cout << ans[i]+1 << endl;
}

Measured memory_bytes: 7328
#include <iostream>
#include <cstdio>

using namespace std;

const int maxn=500005;
typedef long long ll;

int read()
{
	int f=0; char c=getchar();
	while(c>'9' || c<'0') c=getchar();
	while(c<='9' && c>='0') {f=f*10+c-'0'; c=getchar();}
	return f;
}

int a,b,c,d;

int main()
{
//	while(1) cout<<read()<<endl;
	cin>>a;
	if(a%10==7 || (a/10)%10==7 || a/100==7)
		cout<<"Yes"<<endl;
	else
		cout<<"No"<<endl;
	return 0;
}

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

int main() {
  int n;
  string s;
  string y = "";
  
  cin >> n;
  rep(i, n){
    cin >> s;
    if(s == "Y"){
      y = s;
    }
  }
  
  if(y == "Y"){
    cout << "Four" << endl;
  }else{
    cout << "Three" <<endl;
  }
}

Measured memory_bytes: 256
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <stack>
#include <queue>
#include <cmath>
#include <tuple>
#include <cstdio>
#include <bitset>
#include <sstream>
#include <iterator>
#include <numeric>
#include <map>
#include <cstring>
#include <set>
#include <functional>
#include <iomanip>

using namespace std;

#define DEBUG_ //!!提出時にコメントアウト!!
#ifdef DEBUG_
	#define dump(x)  cerr << #x << " = " << (x) << endl;
#else
	#define dump(x)  ;
#endif

#define equals(a,b) (fabs((a)-(b)) < EPS)
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n)  FOR(i,0,n)
#define SZ(x) ((int)(x).size())
#define pb push_back
#define eb emplace_back

//#define int long long

typedef long long LL;
typedef vector<int> VI;
typedef vector<LL> VL;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef pair<LL, LL> PLL;

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

template <typename T>
std::string printVector(const std::vector<T> &data)
{
    std::stringstream ss;
    std::ostream_iterator<T> out_it(ss, ", ");
    ss << "[";
    std::copy(data.begin(), data.end() - 1, out_it);
    ss << data.back() << "]";
    return ss.str();
}

template <typename T>
void print_array(const T &ary, int size){
    REP(i,size){
        cout << ary[i] << " ";
    }
    cout << endl;
}

const int MOD = 1e9+7;
const LL LINF = 1001002003004005006ll;
const int INF = 1001001001;
const double EPS = (1e-10);
int N,M,Q;

vector<vector<int>>  tr(510);

signed main(int argc, char const *argv[])
{
    cin.tie(0);
    ios::sync_with_stdio(false);
    cout << setprecision(12);

    cin >> N >> M >> Q;
    REP(i,M){
        int L,R; cin >> L >> R;
        L--; R--;
        tr[L].eb(R);
    }
    REP(i,N){
        sort(tr[i].begin(),tr[i].end());
    }
    REP(_,Q){
        int p,q; cin >> p >> q; 
        p--; q--;
        int ans = 0;
        for(int i = p; i <  N; i++){
            auto it = lower_bound(tr[i].begin(),tr[i].end(),q+1);
            ans += it - tr[i].begin();
            //dump(i)
            //dump(ans)
        }
        cout << ans << endl;
    }

}


Measured memory_bytes: 2176
using System;

namespace B___Can_you_solve_this
{
    class Program
    {
        static void Main (string[] args)
        {
            var input = Console.ReadLine ().Split (' ');
            var n = int.Parse (input[0]);
            var m = int.Parse (input[1]);
            var c = int.Parse (input[2]);
            input = Console.ReadLine ().Split (' ');
            var b = new int[m];
            for (var i = 0; i < m; i++)
            {
                b[i] = int.Parse (input[i]);
            }
            var ans = 0;
            for (var i = 0; i < n; i++)
            {
                var sum = 0;
                input = Console.ReadLine ().Split (' ');
                for (var j = 0; j < m; j++)
                {
                    sum += int.Parse (input[j]) * b[j];
                }
                if (sum + c > 0)
                {
                    ans++;
                }
            }
            Console.WriteLine (ans);
        }
    }
}

Measured memory_bytes: 13140
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define VV vector
#define pb push_back
#define bitc  __builtin_popcountl
#define m_p make_pair
#define inf 200000000000000
#define MAXN 1000001
#define eps 0.0000000001
#define fastio ios_base::sync_with_stdio(false);cin.tie(NULL);
string char_to_str(char c){string tem(1,c);return tem;}
// string to integer stoi()
// string to long long stoll()
// string.substr(position,length);
// integer to string to_string();

//////////////

#define S second
#define F first
#define int long long  

/////////////

signed main()
{
	fastio;
	#ifdef ANIKET_GOYAL
		freopen("inputf.in","r",stdin);
		freopen("outputf.in","w",stdout);
	#endif
	int n;
	cin>>n;
	int a[n+1];
	for(int i=1;i<=n;++i)
		cin>>a[i];

	int ans=0;
	for(int i=2;i<n;++i)
	{
		if(a[i]>a[i-1]&&a[i]<a[i+1])
			++ans;
		if(a[i]<a[i-1]&&a[i]>a[i+1])
			++ans;
	}
	cout<<ans;
}

Measured memory_bytes: 256
//clear adj and visited vector declared globally after each test case
//check for long long overflow
//while adding and subs check if mod becomes -ve
//while using an integer directly in a builtin function add ll
//Mod wale question mein last mein if dalo ie. Ans<0 then ans+=mod;
//Dont keep array name as size or any other key word

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#define int long long
#define IOS std::ios::sync_with_stdio(false); cin.tie(NULL);cout.tie(NULL);cout.precision(dbl::max_digits10);
#define pb push_back
#define mod 1000000007 //998244353
#define lld long double
#define mii map<int, int>
#define mci map<char, int>
#define msi map<string, int>
#define pii pair<int, int>
#define ff first
#define ss second
#define all(x) (x).begin(), (x).end()
#define rep(i,x,y) for(int i=x; i<y; i++)
#define fill(a,b) memset(a, b, sizeof(a))
#define vi vector<int>
#define setbits(x) __builtin_popcountll(x)
#define print2d(dp,n,m) for(int i=0;i<=n;i++){for(int j=0;j<=m;j++)cout<<dp[i][j]<<" ";cout<<"\n";}
typedef std::numeric_limits< double > dbl;
using namespace __gnu_pbds;
using namespace std;
typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> indexed_set;
const long long N=200005, INF=2000000000000000000;
lld pi=3.1415926535897932;
int lcm(int a, int b)
{
    int g=__gcd(a, b);
    return a/g*b;
}
int power(int a, int b, int p)
    {
        if(a==0)
        return 0;
        int res=1;
        a%=p;
        while(b>0)
        {
            if(b&1)
            res=(res*a)%p;
            b>>=1;
            a=(a*a)%p;
        }
        return res;
    }

int32_t main()
{
    IOS;
    int n;
    cin>>n;
    int co[N];
    fill(co, 0);
    int sum=0;
    for(int i=0;i<n;i++)
    {
        int a;
        cin>>a;
        co[a]++;
        sum+=a;
    }
    int q;
    cin>>q;
    while(q--)
    {
        int b,c;
        cin>>b>>c;
        sum+=((c-b)*(co[b]));
        co[c]+=co[b];
        co[b]=0;
        cout<<sum<<"\n";
    }
}

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

void trace(int a[], int n){
  for(int i = 0; i < n; i++){
    if(i>0) cout << " ";
    cout << a[i];
  }
  cout << endl;
}

int bubbleSort(int a[], int n){
  bool flag = true;
  int count = 0;
  while(flag){
    flag = false;
    for(int i = n - 1; i > 0; i--){
      if(a[i] < a[i - 1]){
	swap(a[i], a[i - 1]);
	flag = true;
	count++;
      }
    }
  }
  return count;
}

int main(){
  int n;
  int a[100];

  cin >> n;
  for(int i = 0; i < n; i++) cin >> a[i];
  int count = bubbleSort(a, n);
  trace(a, n);
  cout << count << endl;
  return 0;
}

Measured memory_bytes: 3104
#include <stdio.h>
#include <iostream>
#include <vector>
#include <string>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <math.h>
#include <algorithm>
using namespace std;
#define ll long long
const double EPS = 1e-10;

int main (){
  string s;
  ll a,b;
  ll ans=0;
  cin>>s;
  a=(s[5]-'0')*10+s[6]-'0';
  b=(s[8]-'0')*10+s[9]-'0';
  if(a<=4)cout<<"Heisei"<<endl;
  else cout<<"TBD"<<endl;
}


Measured memory_bytes: 256
//E.H//
# include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define pii pair<ll,ll>
typedef long long ll;
ll n,arr[100005];
ll d[100005],sum;;
int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    cin>>n;
    for(ll i=1;i<=n;i++){
        cin>>arr[i];
        sum+=arr[i];
    }
    arr[n+1]=arr[1];
    for(ll i=1;i<=n;i++){
        d[i]=arr[i+1]-arr[i];
    }
    if(sum%(n*(n+1)/2) == 0){
        ll k=sum/(n*(n+1)/2);
        for(ll i=1;i<=n;i++){
            if((k-d[i]) % n !=0 || k-d[i]<0){
                cout<<"NO"<<endl;
                return 0;
            }
        }
    }
    else{
        cout<<"NO"<<endl;
        return 0;
    }
    cout<<"YES"<<endl;
}

Measured memory_bytes: 5196
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
template <typename T> using vec = vector<T>;
template <typename T> using vvec = vector<vec<T>>;

int main(){
    int N;
    cin >> N;
    vec<int> A(N),B(N);
    vec<pair<int,int>> C(N),D(N);
    for(int i=0;i<N;i++){
        cin >> A[i];
        C[i] = {A[i],i};
    }
    for(int i=0;i<N;i++){
        cin >> B[i];
        D[i] = {B[i],i};
    }
    sort(C.begin(),C.end());
    sort(D.begin(),D.end());
    bool ok = true;
    for(int i=0;i<N;i++) if(C[i].first>D[i].first) ok = false;
    if(!ok){
        cout << "No" << endl;
        return 0;
    }
    vector<int> match(N,-1);
    for(int i=N-1;i>=0;i--){
        int id = lower_bound(D.begin(),D.end(),make_pair(C[i].first,-1))-D.begin();
        if(i>id){
            cout << "Yes" << endl;
            return 0;
        }else match[C[i].second] = D[i].second;
    }
    int id = C[N-1].second;
    vec<int> vis(N,0);
    while(N--){
        if(vis[id]){
            cout << "Yes" << endl;
            return 0;
        }
        vis[id] = 1;
        id = match[id];
    }
    cout << "No" << endl;
}

Measured memory_bytes: 3328
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Main {

	public static void main(String[] args) throws IOException {
		// TODO ?????????????????????????????????????????????
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		
//		while(true){
//			String input = br.readLine();
//			
//			if(input == null){
//				break;
//			}
//			
//			System.out.println(Solver.solve(input));
//		}
		
		System.out.println(Solver.solve(br.readLine(), 3));
	}

}

class Solver {
	public static String solve(String str, int offset){
		char[] carray = str.toCharArray();
		
		StringBuffer sb = new StringBuffer();
		for(int i = 0; i < carray.length ; i++){
			if(carray[i] <= 'C'){
				sb.append((char)('Z' - ('C' - carray[i])));
			}
			else {
				sb.append((char)(carray[i] - 3));
			}
		}
		
		return sb.toString();
	}
	
	public static String solve(String str){
		char[] inputCharArray = str.toCharArray();
		
		String newString = null;
		for(int i = 0; i < 26; i++){
			newString = makeNewString(inputCharArray, i);
			
			//System.out.println(newString);
			if(newString.contains("this ") || newString.contains("that ") || newString.contains("the ")
					|| newString.contains("this.") || newString.contains("that.") || newString.contains("the.")){
				break;
			}
		}
		
		return newString;
	}
	
	private static String makeNewString(char[] array, int n){
		char [] tmpArray = new char[array.length];
		for(int i = 0; i < array.length ;i++){
			if(Character.isAlphabetic(array[i])){
				tmpArray[i] = (char) (array[i] + n);
				if(tmpArray[i] > 'z'){
					tmpArray[i] -= 26;
				}
			}
			else {
				tmpArray[i] = array[i];
			}
		}
		
		return new String(tmpArray);
	}
}

Measured memory_bytes: 24112
import java.util.*;
public class Main {
	public static void main(String[] args){
        Main main = new Main();
        main.input();
        main.solve();
        main.output();
    }

    long X_start;
    long K_times;
    long D_reach;
    long x;
    long time;
    long result;

    private void input() {
        Scanner sc = new Scanner(System.in);
        X_start = sc.nextLong();
        K_times = sc.nextLong();
        D_reach = sc.nextLong();

        x = X_start;
        time = 0;
    }

    private void solve() {
        approach();
        if (time == K_times) {
            return;
        }
        if ((K_times-time) % 2 == 0) {
            return;
        } else {
            if (x < 0) {
                x = x + D_reach;
            } else {
                x = x - D_reach;
            }
        }
    }

    private void approach() {
        long range = x < 0 ? -x : x;
        long t_approaching = range / D_reach;
        if (K_times < t_approaching) {
            time = K_times;
            if (x < 0) {
                x = x + D_reach*K_times;
            } else {
                x = x - D_reach*K_times;
            }
        } else {
            time = t_approaching;
            if (x < 0) {
                x = x + D_reach*t_approaching;
            } else {
                x = x - D_reach*t_approaching;
            }
        }
    }

    private void output() {
        if (x < 0) {
            System.out.println(-x);
        } else {
            System.out.println(x);
        }
    }
}


Measured memory_bytes: 35692
#include "bits/stdc++.h"
using namespace std;
#define Rep(i,n) for(int i=0;i<(int)(n);i++)
#define For(i,n1,n2) for(int i=(int)(n1);i<(int)(n2);i++)
#define REP(i,n) for(ll i=0;i<(ll)(n);i++)
#define RREP(i,n) for(ll i=((ll)(n)-1);i>=0;i--)
#define FOR(i,n1,n2) for(ll i=(ll)(n1);i<(ll)(n2);i++)
#define RFOR(i,n1,n2) for(ll i=((ll)(n1)-1);i>=(ll)(n2);i--)
#define all(a)  (a).begin(),(a).end()
#define SORT(a) sort((a).begin(),(a).end())
#define oorret 0
#define oor(x) [&](){try{x;} catch(const out_of_range& oor){return oorret;} return x;}()
#define IOS cin.tie(0),ios::sync_with_stdio(false)
typedef long long ll;
typedef unsigned long long ull;
typedef std::pair<ll, ll> P;
template<typename T1, typename T2> inline bool chmin(T1& a, T2 b) { if (a > b) { a = b; return 1; }return 0; }
template<typename T1, typename T2> inline bool chmax(T1& a, T2 b) { if (a < b) { a = b; return 1; }return 0; }
template<class Type>struct is_vector : std::false_type {};
template<class ValueType, class Alloc>struct is_vector<std::vector<ValueType, Alloc>> : std::true_type {};
template <typename T> inline std::ostream& operator << (std::ostream& out, const std::vector<T>& v) {
	if (v.empty())return out;
	constexpr bool is_vector_v = is_vector<T>::value;
	if (is_vector_v)for (auto itr = v.begin(); itr != v.end();)out << (*itr), out << ((++itr != v.end()) ? "\n" : "");
	else for (auto itr = v.begin(); itr != v.end();)out << (*itr), out << ((++itr != v.end()) ? " " : "");
	return out;
}
inline void put() {}
template<class T> inline void put(const T& first) { std::cout << first << "\n"; }
template<class T, class... N> inline void put(const T& first, const N& ... rest) { std::cout << first << " "; put(rest...); }
inline void putn() {}
template<class T, class... N> inline void putn(const T& first, const N& ... rest) { std::cout << first << "\n"; putn(rest...); }

int main() {
	IOS;
	int n = 3;
	vector<int> a(n);
	REP(i, n) {
		cin >> a[i];
	}
	sort(all(a));
	if (a[0] == a[1] || a[1] == a[2]) {
		if (a[0] != a[2]) {
			put("Yes");
			return 0;
		}
	}
	put("No");
	return 0;
}

Measured memory_bytes: 256
#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<string>
#include<string.h>
#include<math.h>
#include<stdlib.h>
#include<vector>
#include<queue>
#include<map>
#include<tuple>
#include<bitset>
#define rep(index,num) for(int index=0;index<num;index++)
#define rep1(index,num) for(int index=1;index<=num;index++)
#define scan(argument) cin>>argument
#define prin(argument) cout<<argument<<endl
#define kaigyo cout<<endl
#define eps 1e-15
#define mp(a1,a2) make_pair(a1,a2)
typedef long long ll;
using namespace std;
typedef pair<ll,ll> pll;
typedef pair<int,int> pint;
typedef vector<int> vint;
typedef vector<ll> vll;
typedef vector<pint> vpint;
typedef vector<pll> vpll;
ll INFl=1e+18+1;
int INF=1e+9+1;
int main(){
	ll N,a[50];
	scan(N);
	rep(i,N) scan(a[i]);
	sort(a,a+N);
	ll ans=0;
	while(a[N-1]>N-1){
		ll downtimeall=0;
		int overst=upper_bound(a,a+N,N-1)-a;
		for(int i=overst;i<=N-1;i++){
			ll downtime=(a[i]-(N-1))/N+((a[i]-(N-1))%N!=0);
			a[i]-=downtime*(N+1);
			downtimeall+=downtime;
		}
		ans+=downtimeall;
		rep(i,N){
			a[i]+=downtimeall;
		}
		sort(a,a+N);
	}
	prin(ans);
	return 0;
}


Measured memory_bytes: 256
#include<bits/stdc++.h>
#define watch(x) cout << (#x) << " is " << (x) << endl
typedef long long ll;
using namespace std;
int static fast = [](){
  ios::sync_with_stdio(false);
  cin.tie(0); cout.tie(0); return 0;
}();

int main() {
    int n;
    cin >> n;
    cout << n*n*n << endl;
    return 0;
}



Measured memory_bytes: 256
#include<iostream>
#include<vector>
#include<limits>

using namespace std;

const long long INF = std::numeric_limits<long long>::max();

int main(void){
    int N;
    cin >> N;
    vector<vector<long long>> A(N, vector<long long>(N, 0));
    for(int i=0;i<N;i++){
        for(int j=0;j<N;j++){
            cin >> A[i][j];
        }
    }

    vector<vector<long long>> w = A;
    for(int k=0;k<N;k++)
        for(int i=0;i<N;i++)
            for(int j=0;j<N;j++)
                w[i][j] = min(w[i][j], w[i][k]+w[k][j]);
    
    for(int i=0;i<N;i++){
        for(int j=0;j<N;j++){
            if(w[i][j] < A[i][j]){
                cout << "-1" << endl;
                return 0;
            }
        }
    }

    long long ans = 0;
    for(int i=0;i<N;i++){
        for(int j=i+1;j<N;j++){
            bool need = true;
            for(int k=0;k<N;k++){
                if(k == i || k == j) continue;
                if(w[i][j] >= w[i][k] + w[k][j]) need = false;
            }
            if(need) ans += w[i][j];
        }
    }

    cout << ans << endl;

    return 0;
}


Measured memory_bytes: 1664
N,K = map(int,input().split())
MOD = 10**9 + 7
A = list(map(int,input().split()))
Ap = [a for a in A if a >= 0]
An = [-a for a in A if a < 0]
Ap.sort()
Ap.reverse()
An.sort()
An.reverse()

if len(Ap) == 0 and K % 2 == 1:
	ans = 1
	An.reverse()
	for i in range(K):
		ans = (ans * -An[i]) % MOD
	print(ans)
	exit()

B = [(a, 1) if a >= 0 else (-a, -1) for a in A]
B.sort()
B.reverse()
M = [0 if B[0][1] > 0 else 1]
for i in range(1, N):
	M.append(M[i-1] + (0 if B[i][1] > 0 else 1))

i = K-1
if M[i] % 2 == 0 or K == N:
	ans = 1
	v = 1 if (M[i] % 2 == 0) else -1
	for j in range(K):
		ans = (ans * B[j][0]) % MOD
	print((ans * v) % MOD)
else:
	n = M[i]
	p = K - n
	if len(An) == n or p == 0:
		n -= 1
		p += 1
	elif len(Ap) == p:
		n += 1
		p -= 1
	elif Ap[p-1] * Ap[p] >= An[n-1] * An[n]:
		n -= 1
		p += 1
	else:
		n += 1
		p -= 1
	ans = 1
	for i in range(p):
		ans = (ans * Ap[i]) % MOD
	for i in range(n):
		ans = (ans * An[i]) % MOD
	print(ans)

Measured memory_bytes: 54072
a=str(input())
b=str(input())
dp=[[0 for i in range(len(a)+1)] for j in range(len(b)+1)]
ret=[]
for i in range(1,len(b)+1):
    for j in range(1,len(a)+1):
        if b[i-1]==a[j-1]:
            dp[i][j]=dp[i-1][j-1]+1
        else:
            dp[i][j]=max(dp[i-1][j],dp[i][j-1])
while i!=0 and j!=0:
    if dp[i][j]!=dp[i-1][j] and dp[i][j]!=dp[i][j-1]:
        ret.append(a[j-1])
        i-=1
        j-=1
    else:
        if dp[i][j]==dp[i-1][j]:
            i-=1
        else:
            j-=1

print(''.join(ret[::-1]))

Measured memory_bytes: 119772