akhauriyash/Code-Regression huggingface dataset:
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <cmath>
#include <stack>
#include <queue>
#include <numeric>
#include <iomanip>
#include <map>
#include <set>
#include <deque>
#include <utility>
using namespace std;
using ll = long long;
#define rep(i, n) for (int i = 0; i < n; i++)
#define repk(i, k, n) for (int i = k; i < n; i++)
#define MOD 1e9 + 7
#define INF 1e9
#define SENTINEL 2e9
#define PIE 3.14159265358979323

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;}
template <class T>
T GCD(T a, T b) {if (b == 0) return a; else return GCD(b, a % b);}
template <class T>
inline T LCM(T a, T b) { return (a * b) / GCD(a, b);}

int main(){
    string s;
    cin >> s;
    string a = "";
    rep(i, s.size()){
        if (s[i] == '0')
            a.push_back('0');
        else if (s[i] == '1')
            a.push_back('1');
        else if (s[i] == 'B' && a.size())
            a.pop_back();
    }
    cout << a << endl;
}

Measured memory_bytes: 256
#include<iostream>
#include<algorithm>
#include<string>
#include<vector>
#include<set>
#include<queue>
#include<cmath>
#include<ctime>
#define R(i, n) for(int i = 0;i < n;i++)
#define S(a) scanf("%d", &a)
#define S2(a, b) scanf("%d%d", &a, &b)
#define S3(a, b, c) scanf("%d%d%d", &a, &b, &c)
#define SL(a) scanf("%lld", &a)
#define SA(i, n, a) R(i, n) scanf("%d", a + i)
#define SAL(i, n, a) R(i, n) scanf("%lld", a + i)
#define P(a) printf("%d", a)
#define PL(a) printf("%lld", a)
using namespace std;
using ll = long long;
using PR = pair<int, int>;
const ll MOD = 1e+9 + 7;

int N, H[100000];
int dp[100001];

int main() {
	S(N);
	SA(i, N, H);
	R(i, N) dp[i] = 1000000000;
	dp[0] = 0;
	R(i, N) {
		if (i + 1 < N) dp[i + 1] = min(dp[i] + abs(H[i + 1] - H[i]), dp[i + 1]);
		if (i + 2 < N) dp[i + 2] = min(dp[i] + abs(H[i + 2] - H[i]), dp[i + 2]);
	}
	P(dp[N - 1]);
}
	

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

class Dice{
    public:
        int s[6];
        void roll(char c){
            int temp;
            if(c=='E'){
                temp=s[2];
                s[2]=s[0];
                s[0]=s[3];
                s[3]=s[5];
                s[5]=temp;
            }
            if(c=='N'){
                temp=s[4];
                s[4]=s[0];
                s[0]=s[1];
                s[1]=s[5];
                s[5]=temp;
            }
            if(c=='S'){
                temp=s[1];
                s[1]=s[0];
                s[0]=s[4];
                s[4]=s[5];
                s[5]=temp;
            }
            if(c=='W'){
                temp=s[3];
                s[3]=s[0];
                s[0]=s[2];
                s[2]=s[5];
                s[5]=temp;
            }
            if(c=='R'){
                temp=s[1];
                s[1]=s[3];
                s[3]=s[4];
                s[4]=s[2];
                s[2]=temp;
            }
        }
        int top(){
            return s[0];
        }
        int front(){
            return s[1];
        }
        int right(){
            return s[2];
        }
        int left(){
            return s[3];
        }
};

int main(){
    Dice dice;
    for(int i=0;i<6;i++){
        cin>>dice.s[i];
    }
    int n;
    cin>>n;
    for(int i=0;i<n;i++){
        int t,f;
        cin>>t>>f;
        if(dice.left()==t){
            dice.roll('E');
        }
        if(dice.right()==t){
            dice.roll('W');
        }
        while(dice.top()!=t){
            dice.roll('N');
        }
        while(dice.front()!=f){
            dice.roll('R');
        }
        cout<<dice.right()<<endl;
    }
    return 0;
}


Measured memory_bytes: 3068
import math

n, k = map(int, input().split(" "))

# ans = (1 / n) * (1 / 2)**math.floor(math.log(k, 2) + 1)

# for i in range(2, n + 1):
#     d = math.floor(math.log(k, i))
#     ans += (1 / n) * (1 / 2)**d

ans = 0

for i in range(1, n + 1):
    v = i
    c = 0
    while True:
        if v >= k:
            ans += (1 / n) * (1 / 2)**c
            break
        v *= 2
        c += 1

print(ans)

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

public class Main {

  public static void main(String[] args) throws IOException {
    
    try(BufferedReader br = new BufferedReader(new InputStreamReader(System.in))) {

      int n = Integer.parseInt(br.readLine());
      String[] line;
      MyList list = new MyList();

      for(int i=0; i<n; i++) {
        line = br.readLine().split(" ");
        switch( line[0] ) {
          case "insert" :
            list.insertFirst( Integer.parseInt(line[1]) );
            break;
          case "deleteFirst" :
            list.deleteFirst();
            break;
          case "deleteLast" :
            list.deleteLast();
            break;
            case "delete" :
              list.delete( Integer.parseInt(line[1]) );
              break;
          default :
            System.out.println("n:" + i + " is failed.");
        }
      }

      System.out.println ( list.getAllElements() );
    }
  }
}

class Node {
  Node next,prev;
  int key;

  public Node(int key) {
    this.key = key;
  }
}

class MyList {
  Node nil;
  Node current;
  int size;

  public MyList() {
    nil = new Node(0);
    nil.next = nil.prev = nil;
    size = 0;
  }

  public String getAllElements() {
    StringBuilder buf = new StringBuilder(size*2);

    if(size > 0){
      current = nil.next;
      for(int i=0; i<size; i++){
        buf.append(current.key).append(" ");
        current = current.next;
      }
    }
    buf.setLength(buf.length()-1);

    return buf.toString();
  }
  public void insertFirst(int x) {
    if(size == 0) {
      current = new Node(x);
      nil.next = current;
      nil.prev = current;
      current.prev = nil;
      current.next = nil;
      size++;
    }
    else {
      Node add = new Node(x);
      
      current.prev.next = add;
      add.prev = current.prev;

      add.next = current;
      current.prev = add;

      current = add;
      size++;
    }
  }
  public void deleteFirst() {
    nil.next = current.next;
    current.next.prev = current.prev;

    current = current.next;
    size--;
  }
  public void deleteLast() {
    current = nil.prev;

    nil.prev = current.prev;
    current.prev.next = current.next;

    current = nil.next;
    size--;
  }
  public void delete(int x) {
    Node target = findFirst(x);

    if(target == nil) current = current.next;
    else {
      current.prev.next = target.next;
      current.next.prev = target.prev;
      current = nil.next;
      size--;
    }
  }
  private Node findFirst(int x) {
    while(current != nil) {
      if(current.key == x) break;
      current = current.next;
    }

    return current;
  }
}


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

