akhauriyash/Code-Regression huggingface dataset:
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <iomanip>
#include <vector>
#include <string.h>
#include <string>
#include <map>
#include <stack>
#include <queue>
#include <deque>
#include <set>
#include <math.h>
#include <algorithm>
#include <numeric>

using namespace std;

// マクロ&定数 ============================================================
typedef unsigned int uint;
typedef long long ll;
//typedef pair<int, int> P;
typedef vector<int> vint;
typedef vector<ll> vll;
typedef vector<double> vdouble;
typedef vector<bool> vbool;

typedef vector<vector<int>> vvint;
typedef vector<vector<ll>> vvll;
typedef vector<vector<double>> vvdouble;
typedef vector<vector<bool>> vvbool;

const int INF = 1e9 + 1;
const ll LLINF = 1e17 + 1;
const int DX[9] = { 0,0,1,-1, 1, 1, -1, -1, 0 }; // 4;４近傍
const int DY[9] = { 1,-1,0,0, 1, -1, 1, -1, 0 }; // 8:８近傍 9:(0,0)を含む
const ll MOD = 1e9 + 7; //10^9 + 7
const double PI = 3.14159265358979323846264338327950288;
//========================================================================

int main() {

	//==================================
	cin.tie(nullptr);
	ios_base::sync_with_stdio(false);
	cout << fixed << setprecision(30);
	//==================================

	ll N, K;
	cin >> N >> K;

	vint A(N);
	for (int i = 0; i < N; i++) {
		cin >> A[i];
	}

	ll count = 0;

	for (int i = 0; i < N; i++) {
		for (int j = i+1; j < N; j++) {
			if (A[i] > A[j]) {
				count++;
				count %= MOD;
			}
		}
	}

	ll ret_count = 0;

	for (int i = 0; i < N; i++) {
		for (int j = 0; j < N; j++) {
			if (i != j && A[i] > A[j]) {
				ret_count++;
				ret_count %= MOD;
			}
		}
	}


	cout << ((count * K) % MOD + (ret_count * (((K*(K - 1)) / 2) % MOD)) % MOD) % MOD<< endl;

}




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

using ll = long long int;

const int MAX = (int)(1e5 + 5);
const ll INF = (ll)(1e10 + 5);

using gridcost = tuple<int, int, int>;

const int MAX_H = (int)(1e2 + 5);
const int MAX_W = (int)(1e2 + 5);

int h, w;
char s[MAX_H][MAX_W];

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

set<pair<int, int>> visited;

int ans;

int main(void) {
  // Here your code !
  scanf("%d %d", &h, &w);

  for (int i = 0; i < h; ++i) {
    scanf("%s", &(s[i]));
  }

  int white = 0;

  for (int i = 0; i < h; ++i) for (int j = 0; j < w; ++j) {
    if (s[i][j] == '.') white += 1;
  }

  int length = -1;

  queue<gridcost> que;

  que.push(gridcost(0, 0, 0));
  visited.insert(make_pair(0, 0));

  while (!que.empty()) {
    gridcost tmp = que.front();
    que.pop();

    int x = get<0>(tmp);
    int y = get<1>(tmp);
    int cost = get<2>(tmp);

    if (x == (h - 1) && y == (w - 1)) {
      length = cost;
      break;
    }

    for (int i = 0; i < 4; ++i) {
      int nx = x + dx[i];
      int ny = y + dy[i];

      if (s[nx][ny] == '#') continue;
      if (visited.find(make_pair(nx, ny)) != visited.end()) continue;

      if (0 <= nx && nx < h && 0 <= ny && ny < w) {
        que.push(gridcost(nx, ny, cost + 1));
        visited.insert(make_pair(nx, ny));
      }
    }
  }

  if (length == -1) {
    printf("%d\n", -1);
  }
  else {
    ans = white - (length + 1);
    printf("%d\n", ans);
  }

  return 0;
}


Measured memory_bytes: 384
import std.conv, std.stdio;
import std.algorithm, std.array, std.range, std.string;
import std.functional;
void main()
{
    auto n = readln.chomp.to!int;
    long answer;
    foreach (a; 1..n+1)
        foreach (b; 1..n+1)
        {
            immutable g = gcd(a, b);
            foreach (c; 1..n+1)
                answer += gcd(g, c);
        }
    answer.writeln;
}
alias gcd = memoize!_gcd;

long _gcd(in long a, in long b)
{
    if (a == 0) return b;
    if (b == 0) return a;
    if (a & b & 1) return gcd(max(a, b) - min(a, b), min(a, b));
    if (!((a | b) & 1)) return gcd(a >> 1, b >> 1) << 1;
    if (a & 1) return gcd(a, b >> 1);
    if (b & 1) return gcd(a >> 1, b);
    assert (false);
}

unittest
{
    assert (gcd(0, 0) == 0);
    assert (gcd(0, 1) == 1);
    assert (gcd(0, 2) == 2);
    assert (gcd(1, 0) == 1);
    assert (gcd(1, 1) == 1);
    assert (gcd(1, 2) == 1);
    assert (gcd(2, 0) == 2);
    assert (gcd(2, 1) == 1);
    assert (gcd(2, 2) == 2);
    assert (gcd(20, 30) == 10);
    assert (gcd(16, 24) == 8);
}

Measured memory_bytes: 6724
#include<bits/stdc++.h>
using namespace std;
int l;
int main () {
    cin >> l;
    int n = log2(l) + 1;
    int m = __builtin_popcount(l) - 1 + (n - 1) * 2;
    int lst = 1;
    cout << n << " " << m << endl;
    
    for (int i = 0; i < n - 1; i++) {
	cout << i + 1 << " " << i + 2 << " " << 0 << '\n';
	cout << i + 1 << " " << i + 2 << " " << lst << '\n';
	lst *= 2;
    }
    int k = __builtin_popcount(l) - 1;
    int cur = n - 1;
    l -= pow(2, cur);
    for (int i = n - 2; i >= 0; i--) {
	if (l >= pow(2, i)) {
	    l -= pow(2, i);
	    cout << cur << " " << n << " " << lst << '\n';
	    lst += pow(2, i);
	}
	cur--;
    }

}


Measured memory_bytes: 256
#include <bits/stdc++.h>
#define rep(i,n) for(int (i)=0;(i)<(n);(i)++)
// const bool debug=true;
const bool debug=false;
#define DEBUG if(debug==true)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const ll MOD = 1000000007;