#define rep(i,n) for(int i=0;i<n;i++)
#define REP(i,s,n) for(int i=(s);i<=(n);i++)
#define repr(i,n) for(int i=n-1;i>=0;i--)
#define REPR(i,s,n) for(int i=(s);i>=(n);i--)
#define all(a) (a).begin(),(a).end()
#define rall(a) (a).rbegin(),(a).rend()
#define Eunique(v) v.erase(unique(all(v)),v.end())
#define Eback(s) s.erase(s.end()-1,s.end())
#define rev(v) reverse(all(v))
#define minvec(v) *min_element(all(v))
#define maxvec(v) *max_element(all(v))
#define sumvec(v) accumulate(all(v),0LL)
#define mapmin(v) v.rbegin()->first
#define mapmax(v) v.begin()->first
#define pb push_back
#define pf push_front
#define m_p make_pair
#define DOUBLE fixed << setprecision(15)
#define OK cerr<<"OK\n"
#define OK1 cerr<<"OK1\n"
#define OK2 cerr<<"OK2\n"
#define SIZE(s) (int)s.size()
#define INF ((1LL<<62)-(1LL<<31))
#define zero(x,n) setw(x) << setfill('0') << n
#define endl '\n'

typedef long long ll;

typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<long long> vll;
typedef vector<vll> vvll;
typedef vector<double> vd;
typedef vector<vd> vvd;
typedef vector<char> vc;
typedef vector<vc> vvc;
typedef vector<bool> vb;
typedef vector<vb> vvb;
typedef vector<string> vs;

typedef pair<ll,ll> pll;
typedef pair<int,int> pii;
typedef vector<pair<int,int>> vpii;
typedef vector<pair<ll,ll>> vpll;

const double pi = acos(-1.0);
const ll mod=1000000007;
const ll mod2=998244353;

template<class A, class B>
ostream& operator<<(ostream& ost, const pair<A, B>&p) {
	ost << "{" << p.first << ", " << p.second << "} ";
	return ost;
}

template<class T>
ostream& operator<<(ostream& ost, const vector<T>&v) {
	ost << "{";
	for (int i = 0; i<(int)v.size(); i++) {
		if (i)ost << " ";
		ost << v[i];
	}
	ost << "} \n";
	return ost;
}

template<class A, class B>
ostream& operator<<(ostream& ost, const map<A, B>&v) {
	ost << "{";
	for (auto p:v) {
		ost << "{" << p.first << ", " << p.second << "} ";
	}
	ost << "} ";
	return ost;
}

template<class T>
inline bool chmax(T& a, T b){if(a<b){a=b;return true;} return false;}

template<class T>
inline bool chmin(T& a, T b){if(a>b){a=b;return true;} return false;}

void YES(bool b) {cout << ((b)?"YES\n":"NO\n");}
void Yes(bool b) {cout << ((b)?"Yes\n":"No\n");}
void yes(bool b) {cout << ((b)?"yes\n":"no\n");}
void Yay(bool b) {cout << ((b)?"Yay!\n":":(\n");}

ll powmod(ll a,ll b){ll c=1; while(b>0){ if(b&1){ c=a*c%mod; } a=a*a%mod; b>>=1; } return c;}
ll nCrmod(ll n,ll r){ll x=1,y=1; rep(i,r){x=x*(n-i)%mod; y=y*(i+1)%mod;} return x*powmod(y,mod-2)%mod;}
ll gcd(ll x, ll y) {return __gcd(x,y);}
ll lcm(ll x, ll y) {return x/__gcd(x,y)*y;}

int main()
{
	ll n,ans=1;
	cin >> n;
	rep(i,n){
		ll a;
		cin >> a;
		ans=lcm(ans,a);
	}
	cout << ans << endl;
	return 0;
}


Measured memory_bytes: 3120
# coding: utf-8
# Your code here!
N,T=map(int,input().split())

l=[]
for _ in range(N):
    a,b=map(int,input().split())
    l.append([a,b])

l.sort()

dp=[-1]*(6001)
dp[0]=0

for cand in l:
    for i in range(T)[::-1]:
        if dp[i]!=-1:
            dp[i+cand[0]]=max(dp[i+cand[0]],dp[i]+cand[1])
print(max(dp))



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

/*-------------------Define Start-------------------*/
typedef bool BL;    					// 布尔类型
typedef char SB;    					// 有符号1字节,8位
typedef unsigned char UB;    			// 无符号1字节,8位
typedef short SW;    					// 有符号短整型,16位
typedef unsigned short UW;    			// 无符号短整型,16位
typedef long SDW;    					// 有符号整型,32位
typedef unsigned long UDW;   			// 无符号整型,32位
typedef long long SLL;    				// 有符号长整型,64位
typedef unsigned long long ULL;			// 无符号长整型,64位
typedef char CH;    					// 单个字符
typedef float R32;    					// 单精度浮点数
typedef double R64;    					// 双精度浮点数

#define Rep(i, n) for (register SDW i = 0; i < (n); ++i)
#define For(i, s, t) for (register SDW i = (s); i <= (t); ++i)
#define rFor(i, t, s) for (register SDW i = (t); i >= (s); --i)
#define foreach(i, c) for (__typeof(c.begin()) i = c.begin(); i != c.end(); ++i)
#define ms0(a) memset(a,0,sizeof(a))
#define msI(a) memset(a,0x7f,sizeof(a))
#define LOWBIT(x) ((x)&(-x))

#define MP make_pair
#define PB push_back
#define ft first
#define sd second

#define pr(x) cout << #x << " = " << x << "  "
#define prln(x) cout << #x << " = " << x << endl

const ULL mod = 1e9 + 7;				//常用模数(可根据题目需要修改)
const ULL inf = 0x7fffffff;				//用来表示无限大
const ULL infLL = 0x7fffffffffffffffLL;	//用来表示无限大
/*-------------------Define End-------------------*/

const UDW maxN = 1e6 + 7;
string N;
SLL len;
SDW K;
SLL ans;

void input(){
	cin >> N >> K;
	len = N.size();
}

void do1() {
	SDW a = N[0] - '0';
	SLL posA = len; // 从1开始从右往左数a在第几个 
	
	ans += 9 * (posA - 1); // len - 1位数的情况 
	ans += a;
}

void do2() {
	SDW a = N[0] - '0';
	SLL posA = len; // 从1开始从右往左数a在第几个 
	
	SDW b = 0; // b为从左往右数第二个非零的数，不存在就置为0，然后位置设为1 
	SLL posB = 1;
	
	For(i, 1, len - 1) {
		if(N[i] != '0') {
			b = N[i] - '0';
			posB = len - i;
			break;
		}
	}
	
	ans += 9 * (posA - 1) * (posA - 2) * 9 / 2; // len - 1位数的情况 
	ans += (a - 1) * (posA - 1) * 9;
	ans += b;
	ans += (posB - 1) * 9;
}

void do3() {
	SDW a = N[0] - '0';
	SLL posA = len; // 从1开始从右往左数a在第几个 
	
	SDW b = 0; // b为从左往右数第二个非零的数，不存在就置为0，然后位置设为1 
	SLL posB = 1;
	
	For(i, 1, len - 1) {
		if(N[i] != '0') {
			b = N[i] - '0';
			posB = len - i;
			break;
		}
	}
	
	SDW c = 0; // c为从左往右数第三个非零的数，不存在就置为0，然后位置设为1 
	SLL posC = 1;
	
	For(i, len - posB + 1, len - 1) {
		if(N[i] != '0') {
			c = N[i] - '0';
			posC = len - i;
			break;
		}
	}
	
	ans += 9 * 9 * 9 * (posA - 1) * (posA - 2) * (posA - 3) / 6; // len - 1位数的情况 (朱世杰恒等式)
	ans += (a - 1) * 9 * 9 * (posA - 1) * (posA - 2) / 2;
	ans += 9 * 9 * (posB - 1) * (posB - 2) / 2; // posB上为0 
	ans += (b - 1) * (posB - 1) * 9;
	ans += c;
	ans += (posC - 1) * 9;
}