int main(void) {
    ll n;cin>>n;
    vector<ll> plus,minus;
    vector<pair<ll,ll> > control;
    ll res=0;
    rep(i,n){
        ll tmp;cin>>tmp;
        tmp > 0 ? plus.push_back(tmp) : minus.push_back(tmp);
    }
    ll np = plus.size();
    ll nm = minus.size();
    if(np>0&&nm==0){
        if(np==2){
            res = max(plus.at(0),plus.at(1)) - min(plus.at(0),plus.at(1));
            cout << res << endl;
            cout << max(plus.at(0),plus.at(1)) << " " << min(plus.at(0),plus.at(1)) << endl;
        }else{
            sort(plus.begin(),plus.end());
            rep(i,np-1){
                res += plus.at(i+1);
            }
            res -= plus.at(0);
            cout << res << endl;
            rep(i,np-2){
                cout << plus.at(0) <<" " << plus.at(i+1) << endl; 
                plus.at(0) -= plus.at(i+1);
            }
            cout << plus.at(np-1) << " " << plus.at(0) << endl;
        }
    }else if(np>0&&nm>0){
        rep(i,np){
            res += plus.at(i);
        }
        rep(i,nm){
            res -= minus.at(i);
        }
        cout << res << endl;
        for(ll i=1;i<np;i++){
            cout << minus.at(0) << " " << plus.at(i) << endl;
            minus.at(0) -= plus.at(i);
        }
        for(ll i=0;i<nm;i++){
            cout << plus.at(0) << " " << minus.at(i) << endl;
            plus.at(0) -= minus.at(i);
        }
    }else{
        sort(minus.begin(),minus.end());
        rep(i,nm-1){
            res -= minus.at(i);
        }
        res += minus.at(nm-1);
        cout << res << endl;
        rep(i,nm-1){
            cout << minus.at(n-1) << " " << minus.at(i) << endl;
            minus.at(n-1) -= minus.at(i);
        }
    }
    return 0;
}


Measured memory_bytes: 2804
#include <iostream>
#include <cstdlib>
#include <algorithm>
#include <cmath>
#include <string>
#include <vector>
using namespace std;

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

  int rail[N][N];

  for(int i=0;i<N;i++){
    for(int j=i;j<N;j++){
      rail[i][j] = 0;
    }
  }

  for(int i=0;i<M;i++){
    int L,R;
    cin >> L >> R;
    L = L-1;
    R = R-1;

    rail[L][R]++;
  }

  int cRail[N][N];
  for(int i=0;i<N;i++){
    cRail[i][i] = rail[i][i];

    for(int j=i+1;j<N;j++){
      cRail[i][j] = cRail[i][j-1] + rail[i][j];
    }
  }

  /*
  for(int i=0;i<N;i++){
    for(int j=i;j<N;j++){
      cout << rail[i][j] << " ";
    }
    cout << endl;
  }

  for(int i=0;i<N;i++){
    for(int j=i;j<N;j++){
      cout << cRail[i][j] << " ";
    }
    cout << endl;
  }
  */

  for(int i=0;i<Q;i++){
    int result = 0;

    int p,q;
    cin >> p >> q;
    p = p-1;
    q = q-1;

    for(int j=p;j<=q;j++){
      result += cRail[j][q];
    }

    cout << result << endl;
  }
  //cout << result << endl;

  return 0;
}


Measured memory_bytes: 2816
n,b,f[50001];main(a){for(;++a<5e4;)if(!f[a])for(b=a;b+a<5e4;)f[b+=a]=1;for(;~scanf("%d",&n);n&&printf("%d\n",b))for(b=0,a=1;a++<n/2;)f[a]+f[n-a]||b++;}

Measured memory_bytes: 600
#define _ 2<<17
n,i;A[_],B[_],T[_],F[_];long M,D[_],P[_],R[_];s(u,p,i,I){D[u]=1;for(i=F[u];I=B[i];i=T[i])I-p?s(I,u),D[u]=D[u]*-~D[I]%M:0;}S(u,x,p,i,I){for(m(i=F[u],p);I=B[i];I-p?S(I,R[i]*x%M+1,u),x=x*-~D[I]%M:0)i=T[i];P[u]=x%M;}m(i,p){i?m(T[i],p),R[i]=R[T[i]]*(B[i]-p?-~D[B[i]]:1)%M:0;}main(u,v){for(;~scanf("%d%d",&u,&v);)i++?A[i-1]=B[i]=u,B[i-1]=A[i++]=v:(n=u,M=v);for(;--i;F[A[i]]=i)T[i]=F[A[i]];s(*R=1,0);S(1,1);for(;i++<n;)printf("%d ",P[i]);}

Measured memory_bytes: 14592
import math
import collections
N = int(input())
mod = 10 ** 9 + 7
def prime_factorize(n):
    a = []
    while n % 2 == 0:
        a.append(2)
        n //= 2
    f = 3
    while f * f <= n:
        if n % f == 0:
            a.append(f)
            n //= f
        else:
            f += 2
    if n != 1:
        a.append(n)
    return a
# print(math.factorial(N))
# print(prime_factorize(math.factorial(N)))

c = collections.Counter(prime_factorize(math.factorial(N)))
# print(list(c.values()))
orion = list(c.values())
unizon = 1
for i in range(len(orion)):
    unizon *= (orion[i]+1)
# for i in range(len(c.values())):
print(unizon % mod)

Measured memory_bytes: 3316
# ABC042C - こだわり者いろはちゃん / Iroha's Obsession (ARC058C)
def main():
    n, k = list(map(int, input().rstrip().split()))
    D = set(map(int, input().rstrip().split()))
    ans = n
    while any(int(i) in D for i in str(ans)):
        ans += 1
    print(ans)


if __name__ == "__main__":
    main()

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

const int P = 1000000007;

int n, k, a[305], b[305], cnt[605], sum[605][605], f[605][605];

int calc(int xl, int xr, int yl, int yr) {
	return sum[xr][yr] - sum[xr][yl - 1] - sum[xl - 1][yr] + sum[xl - 1][yl - 1];
}

int main() {
	scanf("%d%d", &n, &k);
	cnt[0] = 1;
	for (int i = 2; i <= 2 * n; i += 2)
		cnt[i] = cnt[i - 2] * (i - 1ll) % P;
	for (int i = 1; i <= k; ++i) {
		scanf("%d%d", a + i, b + i);
		if (a[i] > b[i])
			swap(a[i], b[i]);
		sum[a[i]][b[i]] = 1;
	}
	for (int i = 1; i <= 2 * n; ++i)
		for (int j = 1; j <= 2 * n; ++j)
			sum[i][j] += sum[i][j - 1];
	for (int i = 1; i <= 2 * n; ++i)
		for (int j = 1; j <= 2 * n; ++j)
			sum[i][j] += sum[i - 1][j];
	int ans = 0;
	for (int l = 2; l <= 2 * n; l += 2)
		for (int i = 1; i + l - 1 <= 2 * n; ++i) {
			int j = i + l - 1;
			if (calc(1, i - 1, i, j) || calc(i, j, j + 1, 2 * n))
				continue;
			f[i][j] = cnt[j - i + 1 - 2 * calc(i, j, i, j)];
			for (int m = i + 1; m < j; m += 2)
				if (!calc(i, m, m + 1, j))
					f[i][j] = (f[i][j] - 1ll * f[i][m] * cnt[j - m - 2 * calc(m + 1, j, m + 1, j)]) % P;
			ans = (ans + 1ll * f[i][j] * cnt[2 * n - (j - i + 1) - 2 * (k - calc(i, j, i, j))]) % P;
		}
	printf("%d\n", (ans + P) % P);
	return 0;
}


Measured memory_bytes: 3072
#include<bits/stdc++.h>
#include<vector>
using namespace std;
int main(void){
  int A, B, C;
  cin >> A >> B >> C;
  if(A == B && B != C){
    cout << "Yes";
  } else if(B == C && C != A){
    cout << "Yes";
  } else if(C == A && A != B){
    cout << "Yes";
  } else {
    cout << "No";
  }
}