void solve(){
	switch(K) {
		case 1:{
			do1();
			break;
		}
		case 2:{
			do2();
			break;
		}
		case 3:{
			do3();
			break;
		}
		default:{
			assert(false);
		}
	}
}

void output(){
	cout << ans << endl;
}

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

Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, j, n) for (ll i = j; i < n; i++)
#define all(x) (x).begin(),(x).end()
#define pii pair<int, int>
#define vc vector
#define a first
#define b second
#define pb pusu_back
#define INF (1000000000)
#define MOD (1000000007)
#define MAX (100000)

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

class XY{
public:
  ll x, y;
  XY() {x = y = 0;}
  XY(ll u, ll v) {x = u; y = v;}
};

template<typename T1, typename T2>
void chmin(T1 &a, T2 b) {if(a > b) a = b;}
template<typename T1, typename T2>
void chmax(T1 &a, T2 b) {if(a < b) a = b;}

template<typename T1, typename T2>
ll mypow(T1 a, T2 n){
  if(n == 0) return 1;
  if(n == 1) return a;
  if(n % 2) return a * mypow(a, n - 1);
  ll tmp = mypow(a, n / 2);
  return tmp * tmp;
}

template<typename T>
int BS(vector<T> &V, int left, int right, T key){
  int mid = (left + right) / 2;
  if(V[mid] <= key) left = mid;
  else right = mid;
  if(right - mid == 1) return left;
  else return BS(V, left, right, key);
}

ll comb(ll n, ll r){
	ll res = 1;
  rep(i, 0, r){
    res *= n - i;
    res /= i + 1;
  }
  return res;
}

template<typename T>
T euclid(T a, T b){
  if(b == 0) return a;
  else return euclid(b, a % b);
}

vector<ll> fact(MAX, 0), fact_inv(MAX, 0);

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

//MOD of Combination

ll pow_mod(ll a, ll b){
	ll res = 1;
	while(b > 0){
		if(b & 1) res = res * a % MOD;
		a = a * a % MOD;
		b >>= 1;
	}
	return res;
}

void prepare_comb_mod(ll n){
	fact[0] = 1LL;
	for(ll i = 0; i < n; i++) fact[i+1] = fact[i] * (i + 1) % MOD;
	fact_inv[n] = pow_mod(fact[n], MOD - 2);
	for(ll i = n - 1; i >= 0; i--) fact_inv[i] = fact_inv[i + 1] * (i + 1) % MOD;
}

ll comb_mod(ll n, ll r){
	return (fact[n] * fact_inv[r]) % MOD * fact_inv[n - r] % MOD;
}

//Union-Find Tree

int UFT_par[MAX];
int UFT_rank[MAX];

void UFT_init(int n){
  rep(i, 0, n){
    UFT_par[i] = i;
    UFT_rank[i] = 0;
  }
}

int UFT_find(int x){
  if(UFT_par[x] == x){
    return x;
  }else{
    return UFT_par[x] = UFT_find(UFT_par[x]);
  }
}

void UFT_unite(int x, int y){
  x = UFT_find(x);
  y = UFT_find(y);
  if(x == y) return;
  if(UFT_rank[x] < UFT_rank[y]){
    UFT_par[x] = y;
  }else{
    UFT_par[y] = x;
    if(UFT_rank[x] == UFT_rank[y]) UFT_rank[x]++;
  }
}

bool UFT_same(int x, int y){
  return UFT_find(x) == UFT_find(y);
}

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

void Main() {
  int N, M; cin >> N >> M;
  vc<int> p(N);
  rep(i, 0, N){
    int tmp; cin >> tmp; tmp--;
    p[tmp] = i;
  }
  UFT_init(N);
  rep(i, 0, M){
    int x, y; cin >> x >> y; x--; y--;
    UFT_unite(x, y);
  }
  int res = 0;
  rep(i, 0, N){
    if(UFT_same(i, p[i])) res++;
  }
  cout << res << endl;
}

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

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

  Main();
}


Measured memory_bytes: 2944
// Mon Aug 24 16:24:09 IST 2020        
#include <bits/stdc++.h>        
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>
#define forn(ii,a,b)       for(int ii = (int)a ; ii < (int)b  ; ii++ )
#define scan(any)          for(auto &i:any) cin>>i;
#define debug(x)           cerr << #x << " " << x << " -- \n"
#define I__O               ios::sync_with_stdio(false),cin.tie(NULL),cout.tie(NULL)
#define int64              long long   
#define all(x)             (x).begin(),(x).end() 
#define ff                 first
#define ss                 second
#define pppp               pair <int,int> 
#define flushOff           fflush(stdout) 
#define precise(x)         cout << fixed << setprecision(12) << x << '\n'; 
using namespace std ;    
// policy based   
using namespace __gnu_pbds;
typedef tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update> indexed_set ; 
 
const int MOD1 = 1e9+7;
const int MOD2 = 1e9+9;
const double pi = 3.141592653589793238;
const int INF = 2e9+7; 
// const int N = 2e5+7;
struct compare{
    bool operator()(pair <int,int> A, pair <int,int> B){
        if(A.first == B.first)
            return A.second > B.second;
        return A.first > B.first;
    }
}; 
void print(vector <int>v){
    for(int t : v){
        cout << t << ' ';
    }
    cout << '\n';
} 
int tests(){
    int n;
    cin >> n;
    vector <int> monster(n+1),hero(n);
    for(auto &x : monster){
        cin >> x;
    }
    for(auto &x : hero){
        cin >> x;
    }
    int64 ans = 0;
    forn(i,0,n){
        if(monster[i] <= hero[i]){
            ans += (int64)monster[i];
            hero[i] -= monster[i];
            ans += (int64)min(hero[i],monster[i+1]);
            if(hero[i])
                monster[i+1] -= hero[i];
            monster[i+1] = max(0, monster[i+1]);
        }
        else if(monster[i] > hero[i]){
            ans += (int64)hero[i];
            monster[i] -= hero[i];
            hero[i] = 0;
        }
    }
    cout << ans << '\n';
    return 0;
}    
int main(){
    I__O ;      
    int cases = 1 ;  
    // cin >> cases ;
    while(cases--)
        tests(); 
    return 0 ; 
}
 