Measured memory_bytes: 256
#define M 100
char m[M][M],s[M][M][M],r[M],*z;
cmp(char *p,char *q){
	for(;*p=='0';p++);for(;*q=='0';q++);
	return (z=strlen(p)-strlen(q))?z:strcmp(p,q);
}
main(w,h,i,j,k,l){
	for(;scanf("%d%d",&w,&h),w;puts(z)){
		for(memset(s,0,sizeof(s)),*r=0,j=1;j<=h;j++)scanf("%s",m[j]+1);
		for(i=1;i<=w;i++)for(j=1;j<=h;j++){
			if(!isdigit(m[j][i]))continue;
			if(cmp(s[j][i-1],s[j-1][i])>0)strcpy(s[j][i],s[j][i-1]),s[j][i][strlen(s[j][i])]=m[j][i];
			else strcpy(s[j][i],s[j-1][i]),s[j][i][strlen(s[j][i])]=m[j][i];
			if(cmp(s[j][i],r)>0)strcpy(r,s[j][i]);
		}
		for(z=r;*z=='0';z++);
	}
return 0;}

Measured memory_bytes: 0
#include <bits/stdc++.h>
#include <map>
#include <set>
#include <vector>
#include <algorithm>
#include <iostream>

#include <bitset>
#include <cassert>
#include <queue>
#include <random>
#include <stack>
#include <iomanip>

using namespace std;

#define rep(i, n) for (int i = 0; i < (int)n; i++)
#define repf(i, a, b) for (int i = (int)a; i < (int)b; i++)
#define repv(x, arr) for (auto &x : arr)
#define all(v) (v).begin(), (v).end()
#define vec(name, num) vector<ll> name((num), 0);
#define mp(a, b) make_pair(a, b)
#define op(i) cout << (i) << endl;
#define F first
#define S second

typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<ld, ld> pdd;

const ll mod = 1e9 + 7;
const ll infl = 1e17;

typedef unsigned long long ull;


int main()
{
    ll n;
    cin>>n;
    map<ll, set<pll>> branch;
    ll a, b, c;
    rep(i,n-1){
        cin >> a >> b >> c;
        branch[a - 1].insert( mp(b - 1, c));
        branch[b - 1].insert(mp(a - 1, c));
    }
    ll q, k;
    cin >> q >> k;
    vector<ll> dist(n, infl);
    k--;
    dist[k] = 0;
    set<ll> check;
    check.insert(k);
    while (!(check.empty())){
        ll now = *check.begin();
        check.erase(now);
        for (auto p = branch[now].begin(); p != branch[now].end();p++)
        {
            pll pp = *p;
            ll nex = pp.F, d = pp.S;
            if (dist[nex]>dist[now]+d){
                dist[nex] = dist[now] + d;
                check.insert(nex);
            }
        }
    }
    ll x, y;
    rep(i,q){
        cin>>x>>y;
        cout << dist[x - 1] + dist[y - 1] << endl;
    }
}


Measured memory_bytes: 28672
#include <bits/stdc++.h>
#define rep(i, a, b) for (int i = a; i < (b); i++)
#define rrep(i, a, b) for (int i = a; i >= (b); i--)
#define all(x) (x).begin(), (x).end()
using namespace std;
using ll = long long;
using P = pair<int, int>;
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;
}
void hr() { cerr << "--------------" << endl; }
const int INF = 1001001001;
const int MOD = 1000000007;

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

   double r;
   cin >> r;
   printf("%0.4f\n", 2*r*acos(-1));
   return 0;
}

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

bool chmax(int &a, int b) {if (a < b) { a = b; return true; } else return false; }
bool chmin(int &a, int b) {if (a > b) { a = b; return true; } else return false; }

const int MAX = 110000;
const int NON = -1000000;
int N;
vector<int> tree[MAX];

int K;
int P[MAX];
int lo[MAX], hi[MAX];
int res[MAX];

bool dfs(int v, int p) {
  bool can = true;
  lo[v] = hi[v] = P[v];
  for (int i = 0; i < tree[v].size(); ++i) {
    int to = tree[v][i];
    if (to == p) continue;
    if (!dfs(to, v)) return false;
  }
  for (int i = 0; i < tree[v].size(); ++i) {
    int to = tree[v][i];
    if (to == p) continue;
    if (lo[to] == NON) continue;
    int tlo = lo[to] - 1;
    int thi = hi[to] + 1;
    if (lo[v] == NON) {
      lo[v] = tlo;
      hi[v] = thi;
    }
    else {
      if ( abs(lo[v] - tlo) % 2 != 0 ) return false;
      chmax(lo[v], tlo);
      chmin(hi[v], thi);
      if (lo[v] > hi[v]) return false;
    }
  }
  //cout << v << ": " << lo[v] << ", " << hi[v] << endl;
  return true;
}

void reconstruct(int v, int p) {
  if (p == -1) {
    if (lo[v] != NON) res[v] = hi[v];
    else res[v] = 0;
  }
  else {
    if (lo[v] != NON) res[v] = min(hi[v], res[p]+1);
    else res[v] = res[p]+1;
  }
  for (auto to : tree[v]) {
    if (to == p) continue;
    reconstruct(to, v);
  }
}
			    

int main() {
  while (cin >> N) {
    for (int i = 0; i < MAX; ++i) tree[i].clear(), P[i] = NON;
    for (int i = 0; i < N-1; ++i) {
      int a, b;
      scanf("%d %d", &a, &b);
      --a, --b;
      tree[a].push_back(b);
      tree[b].push_back(a);
    }
    cin >> K;
    for (int i = 0; i < K; ++i) {
      int v, p;
      scanf("%d %d", &v, &p);
      --v;
      P[v] = p;
    }
    bool can = dfs(0, -1);
    reconstruct(0, -1);

    if (can) {
      puts("Yes");
      for (int i = 0; i < N; ++i) cout << res[i] << endl;
    }
    else puts("No");
  }
}


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

signed main(){
	int n,m;
	cin>>n;
	vector<int>a(n);
	r(i,n)cin>>a[i];
	cin>>m;
	vector<int>b(m);
	r(i,m)cin>>b[i];
	cout<<lexicographical_compare(a.begin(),a.end(),b.begin(),b.end())<<endl;
}



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

#define rep(i, n) for (int i = 0; i < (n); i++)
#define repeat(i, a, b) for (int i = (a); i < (b); i++)
#define INF 1e8
#define all(a) a.begin(), a.end()
#define maxel max_element
#define minel min_element
#define ipow (int)pow
#define Bit(i) ipow(2, i)

int main() {
  int n;
  cin >> n;
  vector<int> dp(n + 1,
                 INF); // dp[money]はmoney円引き出すのに必要な操作の最小回数
  dp[0] = 0;
  repeat(money, 1, n + 1) {
    dp[money] = dp[money - 1] + 1;
    repeat(k, 1, 7) // 6^6<100000<6^7
    {
      if (money >= ipow(6, k)) {
        dp[money] = min(dp[money], dp[money - ipow(6, k)] + 1);
      }
    }
    repeat(k, 1, 6) // 9^5<100000<9^6
    {
      if (money >= ipow(9, k)) {
        dp[money] = min(dp[money], dp[money - ipow(9, k)] + 1);
      }
    }
  }

  cout << dp[n] << endl;
}


Measured memory_bytes: 768
import bisect
import copy
import heapq
import sys
import itertools
import math
import queue
input = sys.stdin.readline
sys.setrecursionlimit(1000000)
# mod = 10 ** 9 + 7

def read_values(): return map(int, input().split())
def read_index(): return map(lambda x: int(x) - 1, input().split())
def read_list(): return list(read_values())
def read_lists(N): return [read_list() for n in range(N)]
def init_dp1(init, N): return [init for _ in range(N)]
def init_dp2(init, N, M): return [[init for _ in range(M)] for _ in range(N)]


def main():
    N, A, B, C, D = read_values()

    M = A + (N - 1) * D
    m = A + (N - 1) * C
    for _ in range(N):
        if m <= B <= M:
            print("YES")
            return
        
        M -= D + C
        m -= D + C

    print("NO")


if __name__ == "__main__":
    main()



Measured memory_bytes: 4076
#include<iostream>
#include<string>
#include<algorithm>

using namespace std;

int main(){
	int n;
	string x;
	while(cin >> n, n){
		int l = 0, r = 0;
		int res = 0;
		int next = 1;
		for(int i = 0;i < n;i++){
			cin >> x;
			if(x == "lu")l = 1;
			if(x == "ru")r = 1;
			if(x == "ld")l = 0;
			if(x == "rd")r = 0;
			if(l == next && r == next){
				next ^= 1;
				res++;
			}
		}
		cout << res << endl;
	}
	return 0;
}

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

using namespace std;
using   ll=long long;

#define fast        	ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)

#define endl        	"\n"
#define pb          	push_back
#define mp          	make_pair
#define F           	first
#define S           	second
#define int             long long int
#define pll				pair<int , int>

#define ALL(v)      	v.begin(),v.end()
#define ALLR(v)     	v.rbegin(),v.rend()
#define pii         	3.14159265358979323
#define inf     		LLONG_MAX
#define ones(x)     	__builtin_popcount(x)
#define fill(a,b)   	memset(a,b,sizeof(a))
#define mod 			1000000007
#define hell            998244353

ll mod_pow(ll a,ll b,ll m)
{
    ll res = 1;
    while(b)
    {
        if(b&1)
        {
        	res=(res*a) % m;
        }
        a=(a*a) % m;
        b>>=1;
    }
    return res;
}

ll mod_inverse(ll a)
{
	return mod_pow(a , mod-2 , mod);
}

void solve()
{
	int x , k , d;
	
	cin >> x >> k >> d;
	
	int move = abs(x/d);
	
	if(x < 0) {
		if(move <= k) {
			int rem = k - move;
			
			if(rem % 2) {
				int p = x + move*d;
				
				cout << min(abs(p+d) , abs(p-d));
				return;
			}
			else {
				cout << abs(x + move*d);
				return;
			}
		}
		else {
			cout << abs(x+k*d);
			return;
		}
	}
	else {
		if(move <= k) {
			int rem = k - move;
			
			if(rem % 2) {
				int p = x - move*d;
				
				cout << min(abs(p+d) , abs(p-d));
				return;
			}
			else {
				cout << abs(x-move*d) << endl;
				return;
			}
		}
		else {
			cout << abs(x-k*d);
			return;
		}
	}
}

signed main() {
    fast;

    int t = 1;

	//cin >> t;

    while(t--) {
        solve();
    }

    return 0;
}

Measured memory_bytes: 3656
#include<bits/stdc++.h>
using namespace std;
int n,a[100000],b[100000],p[100000],t[100000];
int64_t ap;
int64_t inversions(int*s,int*e){
 if(e-s<=1)return 0;
 auto mid=s+(e-s>>1);
 int64_t rt=inversions(s,mid)+inversions(mid,e);
 int*tp=t;
 for(auto l=s,r=mid;l<mid||r<e;){
  if(l<mid&&r<e)*tp++=*l<=*r?(rt+=mid-l,*r++):*l++;
  else if(l<mid)*tp++=*l++;
  else *tp++=*r++;
 }
 copy(t,e-s+t,s);
 return rt;
}
int aux[200005];
long long count(int median)
{
 memset(aux,0,sizeof(aux));
 int sum = 0;
 long long res = 0;
 for(int i = 0; i <= n; ++i) {
  for(int j = n + sum; j >= 0; j -= ~j & j + 1) {
   res += aux[j];
  }
  for(int j = n + sum; j <= n + n; j += ~j & j + 1) {
   aux[j] ++;
  }
  if(i < n) {
   sum += (a[i] >= median ? 1 : -1);
  }
 }
 return res*2>=ap;
}
bool j(int m){
 m=b[m];
 for(int i=0;i<n;++i)p[i+1]=p[i]+(a[i]>=m?1:-1);
 int64_t iv=inversions(p,p+n+1);
 bool geq=iv<<1>=ap;
 return geq;
}
int main(int ac,char**av){
 if(ac>1){
  srand(random_device{}());
  n=rand()%10;
  for(int i=0;i<n;++i)a[i]=rand();
 } else{
  scanf("%d",&n);for(int i=0;i<n;++i)scanf("%d",a+i);
 }
 copy(a,a+n,b);
 sort(b,b+n);
 ap=n*int64_t(n+1)>>1;
 int high=n,low=0;
 while(high-low>1){
  int mid=high+low>>1;
  if(j(mid)!=count(b[mid])){
   for(int i=0;i<n;++i)cerr<<a[i]<<' ';cerr<<endl;
   cerr<<"ERROR "<<b[mid]<<' '<<j(mid)<<' '<<count(mid)<<endl;
   throw;
  }
  (j(mid)?low:high)=mid;
 }
 printf("%d\n",b[low]);
}

Measured memory_bytes: 2560
#include<stdio.h>
int n,A[100000];

int partition(int p, int r){
  int x=A[r];
  int i=p-1;
  //int i=p;
  int s,j;
  for(j=p;j<r;j++){
    if(A[j]<=x){
      i++;s=A[i];A[i]=A[j];A[j]=s;
  }}
  s=A[i+1];A[i+1]=A[r];A[r]=s;

  return i+1;
}


int main(){
  scanf("%d", &n);
  for(int i=0;i<n;i++)scanf("%d", &A[i]);
  int qr=A[n];
  int q=partition(0 ,n-1);
  for(int i=0;i<n;i++){
if(i)printf(" ");
if(i==q)printf("[");
printf("%d", A[i]);
if(i==q)printf("]");
}
printf("\n");
  return 0;
}



Measured memory_bytes: 2188
n,m=map(int,input().split())
x=list(map(int,input().split()))
if n>=m:
    print(0)
    quit()
l=m-n
x.sort()
x0=[]
for i in range(m-1):
    x0.append(x[i+1]-x[i])
x0.sort()
ans=0
for i in range(l):
    ans+=x0[i]
print(ans)

Measured memory_bytes: 13960
open Scanf
open Printf

let read_int () = scanf " %d" (fun x -> x)
let rec read_ints n = if n = 0 then [] else let i = read_int () in i :: read_ints (n-1)