Measured memory_bytes: 4000
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Main {

	public static void main(String[] args) throws IOException{
		BufferedReader br =new BufferedReader(new InputStreamReader(System.in));
		String arraynum = br.readLine();
		String[] ary = arraynum.split(" ");
		int[][] a = new int[Integer.parseInt(ary[0])][Integer.parseInt(ary[1])];
		int[] v = new int[Integer.parseInt(ary[1])];
		int[] ans = new int[Integer.parseInt(ary[0])];
		for(int i = 0 ; i < a.length ; i++){
			String num = br.readLine();
			String[] numa = num.split(" ");
			for(int j = 0 ; j < a[i].length ; j++){
				a[i][j] = Integer.parseInt(numa[j]);
			}
		}
		
		for (int i = 0 ; i < v.length ; i++){
			String vect = br.readLine();
			v[i] = Integer.parseInt(vect);
		}
		for(int i = 0 ; i < a.length ; i++){
			for(int j = 0 ; j < a[i].length ; j++){
				ans[i] = a[i][j]*v[j] + ans[i];
			}
			System.out.println(ans[i]);
		}

	}

}

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

void max_self(int &a, int b){
	a = max(a,b);
}

int main(){
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	
	int k;
	string s;
	cin >> k >> s;
	if((int)s.length() <= k) cout << s;
	else cout << s.substr(0,k) << "...";
	return 0;
}


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

ans = 0
if A + B > 2 * C:
    z = min(X, Y)
    X -= z
    Y -= z
    ans += 2 * z * C

# 偏り分
if 2 * C < A:
    k = X
    X -= k
    Y -= k
    ans += 2 * C * k
if 2 * C < B:
    k = Y
    X -= k
    Y -= k
    ans += 2 * C * k

# あまり分
ans += A * max(0, X) + B * max(0, Y)
print(ans)

Measured memory_bytes: 38384
#include<bits/stdc++.h>
using namespace std;
#define all(x) (x).begin(),(x).end()
typedef long long ll;
#define rep(i,n) for(ll i=0, i##_len=(n); i<i##_len; ++i)
#define REP(i,num,n) for(ll i=num, i##_len=(n); i<i##_len; ++i)
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; }

ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
const ll LLINF = 1LL<<60;
const int INTINF = 1<<30;
const int MOD = 1000000007;

void add(long long &a, long long b) {
  a += b;
  if (a >= MOD) a -= MOD;
}

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


int main(void){
    cin.tie(0);
    ios::sync_with_stdio(false);
    int H, W, D;
    cin >> H >> W >> D;

    pair<int, int> place[H * W + 1];
    // 各数値の座標を記録

    for (int i = 0; i < H; i++) {
        for (int j = 0; j < W; j++) {
            int A;
            cin >> A;
            place[A] = {i, j};
        }
    }

    int mp[H * W + 1];
    // 始点からの消費魔力をメモ

    // 下ごしらえ開始
    for (int i = 1; i <= H * W; i++) {
        if (i <= D) {
            // いわゆる「始点」なので消費魔力は0
            mp[i] = 0;

        } else {
            // 座標を求める
            int x1 = place[i - D].first;
            int y1 = place[i - D].second;
            int x2 = place[i].first;
            int y2 = place[i].second;

            // 魔力を計算
            mp[i] = mp[i - D] + abs(x2 - x1) + abs(y2 - y1);
        }
    }

    int Q;
    cin >> Q;
    for (int i = 0; i < Q; i++) {
        // ここからクエリを捌いていく
        int L, R;
        cin >> L >> R;

        // 差分を取るだけで答えが出る
        cout << mp[R] - mp[L] << endl;
    }
    return 0;
}

Measured memory_bytes: 2176
def solve(A, B, K):
    i = 1
    ans = []
    while i <= max(A, B):
        if A % i == 0 and B % i == 0:
            ans.append(i)
        i += 1
    return ans[::-1][K-1]


if __name__ == "__main__":
    A, B, K = tuple(map(int, input().split(" ")))
    print(solve(A, B, K))


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

using namespace std;

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

    int Nd=2*d+1;

    int ans=n/Nd;
    int an2=n%Nd;

    if(an2!=0){
        ans++;
    }

    cout << ans << endl;
}

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

#define rep(i, n)     for(int i = 0;      i < int(n); ++i)
#define rep3(i, a, b) for(int i = int(a); i < int(b); ++i)
#define all(x)        (x).begin(),(x).end()
#define sz(x)         int(x.size())
#define mp            make_pair
#define pb            push_back


typedef long long ll;

using namespace std;

int main(){
  int n, i;
  cin >> n >> i;
  cout << n-i+1 << endl;

  return 0;
}


Measured memory_bytes: 256
#include <iostream>
#include <iomanip>
#include <vector>
#include <utility>
#include <algorithm>
using namespace std;

typedef pair<double, int> data;

int main() {
    vector<data> a, b, c;
    vector<data> all;
    for (int i = 0; i < 8; i++) {int n; double time; cin >> n >> time; a.push_back(make_pair(time, n));}
    for (int i = 0; i < 8; i++) {int n; double time; cin >> n >> time; b.push_back(make_pair(time, n));}
    for (int i = 0; i < 8; i++) {int n; double time; cin >> n >> time; c.push_back(make_pair(time, n));}
    sort(a.begin(), a.end());
    sort(b.begin(), b.end());
    sort(c.begin(), c.end());
    all.insert(all.end(), a.begin()+2, a.end());
    all.insert(all.end(), b.begin()+2, b.end());
    all.insert(all.end(), c.begin()+2, c.end());
    sort(all.begin(), all.end());
    cout.setf(ios::showpoint);
    cout.precision(4);
    cout << a[0].second << ' ' << a[0].first << endl;
    cout << a[1].second << ' ' << a[1].first << endl;
    cout << b[0].second << ' ' << b[0].first << endl;
    cout << b[1].second << ' ' << b[1].first << endl;
    cout << c[0].second << ' ' << c[0].first << endl;
    cout << c[1].second << ' ' << c[1].first << endl;
    cout << all[0].second << ' ' << all[0].first << endl;
    cout << all[1].second << ' ' << all[1].first << endl;
    return 0;
}

    

Measured memory_bytes: 0
d,m=1e9+7,l,dp[10001][2][100];S[9999];
char*s=S;
f(i,k,t,x,L)
{
	int*r=dp[i][t]+k;
	*r=i-l?*r:!k;
	if(!~*r)
	{
		*r=0;
		L=t?9:s[i]-48;
		for(x=0;x<=L;*r%=m)*r+=f(i+1,(k+x++)%d,t|x<L);
	}
	return*r;
}
main()
{
	scanf("%s%d",S,&d);
	memset(dp,-1,sizeof dp);
	l=strlen(s);
	printf("%d",(f(0,0,0)-1+m)%m);
}

Measured memory_bytes: 8576
//#pragma GCC optimize("Ofast")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
//#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>

#define pb push_back
#define sz(x) ((int)(x).size())
#define all(x) (x).begin(),(x).end()
#define ll long long



using namespace std;

void file(){
	#ifndef ONLINE_JUDGE
    	freopen("in.txt", "r", stdin);
	#endif
}
int tc;
const int N=2e5+5,M=2e6+5,MOD=1e9+7,OO=1e9;
 	
int main(){
	ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
	//file();	
 	
 	double a,b;
 	cin>>a>>b;
 	for(double i=1;i<=1e6;i++){
 		if((floor(i*0.08) == a) && (floor(i*0.1) == b)){
 			cout<<i<<endl;
 			return 0;
 		}
 	}

 	cout<<"-1\n";
 	
}		