let rec for_all_iter a b f = if a >= b then true else f a && for_all_iter (a+1) b f

let () =
  let a = read_int () in let b = read_int () in
  let s = scanf " %s" (fun s -> s) in
  let check1 = for_all_iter 0 a (fun i -> '0' <= s.[i] && s.[i] <= '9') in
  let check2 = s.[a] = '-' in
  let check3 = for_all_iter (a+1) (a+b+1) (fun i -> '0' <= s.[i] && s.[i] <= '9') in
  printf "%s\n" (if check1 && check2 && check3 then "Yes" else "No")


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

int main() {
    ll N; cin >> N;
    vector<ll> A(N, 0);
    ll sum = 0;
    rep(i, N) {
        cin >> A.at(i);
        sum += A.at(i);
    }

    ll half = sum / 2;
    ll tmp = 0;
    for(int i=0; i<N; i++){
        tmp += A.at(i);
        if(tmp > half){
            if((2*tmp-sum) < (sum-2*tmp+2*A.at(i))){
                ll diff = 2*tmp - sum;
                cout << diff << endl;
                return 0;
            }
            else{
                ll diff = sum-2*tmp+2*A.at(i);
                cout << diff << endl;
                return 0;
            }
        }
    }

}


Measured memory_bytes: 1792
import fractions
from functools import reduce

def lcm_base(x, y):
    return (x * y) // fractions.gcd(x, y)

def lcm_list(numbers):
    return reduce(lcm_base, numbers, 1)
  

N, M = map(int, input().split())
A = list(map(int, input().split()))
for i in range(N):
  A[i] = A[i] // 2
  
p = 0
de_2 = 0
x = A[0]
while (p == 0):
  if x % 2 == 0:
    x = x // 2
    de_2 += 1
  else:
    p = 1
    
de = 2 ** de_2
for i in range(1, N):
  if A[i] % de == 0:
    if A[i] % (de * 2) != 0:
      continue
    else:
      print("0")
      quit()
  else:
    print("0")
    quit()
  
lcm = lcm_list(A)
p = M // lcm
if p % 2 == 0:
  ans = p // 2
else:
  ans = (p + 1) // 2
  
print(ans)




Measured memory_bytes: 16348
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <map>
using namespace std;

const int maxn = 1e5 + 100;
int a[maxn], b[maxn], c[maxn];
int cnt1[maxn],cnt2[maxn];
int main() {
	int n;
	cin >> n;
	for (int i = 1; i <= n; i++) cin >> a[i];
	for (int i = 1; i <= n; i++) cin >> b[i];
	for (int i = 1; i <= n; i++) cin >> c[i];
	sort(a + 1, a + 1 + n);

	for (int i = 1; i <= n; i++) cnt2[i] = i;
	
	cnt1[0] = 0;
	sort(b + 1, b + 1 + n);
	int id = 1;

	long long tot = 0;
	
	for (int i = 1; i <= n; i++) {
		if (a[id] >= b[i]) {
			cnt1[i] = cnt1[i-1];
			continue;
		}
		while (a[id] < b[i] && id <= n) {
			tot += cnt2[id++];
		}
		cnt1[i] = id - 1;
	}

	sort(c + 1, c + 1 + n);
	long long ans = 0;
	tot = 0;
	id = 1;
	for (int i = 1; i <= n; i++) {
		if (b[id] >= c[i]) {
			ans += tot;
			continue;
		}
		while (b[id] < c[i] && id <= n) {
			tot += cnt1[id];
			id++;
		}
		ans += tot;
	}
	cout << ans << endl;
//	system("pause");



}

Measured memory_bytes: 2304
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> ii;
typedef vector<ii> vii;
#define pb push_back
#define mp make_pair
#define ms memset
#define FOR(i, a, b) for(int i = a; i <= b; i++)
#define lower_bound lb
#define upper_bound ub

int main()
{
    int n; cin >> n;
    vector<int> arr(n);
    for (int i = 0; i < n; i++) cin >> arr[i];
    ll stocks = 0; ll money = 1000;
    for (int i = 1; i < n; i++){
        if (arr[i] > arr[i-1]){
            if (stocks == 0){
                stocks += money / arr[i-1];
                money -= stocks * arr[i-1];
            }
        }
        else{
            if (stocks > 0){
                money += stocks * arr[i-1];
                stocks = 0;
            }
        }
    }
    if (arr[n-1] > arr[n-2]){
        if (stocks > 0){
            money += stocks * arr[n-1];
            stocks = 0;
        }
    }
    cout << money << '\n';
    return 0;
}

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

#define rep(i,n) for(int i = 0; i < n; i++)
#define REP(i,n) for(int i = 1; i < n; i++)
#define repll(i,n) for(ll i = 0; i < n; i++)
#define REPll(i,n) for(ll i = 1; i < n; 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 YesorNo(a) printf(a ? "Yes\n" : "No\n")
#define YESorNO(a) printf(a ? "YES\n" : "NO\n") 
#define endl "\n"
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>;
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;}


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

  ll n, x;
  cin >> n >> x;
  vi a(n);
  rep(i,n) cin >> a[i];
  ll sum = 0;
  rep(i,n){
    sum += a[i];
  }
  if(sum == x){
    cout << n << endl;
    return 0;
  }
  if(sum < x){
    cout << n-1 << endl;
    return 0;
  }
  int ans = 0;
  sort(all(a));
  rep(i,n){
    x -= a[i];
    if(x < 0){
      cout << ans << endl;
      return 0;
    }
    ans++;
  }
  cout << -1 << endl;
}

Measured memory_bytes: 256
#include<bits/stdc++.h>
using namespace std;
int main() {
	int k, s,ans=0;
	cin >> k >> s;

	for (int x = 0; x <= k; x++)
	{
		for (int y = 0; y <= k; y++)
		{
			int z = s - (x + y);
			if (z <= k && z >= 0)ans++;
		}
	}

	cout << ans << endl;
	
}

Measured memory_bytes: 256
N, C,K = map(int, input().split())

T = []
for _ in range(N):
    T.append(int(input()))

T = sorted(T)

Standard = T[0] + K
count = 1


ans = 0
for i in range(1,N):
    if (count == C) or (Standard < T[i]):
        ans = ans + 1
        count = 1 
        Standard = T[i] + K
    else:
        count = count + 1

print(ans+1)


Measured memory_bytes: 13964
//Reference : https://atcoder.jp/contests/abc163/submissions/12199470
#include<bits/stdc++.h>
//macros
using namespace std;
typedef long long ll;
typedef pair<int, int > Pii;
typedef pair<ll, ll> Pll;
typedef vector<int> Vi;
typedef vector<ll> Vll;
#define pb push_back
#define rs resize 
#define rep(i, a, b) for(int i=(a); i<(b); i++)
#define repll(i, a, b) for(ll i=(a); i<(b); i++)
#define Yes cout << "Yes" << endl
#define No cout << "No" << endl
#define YES cout << "YES" << endl
#define NO cout << "NO" << endl
#define COUT(a) cout << a << endl
#define ALL(a) (a).begin(),(a).end()
const long long int mod = 1e9 + 7;

ll sum(ll l, ll r, ll k){
    return ((l+r)*k/2);
}

int main(){
    cin.tie(nullptr);
    ios::sync_with_stdio(false); 
    ll n,k, ans=0;
    cin >> n >> k;
    repll(i,k,n+2){
        ll l = sum(0,i-1,i), r = sum(n-i+1,n,i);
        ans += (r-l+1)%mod;
        ans = ans%mod;
    }
    cout << ans << endl;
    return 0;  
}

Measured memory_bytes: 3620
#include<bits/stdc++.h>
using lint = int_fast64_t;

int main(){
        lint h, w;
        scanf("%ld %ld", &h, &w);

        lint ans = 1e18;
        for(lint i=0; i<=w; i++){
                lint a = h*i, b = h/2*(w-i), c = (h+1)/2*(w-i);
                ans = std::min(ans, std::max({a,b,c})-std::min({a,b,c}));
                b = (w-i)/2*h, c = (w-i+1)/2*h;
                ans = std::min(ans, std::max({a,b,c})-std::min({a,b,c}));
        }

        for(lint i=0; i<=h; i++){
                lint a = w*i, b = w/2*(h-i), c = (w+1)/2*(h-i);
                ans = std::min(ans, std::max({a,b,c})-std::min({a,b,c}));
                b = (h-i)/2*w, c = (h-i+1)/2*w;
                ans = std::min(ans, std::max({a,b,c})-std::min({a,b,c}));
        }
        printf("%ld\n", ans);
        return 0;
}

Measured memory_bytes: 256
#!/usr/bin/python3
import numpy as np


def main():
    N, K = map(int, input().split())
    height = np.array(list(map(int, input().split())))

    dp = np.zeros(N, dtype=int)

    for i in range(1, N):
        startpoint = max(0, i - K)
        dp[i] = min(dp[startpoint:i] + np.abs(height[i] - height[startpoint:i]))

    print(dp[N-1])


if __name__ == '__main__':
    main()


Measured memory_bytes: 22736
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using P = pair<int, int>;
const ll MOD = 1000000007;

int dp[2][10002][202];

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

    vector<int> w(N);
    for(int i=0;i<N;i++){
        cin >> w[i];
    }
    sort(w.begin(), w.end());
    w.push_back(0);
    reverse(w.begin(), w.end());

    memset(dp, 0, sizeof(dp));
    int cur = 0;
    dp[cur][0][0] = 1;
    for(int i=1;i<=N;i++){
        int next = cur ^ 1;
        for(int j=0;j<=W;j++){
            dp[next][j][i] = 0;
            for(int k=0;k<i;k++){
                dp[next][j][i] += dp[cur][j][k];
                dp[next][j][i] %= MOD;
            }
        }
        for(int j=W;j>=0;j--){
            for(int k=0;k<i;k++){
                if(j-w[i] >= 0) dp[next][j][k] = dp[cur][j-w[i]][k];
                else dp[next][j][k] = 0;
            }
        }
        cur = next;
    }

    ll ans = 0;
    for(int j=0;j<=W;j++){
        ans += dp[cur][j][0];
        for(int k=1;k<=N;k++){
            if(W - j < w[k]) ans += dp[cur][j][k];
        }
    }

    cout << ans % MOD << endl;

    return 0;
}

Measured memory_bytes: 18920
#include "bits/stdc++.h"
using namespace std;
#define fastcin {\
    cin.tie(0);\
    ios::sync_with_stdio(false);\
}
#define rep(i, a, b) for(int i = a; i < b; i++)
#define print(x) cout << x << "\n"

int main() {
    fastcin;
    int n, in, ans = 0;
    cin >> n;
    rep(i, 0, n) {
        cin >> in;
        int c = 0;
        while(!(in&1)) {
            ans++;
            in >>= 1;
        }
        ans += c;
    }
    print(ans);
    return 0;
}

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

#define endl '\n'

using namespace std;

int main(){

  cin.tie(0);     // cout と cin の同期を切る
  ios::sync_with_stdio(false);  /* cの stdioストリーム (printfとか)と*/

  int N;

  cin>>N;
  
  long long sum[200001];

  long long a;
  
  sum[0]=0;

  for(int i=1;i<=N;i++){

    cin>>a;

    sum[i]=sum[i-1]+a;

  }

  long long minmin=LLONG_MAX;
  
  for(int i=1;i<=N-1;i++){

    minmin=min(minmin,llabs(sum[i]-(sum[N]-sum[i])));

  }
  
  cout<<minmin<<endl;	       
    
   
  return 0;

}


Measured memory_bytes: 1792
#include <iostream>
#include <vector>
using namespace std;
long long INF = 2e18;
int main() {
	int n, W; cin >> n >> W;
	vector<int> w(n + 1), v(n + 1);
	for (int i = 1; i <= n; i++) {
		cin >> w[i] >> v[i];
	}
	long long dp[n + 1][W + 1];
	for (int i = 0; i <= n; i++) {
		for (int j = 0; j <= W; j++) {
			dp[i][j] = -INF;
		}
	}
	dp[0][0] = 0;
	for (int i = 1; i <= n; i++) {
		for (int j = 0; j <= W; j++) {
			if (w[i] > j) dp[i][j] = dp[i - 1][j];
			else {
				dp[i][j] = max(dp[i - 1][j], dp[i - 1][j - w[i]] + v[i]);
			}
		}
	}
	long long ans = 0;
	for (int j = 0; j <= W; j++) {
		ans = max(ans, dp[n][j]);
	}
	cout << ans << endl;
	return 0;
}

Measured memory_bytes: 79104
import java.util.Scanner;
public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int N = sc.nextInt();
		int[][] list = new int[N][100];
		for(int i = 0; i < N; i++) {
			int a = sc.nextInt();
			for(int j = 1; j <= 100; j++) {
				list[i][j-1] = (j-a)*(j-a);
			}
		}
		int ans = listCheck(list);
		System.out.println(ans);
	}
	
	public static int listCheck(int[][] list) {
		int ans = Integer.MAX_VALUE;
		for(int i = 0; i < 100; i++) {
			int check = 0;
			for(int j = 0; j < list.length; j++) {
				check += list[j][i];
			}
			if(ans > check) {
				ans = check;
			}
		}
		return ans;
	}
}

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

int main() {
  int A,B;
  bool check = 0;
  cin >> A >> B;
  
  for (int i = 1; i < B * 100; i++) {
    if (i * 8 / 100 == A && i / 10 == B) {
      cout << i << endl;
      check = 1;
      break;
    }
  }
  if (check == 0) {
    cout << -1 << endl;
  }
  
}

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

int main() {
  int K,N;
  cin >> N>> K;
  vector <int> price(N);
  for (int i=0; i<N; i++) {
    cin >> price.at(i);
  }
  sort (price.begin(),price.end());
  
 int sum=0;
  for(int i=0;i<K;i++) {
    sum+=price.at(i);
  }
  cout << sum << endl;
}

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

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

  ll a, b, c, k;
  cin >> a >> b >> c >> k;

  if(k < a)
  {
    cout << k << endl;
    return 0;
  }
  if(k <= a+b)
  {
    cout << a << endl;
    return 0;
  }
  cout << 2*a + b - k << endl;
  return 0;
}