Measured memory_bytes: 256
#include<bits/stdc++.h>
#define STDIO
#define MOD 1000000007
#define testbit(mask,i) (((mask)>>(i))&1)
#define setbit(mask,i) ((mask)|(1<<i))
#define delbit(mask,i) ((mask)^(1<<i))
#define lowbit(x) ((x)&(-x))
#define INF MOD
using namespace std;
int n;
long long m;
long long finv[10010],p3[10010],p2[10010];
long long inverse(long long x){
	long long ret=1LL;
	int bs=m-2;
	while(bs){
		if(bs&1)
			ret=(ret*x)%m;
		x=(x*x)%m;
		bs>>=1;
	}
	return ret;
}
int main(){
	#ifndef STDIO
		freopen("input.in","r",stdin);
		freopen("output.out","w",stdout);
	#endif
	ios::sync_with_stdio(0);
	cin.tie(NULL);
	cout.tie(NULL);
	cin>>n>>m;
	finv[0]=1LL;
	for(int i=1;i<=10000;i++){
		finv[i]=(finv[i-1]*inverse(i))%m;
	}
	p3[0]=1LL;
	for(int i=1;i<=10000;i++){
		p3[i]=(p3[i-1]*inverse(3))%m;
	}
	p2[0]=1LL;
	for(int i=1;i<=10000;i++){
		p2[i]=(p2[i-1]*inverse(2))%m;
	}
	long long s=0LL;
	for(int i=0;i<=n;i++){
		for(int j=0;j<=n-i;j++){
			s=(s+(finv[i]*finv[j]%m*finv[3*n-3*i-2*j]%m*p3[i]%m*p2[j]%m))%m;
		}
	}
	for(int i=1;i<=n*3;i++){
		s=(s*i)%m;
	}
	cout<<s<<endl;
	return 0;
}


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

int main(){
  int n; cin>>n;
  vector<long long> a(n);
  for(int i=0; i<n; ++i)cin>>a[i];

  long long l=a[0], r=a[n-1];

  int i=0, j=n-1;
  while(i+1!=j){
    if(l>=r){r+=a[j-1]; --j;}
    else{l+=a[i+1]; ++i;}
  }

  cout<<abs(l-r)<<endl;
}

Measured memory_bytes: 1792
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <map>
#include <set>
#include <vector>
#include <stack>
#include <queue>
#include <bitset>
#include <algorithm>
#include <numeric>
#include <functional>
using namespace std;

#define Rep(b, e, i) for(int i = b; i <= e; i++)
#define Repr(e, b, i) for(int i = e; i >= b; i--)
#define rep(n, i) Rep(0, n-1, i)
#define repr(n, i) Repr(n-1, 0, i)
#define all(v) (v).begin(), (v).end()
#define pb(x) push_back(x)
#define uniq(x) (x).erase(unique(all(x)),(x).end())

typedef long long ll;
typedef pair<int, int> P;
typedef std::priority_queue<int> IntPrioQueue; //Z->A
typedef std::priority_queue<int, std::vector<int>, std::greater<int> > IntReversePrioQueue; //A->Z

const int MAX = 256;
const double PI = 3.14159265358979323846;
const double EPS = 1e-12;
const int INF = 1<<29;
const int MOD = 1000000007;

int ts[MAX], vs[MAX], tacc[MAX], maxi[MAX*MAX];

void solve(void){
    //input
    int N;
    scanf("%d\n", &N);
    rep(N, i) {
        scanf("%d", &ts[i]);
        tacc[i+1] = ts[i] + tacc[i];
    }
    rep(N, i) scanf("%d", &vs[i]);
    tacc[N+1] = INF;
    int r = 2*tacc[N];
    Rep(0, r, t) maxi[t] = min(t, r-t);

    rep(N, n) {
        Rep(0, r, t) {
            if (t < 2*tacc[n]) maxi[t] = min(maxi[t], (2*tacc[n]-t)+2*vs[n]);
            else if (t > 2*tacc[n+1]) maxi[t] = min(maxi[t], abs(2*tacc[n+1]-t)+2*vs[n]);
            else maxi[t] = min(maxi[t], 2*vs[n]);
        }
    }
    double ans = 0.;
    Rep(0, r-1, t) ans += (double)(maxi[t+1] + maxi[t])/2/4;

    printf("%.6lf\n", ans);
}

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


Measured memory_bytes: 384
#include<bits/stdc++.h>
using namespace std;
#define ll  long long int
#define ld long double
#define REP(i,a,b) for(i=a;i<b;i++)
#define rep(i,a) REP(i,0,a)
#define REV(i,a,b) for(i=a-1;i>=b;i--)
#define rev(s) reverse(s.begin(),s.end())
#define pb push_back
#define mp make_pair
#define ord(v) sort(v.begin(),v.end());
const ll MAXN=2*1e5+10;
#define MAX 2*1e9
#define fi first
#define se second
#define pi 3.141592653589793238
#define rem 1000000007
#define eps 1e-9
// fflush(stdout);
//lower_bound,upper_bound,equal_range
int main()
{
	#ifndef ONLINE_JUDGE
    freopen("input.txt","r",stdin);
    freopen("output.txt","w",stdout);
    #endif
	ll n,W;
	cin>>n>>W;
	ll i,j;
	vector<ll>v(n),w(n);
	ll total = 0;
	rep(i,n)
	{
		cin>>w[i]>>v[i];
		total+=v[i];
	}
	vector<ll>dp(total+1,INT_MAX);
	dp[0]=0;
	rep(i,n)
	{
		for(int j=total;j>=v[i];j--)
		{
			dp[j]=min(dp[j],dp[j-v[i]]+w[i]);
		}
	}
	ll m = INT_MIN;
	rep(i,total+1)
	{
		if(dp[i]<=W){
			m=max(m,i);
		}
	}
	cout<<m;
	return 0;
}

Measured memory_bytes: 3980
def main():
    N,M,X,Y = map(int, input().split())
    x_wants=[int(s) for s in input().split()]
    y_wants=[int(s) for s in input().split()]

    x_wants.append(X)
    y_wants.append(Y)
    x_wants = sorted(x_wants)
    y_wants = sorted(y_wants)

    x_max = x_wants[len(x_wants)-1]
    y_min = y_wants[0]

    if((y_min-x_max)<1):
        print('War')
        return
    
    print('No War')

if __name__ == "__main__":
    main()

Measured memory_bytes: 3064
#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<string>
#include<cmath>
#include<numeric>
#include<vector>
#include<tuple>
using namespace std;
#define FOR(i,a,b) for(ll i=a;i<b;i++)
#define vint vector<int> 
#define vvint vector<vint>
#define ct(a) cout<<a<<endl
typedef long long ll;
const int MOD = 1e9 + 7;

string ans = "AKIHABARA     ";
string s;
string unko;
bool flag = true;
int main(void)
{
	cin >> s;
	int k = 0;
	FOR(i, 0, s.length()) {
		if (k > 9) {
			break;
		}
		if (s[i] != ans[k]) {
			unko.push_back('A');
			k += 2;
		}
		else k++;
		unko.push_back(s[i]);
	}
	if (unko=="AKIHABARA"||unko=="AKIHABAR")cout << "YES" << endl;
	else cout << "NO" << endl;
	return 0;
}

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