Measured memory_bytes: 3652
#include<bits/stdc++.h>
#define int long long
#define rep(i,n) for(int i=0;i<n;i++)
#define elif else if
using namespace std;
int a,c=0;
pair<int,int> b[220000];
string s;
signed main()
{
	cin>>a;
	rep(i,a){
		cin>>b[i].second>>b[i].first;
	}
	sort(b,b+a);
	rep(i,a){
		c+=b[i].second;
		if(b[i].first<c){
			cout<<"No";
			return 0;
		}
	}
	cout<<"Yes";
}

Measured memory_bytes: 3328
#include <iostream>

using namespace std;

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

    int pre, ans;
    cin >> pre;
    ans = pre;
    for (int i=0; i<n-2; i++) {
        int b;
        cin >> b;
        ans += min(pre, b);
        pre = b;
    }

    ans += pre;
    cout << ans << endl;
}


Measured memory_bytes: 256
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10 ** 7)

# 二部グラフに分割したい
# 1レベル使っても、半分以上は同じ組になってしまう。
# kレベル使っても、2^k室までしか作れない

N = int(input())

for i in range(1,N):
    row = []
    for j in range(i+1,N+1):
        k=1
        bit = 1
        while (i&bit) == (j&bit):
            k += 1
            bit <<= 1
        row.append(k)
    print(*row)



Measured memory_bytes: 3952
#include <iostream>

using namespace std;
#define REP(i, n) for (int i = 0; (i) < (n); ++i)

int main()
{
    cin.tie(0);
    ios::sync_with_stdio(false);
    int N, M, A;
    cin >> N >> M;
    for (int i = 0; i < M; ++i)
    {
        cin >> A;
        N -= A;
    }
    if (N < 0)
        cout << -1;
    else
    {
        cout << N;
    }

    return 0;
}

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

signed main(){
    cin.tie(0);
    ios::sync_with_stdio(0);
    
    ll n,k;cin>>n>>k;
    vector<ll> a(n),b(n);
    rep(i,n) cin>>a[i]>>b[i];
    string s;ll tmp=k;
    while(tmp>0){
        s+=('0'+tmp%2);
        tmp/=2;
    }
    //sはbit表現の逆順

    ll ans=0;
    rep(i,n){
        if(k==(a[i]|k)) ans+=b[i];
    }

    rep(i,s.size()){
        if(s[i]=='0') continue;

        ll bi=k;
        // i桁を0にしてi桁以下をすべて1にした数を生成する
        rep(j,i){
            bi=(bi|(1<<j));
        }
        bi=(bi^(1<<i));
        // debug
        // cout<<i<<": "<<bitset<5>(bi)<<endl;

        ll tmp=0;
        rep(j,n){
            if(bi==(a[j]|bi)) tmp+=b[j];
        }
        ans=max(ans,tmp);
    }
    cout<<ans<<endl;
    return 0;
}


Measured memory_bytes: 1792
N, Q = map(int, input().split())
pNode = [1] * N
c = [0] * N
for i in range(N-1):
    a, b = map(int, input().split())
    pNode[b-1] = a-1
for i in range(Q):
    p, x = map(int, input().split())
    c[p-1] += x
for i in range(1, N):
    c[i] += c[pNode[i]]
print(*c)

Measured memory_bytes: 25828
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <deque>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

int main() {
    int n, x, y;
    cin >> n >> x >> y;
    --x;
    --y;
    vector<long long> ans(n);
    for (int i = 0; i < n - 1; ++i) {
        for (int j = i + 1; j < n; ++j) {
            int d;
            if (i <= x) {
                if (j <= x)
                    d = j - i;
                else if (j <= y)
                    d = min(j - i, x - i + y - j + 1);
                else
                    d = j - i - (y - x) + 1;
            } else if (i <= y) {
                if (j <= y)
                    d = min(j - i, i - x + y - j + 1);
                else
                    d = min(j - i, i - x + j - y + 1);
            } else {
                d = j - i;
            }
            ++ans[d];
        }
    }
    for (int i = 1; i < n; ++i) {
        cout << ans[i] << endl;
    }
    return 0;
}


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

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

#define m0(x) memset(x, 0, sizeof(x))
#define all(x) x.begin(), x.end()
#define rep(i, n) for(int i = 0; i < (n); i++)
#define asort(x) sort(all(x));
#define dsort(x, t) sort(x.begin(), x.end(), greater<t>());
#define dump(x) cout << #x << " = " << (x) << endl
#define UNIQUE(v) v.erase(unique(v.begin(), v.end()), v.end());
#define MINS(m, s) m.insert(make_pair(s, 1));
#define MFIN(m, s) m.find(s) != m.end()
#define YES(n) cout << ((n) ? "YES" : "NO") << endl
#define Yes(n) cout << ((n) ? "Yes" : "No") << endl

const int INF = 1e9;
const ll LINF = 1e18;
const int mod = 1e9 + 7;

int main() {
    int n;
    int a[25], b[25], c[25];
    cin >> n;
    for(int i = 1; i <= n; i++) {
        cin >> a[i];
    }
    for(int i = 1; i <= n; i++) {
        cin >> b[i];
    }
    for(int i = 1; i <= n - 1; i++) {
        cin >> c[i];
    }

    int ans = 0;
    for(int i = 1; i <= n; i++) {
        ans += b[a[i]];
        if(i != n) {
            if(a[i + 1] - a[i] == 1) {
                ans += c[a[i]];
            }
        }
    }
    cout << ans << endl;
    return 0;
}

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

using namespace std;

int main(){
    cin.exceptions(cin.failbit);
    cin.tie(0);
    cout.tie(0);
    cin.sync_with_stdio(0);
    int n, m;
    cin >> n >> m;
    vector<int> num(n, -1);
    for (int i = 0; i < m; ++i){
        int s, c;
        cin >> s >> c;
        s--;
        if (num[s] == -1){
            num[s] = c;
        }
        else {
            if (s == 0 and num[s] == 0) num[s] = c;
            else {
                if (c != num[s]){
                    cout << -1 << '\n';
                    return 0;
                }
            }
        }
    }
    if (n == 1){
        if (num[0] < 0) cout << 0 << '\n';
        else cout << num[0] << '\n';
        return 0;
    }
    if (n == 2){
        if (num[0] == 0){
            cout << -1 << '\n';
            return 0;
        }
        if (num[0] < 0) num[0] = 1;
        if (num[1] < 0) num[1] = 0;
        cout << num[0] << num[1] << '\n';
        return 0;
    }
    if (n == 3){
        if (num[0] == 0){
            cout << -1 << '\n';
            return 0;
        }
        if (num[0] < 0) num[0] = 1;
        if (num[1] < 0) num[1] = 0;
        if (num[2] < 0) num[2] = 0;
        cout << num[0] << num[1]  << num[2] << '\n';
        return 0;
    }
    
    return 0;
}

Measured memory_bytes: 256
#include <iostream>
using namespace std;
int main() {
	int n; cin >> n;
	int curr;
	for(int i = 0; i < n; i++){
		int x; cin >> x;
		if (i == 0) curr = x - 1;
		else{
			if (x > curr) curr = x - 1;
			else if (x == curr);
			else {
				cout << "No"; return 0;
			}
		}
	}
	cout << "Yes";
}