using namespace std;
using ll =long long;

#define SORT(a) sort((a).begin(),(a).end())
#define rSORT(a) reverse((a).begin(),(a).end())
#define For(i, a, b)    for(int i = (a) ; i < (b) ; ++i)
#define rep(i, n)       For(i, 0, n)
#define debug(x)  cout << #x << " = " << (x) << endl;
void coY() {cout <<"Yes"<<endl;}
void coN(){cout <<"No"<<endl;}

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 ll INF = 1LL << 60;

//Write From this Line

int main()
{
//sunuke ac
	int n , m; 
	cin >>  n >> m; 
	vector<int> ac(n) , pena(n);
	rep(i,m){
		int p;
		cin >>  p;
		string s;
		cin >>  s;
		--p;
		if( ac[p])continue;
		if( s == "AC"){
			ac[p] = 1 ;
		}else{
			pena[p] ++;
		}
	}
	int AC = 0, PENA = 0 ;
	rep(i,n){
		AC +=ac[i];
		if(ac[i]) PENA += pena[i];
	}
	printf("%d %d\n", AC ,PENA);
}


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

int main(){
    int N, sum=0;
    cin >> N;
    vector<int> a(N, 0), ans(N, 0);

    for(int i=0; i<N; i++){
	cin >> a[i];
	sum+=a[i];
    }
    ans[0]=sum;
    
    for(int i=1; i<N; i+=2){
	ans[0]-=2*a[i];
    }    
    for(int i=0; i<N-1; i++){
	ans[i+1] = 2*a[i]-ans[i];
    }    
    for(int i=0; i<N; i++)
	cout << ans[i] << endl;    
}




Measured memory_bytes: 2048
#include<iostream>
using namespace std;
const int a[] = {1, 1, 1, 2, 1, 2, 1, 5, 2, 2, 1, 5, 1, 2, 1, 14, 1, 5, 1, 5, 2, 2, 1, 15, 2, 2, 5, 4, 1, 4, 1, 51};
int main()
{
    int k;
    cin >> k;
    cout << a[k - 1] << endl;
}


Measured memory_bytes: 256
import java.io.*;
import java.util.*;

class Main {

	public static void main(String[] args) throws Exception {
		final Scanner sc = new Scanner(System.in);
		int N = sc.nextInt();
		long[] a = new long[(3 * N)];
		for (int i = 0; i < 3 * N; i++) {
			a[i] = sc.nextLong();
		}
		solve(N, a);
	}

	static void solve(int N, long[] a) {
		PriorityQueue<Long> first = new PriorityQueue<>();
		PriorityQueue<Long> second = new PriorityQueue<>(Comparator.reverseOrder());
		long fsum = 0;
		long ssum = 0;
		for (int i = 0; i < N; i++) {
			first.offer(a[i]);
			fsum += a[i];
			second.offer(a[3 * N - i - 1]);
			ssum += a[3 * N - i - 1];
		}

		long[] f = new long[N + 1];
		long[] s = new long[N + 1];

		f[0] = fsum;
		s[N] = ssum;
		for (int k = 0; k < N; k++) {
			int l = N + k;
			int r = 2 * N - 1 - k;

			first.offer(a[l]);
			fsum += a[l];
			fsum -= first.poll();
			f[k + 1] = fsum;

			second.offer(a[r]);
			ssum += a[r];
			ssum -= second.poll();
			s[N - 1 - k] = ssum;
		}

		long answer = Long.MIN_VALUE;
		for (int i = 0; i <= N; i++) {
			answer = Math.max(answer, f[i] - s[i]);
		}

		System.out.println(answer);
	}
}


Measured memory_bytes: 99908
import java.math.BigDecimal;
import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		try (Scanner sc = new Scanner(System.in)) {
			int n = Integer.parseInt(sc.nextLine());
			BigDecimal mother = new BigDecimal(0);
			for (int i = 0; i < n; i++) {
				mother = mother.add(new BigDecimal(1).divide(sc.nextBigDecimal(), 8, BigDecimal.ROUND_HALF_DOWN));
			}
			System.out.println(new BigDecimal(1).divide(mother, 8, BigDecimal.ROUND_HALF_DOWN));
		}
	}
}

Measured memory_bytes: 24404
//#include <fsociety>
#include <cmath>
#include <deque>
#include <algorithm>
#include <iterator>
#include <list>
#include <map>
#include <unordered_map>
#include <queue>
#include <set>
#include <unordered_set>
#include <stack>
#include <string>
#include <vector>
#include <fstream>
#include <iostream>
#include <iomanip> 
#include <stdio.h>
//end of libraries ;
// freopen("input.txt","r",stdin);
// freopen("output.txt","w",stdout);
// ll lmax(ll o , ll w) {if(o>w)return o; return w;}
// ll lmin(ll o , ll w) {if(o<w)return o; return w;}
#define LNF 3999999999999999999
#define INF 999999999
#define N 3000003
#define PI 3.14159265359
#define endl "\n"
#define F first
#define S second
#define pb push_back
#define ll long long
#define all(c) (c).begin(),(c).end()
#define sz(c) (int)(c).size()
#define mkp(a,b) make_pair(a,b)
#define fcin ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0);
using namespace std;
int n , b[N+50] , d[N+50] , p[N+50] , ans = 1 , g = 0;
int gcd(int x,int y){
    if(x>y) swap(x,y);
    if(x==0) return y;
    return gcd(y%x , x);
}
int main(){
fcin;
  cin >> n;
  vector<int> a(n);
  for(int i = 0 ; i < n ; i++){
    cin >> a[i];
    g=gcd(a[i],g);
    b[a[i]]++;
  }
      for(int i = 2 ; i <= N ; i++){
         if(p[i]) continue;
         for(int j = i ; j <= N ; j+= i){
            p[j] = 1;
            if(b[j]){
                if(d[i] || b[j]>1) ans = 0;
                d[i] = 1;
            }
         }    
      }
    if(ans) {
        cout << "pairwise coprime";
        return 0;
    }
    else if(g==1) {
        cout << "setwise coprime";
        return 0;
    }
    cout << "not coprime";


}


Measured memory_bytes: 26460
card_qty = gets.chomp.to_i
card_array = gets.chomp.split.map(&:to_i).sort.reverse
card_array << 0 if card_array.count % 2 != 0
big_sum = card_array.each_slice(2).map(&:first).inject(:+)
small_sum = card_array.each_slice(2).map(&:last).inject(:+)
puts "#{big_sum - small_sum}"

Measured memory_bytes: 1788
#!/usr/bin/perl

use strict;
use warnings;

my $n = <STDIN>;
for (my $i=int($n**0.5); $i>=1; $i--)
{
    if ($n%$i==0)
    {
        my $ans = $i+$n/$i-2;
        print "$ans\n";
        exit;
    }
}

Measured memory_bytes: 640
#include <bits/stdc++.h>
using namespace std;
 
int main() {
  int N;
  int K;
  cin >> N >> K;
  int x;
  x = 0;
  for (int i=0; i < N; i++){
    int a;
    cin >> a;
    int b = K - a;
    if (b > a){
    x += 2*a;
    }else{
      x += 2*b;
    }
  }
    cout << x << endl;
}

Measured memory_bytes: 3564
#include <bits/stdc++.h>
#define debug(x) cerr << #x << ": " << x << '\n';
using namespace std;
using ll = long long;
using P = pair<ll, int>;
const ll INF = (ll)1e17;

struct edge{
	int to, cost;
};

vector<edge> G[100010];
ll d[100010];

int main(void){
	int N; cin >> N;
	for(int i = 0; i < N-1; i++){
		int a, b, c; cin >> a >> b >> c;
		a--; b--;
		edge e; e.to = b; e.cost = c; G[a].push_back(e);
		e.to = a; G[b].push_back(e);
	}
	int Q, K; cin >> Q >> K;

	// for(int i = 0; i < N; i++){
	// 	printf("G[%d]: ", i);
	// 	for(auto& v: G[i]){
	// 		printf("%d ", v.to);
	// 	}
	// 	printf("\n");
	// }

	queue<int> que;
	que.push(K-1);
	fill(d, d+N, -1); d[K-1] = 0;

	while(not que.empty()){
		int v = que.front(); que.pop();
		for(int i = 0; i < (int)G[v].size(); i++){
			edge e = G[v][i];
			if(d[e.to] >= 0) continue;

			d[e.to] = d[v] + e.cost;
			que.push(e.to);
		}
	}

	// for(int i = 0; i < N; i++) printf("d[%d] = %lld\n", i, d[i]);

	for(int i = 0; i < Q; i++){
		int x, y; cin >> x >> y;
		x--; y--;
		printf("%lld\n", (ll)d[x] + d[y]);
	}

	return 0;
}


Measured memory_bytes: 8820
#include<iostream>
#include<algorithm>
#include<string>
#include<vector>
#include<queue>
#include<map>
#include<cmath>
using namespace std;
#define REP(i, limit) for(int i=0; i< limit; ++i)
#define LINT long long
const LINT MOD = 1000000007; //nより大きい素数であってくれ

int main(){
  int H, W, K;
  cin>>H>>W>>K;

  LINT dp[H+1][W];
  dp[0][0] = 1;
  REP(i, W-1) dp[0][i+1]=0;
  if(W==1){
    cout<<1<<endl;
    return 0;
  }

  LINT fib[8];
  fib[0]=1; fib[1]=1;
  for(int i=2; i<8; ++i) fib[i]=fib[i-1]+fib[i-2];


  REP(i, H){
      dp[i+1][0] = (dp[i][0]*fib[W-1]%MOD+dp[i][1]*fib[W-2]%MOD)%MOD;
      dp[i+1][W-1] = (dp[i][W-1]*fib[W-1]%MOD+dp[i][W-2]*fib[W-2]%MOD)%MOD;
      for(int j=1; j<W-1; ++j){
        dp[i+1][j] = (dp[i][j-1]*fib[j-1]*fib[W-1-j]%MOD +dp[i][j]*fib[j]*fib[W-1-j]%MOD
                    +dp[i][j+1]*fib[j]*fib[W-2-j]%MOD)%MOD;
      }
  }
  cout<<dp[H][K-1]<<endl;
  //REP(i, W)cout<<dp[H][i]<<" ";




  return 0;
}


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

int main(){
    string S;
    cin >> S;
    char c[S.size()];
    rep(i, S.size()){
        c[i] = S[i];
    }
    sort(c, c+S.size());
    rep(i, S.size()-1){
        if(c[i]==c[i+1]){
            printf("no\n");
            return 0;
        }
    }
    printf("yes\n");
    return 0;
}

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

const int inf=1000000000;

namespace Dinic
{
	int hd[210],cur[210],q[210],l,r,dis[210],cnt,n;
	struct node{int to,next,c;}e[100010];
	void init(int N){n=N,cnt=1,memset(hd,0,sizeof(hd));}
	void addedge(int x,int y,int c)
	{
		e[++cnt]=(node){y,hd[x],c},hd[x]=cnt;
		e[++cnt]=(node){x,hd[y],0},hd[y]=cnt;
	}
	bool bfs()
	{
		q[l=r=1]=1,dis[1]=0;
		for (int i=2; i<=n; i++) dis[i]=-1;
		while (l<=r)
		{
			int x=q[l++];
			for (int i=hd[x]; i; i=e[i].next)
				if (e[i].c&&dis[e[i].to]==-1) dis[q[++r]=e[i].to]=dis[x]+1;
		}
		return dis[n]!=-1;
	}
	int dfs(int x,int f)
	{
		if (x==n) return f;
		for (int &i=cur[x]; i; i=e[i].next) 
			if (e[i].c&&dis[e[i].to]==dis[x]+1)
			{
				int nw=dfs(e[i].to,min(f,e[i].c));
				if (nw) return e[i].c-=nw,e[i^1].c+=nw,nw;
			}
		return 0;
	}
	int solve()
	{
		int ans=0,nw;
		while (bfs())
		{
			for (int i=1; i<=n; i++) cur[i]=hd[i];
			while (nw=dfs(1,inf)) ans+=nw;
		}
		return ans;
	}
}

int n,m,a[110][110],col[110][110],po[110][110],b[110][110];

int main()
{
	scanf("%d%d",&n,&m);
	for (int i=1; i<=n; i++)
		for (int j=1; j<=m; j++) 
		{
			scanf("%d",&a[i][j]);
			col[i][j]=(a[i][j]-1)/m+1;
		}
	for (int i=1; i<=m; i++)
	{
		Dinic::init(2*n+2);
		for (int j=1; j<=n; j++)
		{
			Dinic::addedge(1,j+1,1),Dinic::addedge(j+n+1,2*n+2,1);
			for (int k=1; k<=n; k++) po[j][k]=0;
			for (int k=i; k<=m; k++) po[j][col[j][k]]=k;
			for (int k=1; k<=n; k++)
				if (po[j][k]) Dinic::addedge(j+1,k+n+1,1);
		}
		Dinic::solve();
		for (int j=1; j<=n; j++)
			for (int y,k=Dinic::hd[j+1]; k; k=Dinic::e[k].next)
				if (Dinic::e[k].to>n+1&&!Dinic::e[k].c) 
				{
					y=Dinic::e[k].to-n-1;
					if (po[j][y]!=i) swap(a[j][i],a[j][po[j][y]]),swap(col[j][i],col[j][po[j][y]]);
					break; 
				}
	}
	for (int i=1; i<=n; i++,puts(""))
		for (int j=1; j<=m; j++) printf("%d ",a[i][j]),b[col[i][j]][j]=a[i][j];
	for (int i=1; i<=n; i++,puts(""))
		for (int j=1; j<=m; j++) printf("%d ",b[i][j]);
	return 0;
}

Measured memory_bytes: 768
import math
l=[]
while True:
  n,x=map(int,input().split())
  if (n,x)==(0,0):
    break
  l.append((n,x))