Measured memory_bytes: 256
#include <iostream>
using namespace std;
int main() {
    int m,f,r;
    char a;
    while (cin>>m>>f>>r) {
        if (m == -1 && f == -1 && r == -1) break;
        else if (m == -1 || f == -1) a = 'F';
        else if (m + f >= 80) a = 'A';
        else if (m + f >= 65) a = 'B';
        else if (m + f >= 50 || (m + f >= 30 && r >= 50)) a = 'C';
        else if (m + f >=30) a = 'D';
        else a = 'F';
        cout << a << endl;
    }
    return 0;
}

Measured memory_bytes: 3108
import java.util.*;

public class Main {

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

        boolean isHarshad = false;
        if (n % sumDigits(n) == 0) isHarshad = true;

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

    private static int sumDigits(int num){
        int sum = 0;

        while (num > 0) {
            sum = sum + (num % 10);
            num /= 10;
        }

        return sum;
    }
}


Measured memory_bytes: 21844
from collections import defaultdict
n = int(input())
a_list = list(map(int,input().split()))
q = int(input())
d = defaultdict(lambda: 0)
ans = sum(a_list)
for i in a_list:
    d[i] += 1
    
ans = sum(a_list)
for i in range(q):
    a,b = map(int,input().split())
    ans = ans + b*d[a] - a*d[a]
    d[b] += d[a]
    d[a] -= d[a]
    print(ans)

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

using namespace std;

int main() {

int N, rab, likes[100000];
int ans = 0;

memset(likes, -1, sizeof likes);
scanf("%d", &N);
for (int i = 1; i <= N; i++) {
    scanf("%d", &rab);
    likes[i] = rab;
    if (likes[rab] == i) ans++;
}
printf("%d\n", ans);

return 0;
}

Measured memory_bytes: 640
#include<bits/stdc++.h>
using namespace std;
string a,b;
int S[3001][3001];
vector<char> ans;
int main(){
    ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
    cin>>a>>b;
    for(int i=0 ; i<a.size() ; i++){
        for(int j=0 ; j<b.size() ; j++){
            if(a[i]==b[j]){
                S[i+1][j+1] = S[i][j]+1; 
            }
            else{
                S[i+1][j+1] = max(S[i+1][j],S[i][j+1]);
            }
        }
    }
    int k = S[a.size()][b.size()];
    int i=a.size();
    int j=b.size();
    //cout<<k<<"\n";
    while(k){//대칭성이 있다는게 큰 장점인듯. 그러면 n^2/2로 줄이는것도 되겠네
        while(j&&S[i][j]==k)j--;
        j++;
        while(i&&S[i][j]==k)i--;
        i++;
        ans.push_back(b[j-1]);
        k--;
        i--;
        j--;
    }
    for(int i=ans.size()-1 ; i>=0 ; i--){
        cout<<ans[i];
    }
}

Measured memory_bytes: 35456
fn b() {
    let mut input = String::new();
    std::io::stdin().read_line(&mut input).unwrap();
    let S = input.trim_right().parse::<i64>().unwrap();

    println!("{}", (S / 500) * 1000 + ((S % 500) / 5) * 5);
}

fn main() {
    b();
}

Measured memory_bytes: 4352
n=int(input())
l=list(map(int,input().split()))
ans=0
for i in range(n-2):
    for j in range(i+1, n-1):
        for k in range(j+1, n):
            if l[i]<l[j]+l[k] and l[j]<l[i]+l[k] and l[k]<l[i]+l[j] and l[i]!=l[j] and l[i]!=l[k] and l[j]!=l[k]:
                ans+=1
print(ans)

Measured memory_bytes: 9200
import java.util.Scanner;

public class Main {

    public Main(){
        Scanner sc = new Scanner(System.in);
        double A = Double.parseDouble(sc.next());
        double B = Double.parseDouble(sc.next());
        double H = Double.parseDouble(sc.next());
        double M = Double.parseDouble(sc.next());

        double K = Math.abs(30. * (H+M/60.) - 6. * M);

        System.out.println(Math.sqrt(A*A + B*B - 2*A*B*Math.cos(Math.toRadians(K))));
    }

    public static void main(String[] args) {
        Main M = new Main();
    }
}


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

using namespace std;
#define all(a)a.begin(),a.end()
using ll=long long;
const int INF = 1<<30;
const ll INFll =1LL<<62;
const int mod= int(1e9)+7;
using P = pair<ll,ll>;
using ld=long double;

int main(){
    int n;cin >>n;
    vector<P>pos(n);
    for (int i = 0; i < n; ++i) {
        ll x,range;cin >>x>>range;
        pos[i]={x+range,x-range};//r,l
    }
    sort(all(pos));
    int ans=0;
    ll r=-INFll;
    for (int i = 0; i < n; ++i) {
        if(r<=pos[i].second){
            ans++;
            r=pos[i].first;
        }
    }
    cout <<ans <<endl;
}


Measured memory_bytes: 4844
package main

import (
	"bufio"
	"fmt"
	"os"
)

func main() {

	reader := bufio.NewReader(os.Stdin)
	writer := bufio.NewWriter(os.Stdout)
	defer writer.Flush()

	var h, w , k int
	_, _ = fmt.Fscan(reader, &h, &w, &k)
	a := make([][]byte, h)
	for i := 0; i < h; i++ {
		var s string
		_, _ = fmt.Fscan(reader, &s)
		a[i] = make([]byte, w)
		for j := 0; j < w; j++ {
			a[i][j] = s[j]
		}
	}
	ans := 0
	for i := 0; i < (1 << h); i++ {
		for j := 0; j < (1 << w); j++ {
			cnt := 0
			for ii := 0; ii < h; ii++ {
				for jj := 0; jj < w; jj++ {
					if (i & (1 << ii) != 0) && (j & (1 << jj) != 0) && a[ii][jj] == '#' {
						cnt++
					}
				}
			}
			if cnt == k {
				ans++
			}
		}
	}
	fmt.Println(ans)
}


Measured memory_bytes: 1844
#include <vector>
#include <iostream>
#include <string>
#include <unordered_map>
#include <algorithm>
#include <set>
#include <map>
#include <queue>
#include <cstdio>
#include <cmath>
#include <utility>
#include <bitset>
#include <complex>
#include <stack>
#include <tuple>
 
using namespace std;
typedef long long ll;
typedef pair<ll,ll> P;
typedef tuple<ll,ll,ll> T;
const ll INF = 1e9;
const ll MOD = (1e9)+7;
const ll MAX_N = 1 << 17;

int main(){
	ll N,M;
	cin >> N >> M;
	vector<P> n(N);
	for(int i=0;i<N;i++){
		ll a,b;
		cin >> a >> b;
		n[i]=P(a,b);
	}
	vector<P> m(M);
	for(int i=0;i<M;i++){
		ll a,b;
		cin >> a >> b;
		m[i]=P(a,b);
	}
	for(int i=0;i<N;i++){
		ll mini=INF;
		ll ans=0;
		for(int j=0;j<M;j++){
			ll cost=abs(m[j].first-n[i].first)+abs(m[j].second-n[i].second);
			if(cost<mini){
				mini=cost;
				ans=j;
			}
		}
		cout << ans+1 << endl;
	}
	return 0;
}


Measured memory_bytes: 256