for (n,x) in l:
  ans=0
  a_min=int(math.ceil(x/3))+1
  for a in range(n,a_min-1,-1):
    b_min=((x-a)//2)+1
    for b in range(a-1,b_min-1,-1):
      if x-a-b<=0:
        continue
      ans+=1
  print(ans)


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

int main() {
  int X, Y;
  cin >> X >> Y;
  bool finished = false;
    
  for (int c=0; c<51; c++){
    for (int t=0; t<26; t++){
      if (2*c+4*t==Y && c+t==X){
        finished = true;
        break;
      }
    }
    if (finished) {break;}
  }
 if (finished){
   cout << "Yes" << endl;
 }
  else {cout << "No" << endl;}
}


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

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

    vector<vector<int>> S(N, vector<int>(2));
    vector<vector<int>> C(M, vector<int>(2));

    for(int i{0}; i<N; ++i){
        cin >> S[i][0];
        cin >> S[i][1];
    }

    for(int i{0}; i<M; ++i){
        cin >> C[i][0];
        cin >> C[i][1];
    }

    for(int i{0}; i<N; ++i){
        int d_min;
        int min_j;
        for(int j{0}; j<M; ++j){
            if(j==0){
                d_min = abs(S[i][0]-C[j][0]) + abs(S[i][1]-C[j][1]);
                min_j = j;
            }
            if(abs(S[i][0]-C[j][0]) + abs(S[i][1]-C[j][1])<d_min){
                d_min = abs(S[i][0]-C[j][0]) + abs(S[i][1]-C[j][1]);
                min_j = j;
            }
        }
        cout << min_j+1 << endl;
    }
}


Measured memory_bytes: 256
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import accumulate, permutations, combinations, product, groupby
from operator import itemgetter, mul
from copy import deepcopy, copy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left
from fractions import gcd
from heapq import heappush, heappop
from functools import reduce
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def ZIP(n): return zip(*(MAP() for _ in range(n)))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7

def dist(p1, p2):
	return sqrt((p1[0]-p2[0])**2 + (p1[1]-p2[1])**2)

N = INT()
xy = [LIST() for _ in range(N)]

distance = 0
cnt = 0

for p in permutations(xy, N):
	cnt += 1
	for i in range(N-1):
		distance += dist(p[i], p[i+1])

print(distance/cnt)

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

#define repd(i, a, b) for (ll i = (a); i < (b); i++)
#define repb(i, n) for (ll i = (n)-1; i >= 0; i--)
#define rep(i, n) repd(i, 0, n)

using namespace std;

using ll = long long;
using ul = unsigned long long;
ll mod = 1000000007;

int main() {
source:
    double a, b, h, m;
    cin >> a >> b >> h >> m;
    h += m / 60;
    static const double pi = 3.141592653589793;
    double theta = pi / 30 * abs(5 * h - m);
    double ans_temp = a * a + b * b - 2 * a * b * cos(theta);
    double ans = sqrt(ans_temp);
    printf("%.10f\n", ans);
    return 0;
}



Measured memory_bytes: 4288
//header{{{
#pragma GCC optimize("Ofast")
#include<bits/stdc++.h>
#include<boost/multiprecision/cpp_int.hpp>
#include<boost/math/common_factor_rt.hpp>
  
using namespace std;
using namespace boost::multiprecision;  
using namespace boost::math;

#define rep(i,n) for(int i=0;i<(n);++i)
#define reps(i,n) for(int i=1;i<=(n);++i)
#define all(x) (x).begin(),(x).end()
#define Fixed fixed << setprecision(12)
//#define int int64_t
using pii = pair<int,int>;
constexpr int INF  = 0x3f3f3f3f;
constexpr long long LINF = 0x3f3f3f3f3f3f3f3fLL;
constexpr int mod1 = 1e9+7; 
constexpr int mod2 = 998244353;

template <class A, class B> inline bool chmax(A &a, const B &b) { return b > a && (a = b, true); }
template <class A, class B> inline bool chmin(A &a, const B &b) { return b < a && (a = b, true); }

template <class T> using min_heap = priority_queue<T,vector<T>,greater<T> >;
template <class T> using max_heap = priority_queue<T>;
template <class A, class B> using umap = unordered_map<A,B>;
  
inline int square(int a){ return a * a;}
inline int updiv(int a,int b){ return (a + b - 1) / b; }

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

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

    vector<int> a(3);
    rep(i,3){
        cin >> a[i];
    }

    sort(all(a));

    if((a[0] == a[1] && a[0] != a[2]) || (a[1] == a[2] && a[0] != a[1])){
        cout << "Yes\n";
    }else{
        cout << "No\n";
    }



    return 0;
}


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

long long gcdExtended(long long a, long long b, long long *x, long long *y); 

long long modInverse(long long b, long long m){
    long long x, y;
    long long g = gcdExtended(b, m, &x, &y);
    if(g != 1){
        return -1;
    }
    return (x%m+m)%m;
}

void modDivide(long long a, long long b, long long m){
    a = a % m; 
    long long inv = modInverse(b,m);
    cout << (inv*a)%m;
}
 
long long gcdExtended(long long a, long long b, long long *x, long long *y){
    if(a == 0){
        *x = 0, *y = 1;
        return b;
    }
    long long x1, y1;
    long long gcd = gcdExtended(b%a, a, &x1, &y1);
    *x = y1 - (b/a) * x1;
    *y = x1;
    return gcd;
}

int main(){
    long long x,y;
    cin >> x >> y;
    long long fact[1000001];
    fact[0] = 0;
    fact[1] = 1;
    for(int i = 2 ; i < 1000001 ; i += 1){
        fact[i] = fact[i-1]*i;
        fact[i] %= 1000000007;
    }
    for(long long i = 0 ; i <= x ; i += 1){
        if((x-i)%2 == 0){
            if((x-i)/2+2*i == y){
                if(x-i == 0 || i == 0){
                    cout << 1;
                    return 0;
                }
                modDivide(fact[(x-i)/2+i], (fact[(x-i)/2]*fact[i])%1000000007, 1000000007);
                return 0;
            }
        }
    }
    cout << 0;
}

Measured memory_bytes: 8064
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,copy,functools

sys.setrecursionlimit(10**7)
inf = 10**20
eps = 1.0 / 10**10
mod = 998244353
dd = [(0,-1),(1,0),(0,1),(-1,0)]
ddn = [(0,-1),(1,-1),(1,0),(1,1),(0,1),(-1,-1),(-1,0),(-1,1)]

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()
def pf(s): return print(s, flush=True)


def main():
    rr = []
    ii = [2**i for i in range(6)]

    while True:
        n = I()
        if n == 0:
            break
        a = [S() for _ in range(n)]
        d = 0
        s = [[]]
        for c in a:
            l = len(c)
            while l <= d:
                if s[-1][0] == '+':
                    k = 0
                    for kc in s[-1][1:]:
                        k += kc
                else:
                    k = 1
                    for kc in s[-1][1:]:
                        k *= kc
                s = s[:-1]
                s[-1].append(k)
                d -= 1

            t = c[-1]
            if t == '+' or t == '*':
                d += 1
                s.append([t])
            else:
                s[-1].append(int(t))

        while len(s) > 1:
            if s[-1][0] == '+':
                k = 0
                for kc in s[-1][1:]:
                    k += kc
            else:
                k = 1
                for kc in s[-1][1:]:
                    k *= kc
            s = s[:-1]
            s[-1].append(k)

        rr.append(s[0][0])


    return '\n'.join(map(str, rr))


print(main())




Measured memory_bytes: 9048