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

#define ff              first
#define ss              second
#define int             long long
#define pb              push_back
#define mp              make_pair
#define pii             pair<int,int>
#define vi              vector<int>
#define mii             map<int,int>
#define pq              priority_queue<int>
#define pqs             priority_queue<int,vi,greater<int> >
#define setbits(x)      __builtin_popcountll(x)
#define zrobits(x)      __builtin_ctzll(x)
#define mod             1000000007
#define inf             1e18
#define ps(x,y)         fixed<<setprecision(y)<<x
#define mk(arr,n,type)  type *arr=new type[n];
#define w(x)            int x; cin>>x; while(x--)
mt19937                 rng(chrono::steady_clock::now().time_since_epoch().count());

typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> pbds;


void c_p_c()
{
	ios_base::sync_with_stdio(0); 
	cin.tie(0);
	cout.tie(0);
/*
#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif
*/
}

int32_t main()
{
	c_p_c();
	int n,m;
	cin>>n>>m;
	int arr[m];
	int sum=0;
	for(int i=0;i<m;i++){
		cin>>arr[i];
		sum+=arr[i];
	}
	if(sum>n)
	cout<<-1<<endl;
	else
	cout<<(n-sum)<<endl;
	
	return 0;
}


Measured memory_bytes: 3736
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
const int maxn = 1e6 + 7;

int main(){
    char s[maxn];
    int lt = 2e5;
    scanf("%s", s + lt);
    int l = lt - 1;
    int r = strlen(s + lt) + lt;
    int q; scanf("%d", &q);
    int flag = 0;
    while(q--){
        int t; scanf("%d", &t);
        if(t == 1){
            if(flag == 0) flag = 1;
            else flag = 0;
        }
        else{
            int f; scanf("%d", &f);
            char c[2]; scanf("%s", c);
            if(f == 1){
                if(flag == 1){
                    s[r] = c[0];
                    r++;
                }
                else{
                    s[l] = c[0];
                    l--;
                }
            }
            else{
                if(flag == 0){
                    s[r] = c[0];
                    r++;
                }
                else{
                    s[l] = c[0];
                    l--;
                }
            }
        }
    }
    if(flag == 0){
        for(int i = l + 1; i < r; ++i){
            printf("%c", s[i]);
        }
    }
    else{
        for(int i = r - 1; i > l; --i)
            printf("%c", s[i]);
    }
    printf("\n");
    return 0;
}


Measured memory_bytes: 896
// @snippet<sh19910711/contest:headers.cpp>
#include <iostream>
#include <sstream>
#include <fstream>
#include <iomanip>
#include <vector>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <algorithm>
#include <numeric>
#include <limits>
#include <complex>
#include <functional>
#include <iterator>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>

// @snippet<sh19910711/contest:solution/typedef.cpp>
namespace solution {
    typedef std::istringstream ISS;
    typedef std::ostringstream OSS;
    typedef std::vector<std::string> VS;
    typedef long long LL;
    typedef int INT;
    typedef std::vector<INT> VI;
    typedef std::vector<VI> VVI;
    typedef std::pair<INT,INT> II;
    typedef std::vector<II> VII;
}

// @snippet<sh19910711/contest:solution/interface.cpp>
namespace solution {
    class ISolution {
    public:
        virtual void init() {};
        virtual bool input() { return false; };
        virtual void output() {};
        virtual int run() = 0;
    };
}

// @snippet<sh19910711/contest:solution/solution.cpp>
namespace solution {
    using namespace std;

    const int SIZE = 100 + 11;
    const int NONE = -1;

    int n;
    int X[SIZE][SIZE];
    int Y[SIZE][SIZE];
    bool V[SIZE][SIZE];
    bool filled[SIZE][SIZE];

    void check_init() {
        for ( int i = 0; i < SIZE; ++ i )
            for ( int j = 0; j < SIZE; ++ j )
                V[i][j] = false;
    }

    bool check( int x, int y, int sx, int sy ) {
        if ( V[x][y] )
            return x == sx && y == sy;
        V[x][y] = true;
        int nx = X[x][y];
        int ny = Y[x][y];
        return check(nx, ny, sx, sy);
    }

    void fill( int x, int y ) {
        if ( filled[x][y] )
            return;
        filled[x][y] = true;
        int nx = X[x][y];
        int ny = Y[x][y];
        fill(nx, ny);
    }

    int solve() {
        int res = 0;
        for ( int y = 0; y < n; ++ y ) {
            for ( int x = 0; x < n; ++ x ) {
                check_init();
                if ( ! filled[x][y] && check(x, y, x, y) ) {
                    res ++;
                    fill(x, y);
                }
            }
        }
        return res;
    }
    
    class Solution: public ISolution {
    public:

        void init() {
            for ( int i = 0; i < SIZE; ++ i )
                for ( int j = 0; j < SIZE; ++ j )
                    filled[i][j] = false;
        }
        
        bool input() {
            if ( ! ( cin >> n ) )
                return false;
            if ( n == 0 )
                return false;
            for ( int y = 0; y < n; ++ y ) {
                for ( int x = 0; x < n; ++ x ) {
                    cin >> X[x][y] >> Y[x][y];
                }
            }
            return true;
        }

        void output( int result ) {
            cout << result << endl;
        }
        
        int run() {
            while ( init(), input() ) {
                output(solve());
            }
            return 0;
        }
        
    };
}

// @snippet<sh19910711/contest:main.cpp>
int main() {
    return solution::Solution().run();
}

Measured memory_bytes: 1280
program main
  
  implicit none
  integer(8) :: n
  integer(8) :: i,j,k,l,m,ans
  integer(8), allocatable :: mm(:), ll(:)
  integer(8) :: tmp, summ
  
  read(*,*) n, k
  allocate( mm(k:n+1) )
  allocate( ll(k:n+1) )
  
  tmp = 0
  do j = 0, k - 2 
    tmp = tmp + j
  end do
  summ = tmp
  do j = k, n + 1  
   summ = summ + j - 1
    mm(j) = mm(j) + summ
  end do

  tmp = 0
  do j = n-k+1, n  
    tmp = tmp + j
  end do
  summ = tmp
  tmp  = n-k+1
  do j = k, n+1  
   ll(j) = summ
    tmp  = tmp - 1
    summ = summ + tmp
  end do

  ans = 0 
  do i = k, n + 1

    !m = 0
    !do j = 0, i - 1  
    !  m = m + j
    !end do
    !l = 0
    !do j = n-i+1, n 
    !  l = l + j
    !end do

    ans = ans + ll(i) - mm(i) + 1
    !ans = ans + l - mm(i) + 1

  end do
  print*, mod( ans, 10**9+7 ) 
  
end program main



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

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

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

    sort(a,a+n);

    int tt = 0;

    for(int i = 0; i < n-2; i++)
            for(int j = i+1; j < n-1; j++)
                if(a[i] != a[j])
                    for(int k = j+1; k < n; k++){
                        if(a[k] != a[j])
                            if(a[i]+a[j] > a[k]) tt++;
                    }
    cout << tt;

    return 0;
}


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

typedef long l;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<int, string> pis;
typedef pair<long, long> pll;

#define EACH(t, c) for (auto&& t : c)
#define ALL(c) begin(c), end(c)
#define FOR(i, s, e) for (int i = (s); i < (int)(e); ++i)
#define REP(i, n) FOR(i, 0, n)
#define endl '\n'
#define F first
#define S second
#define pb push_back
#define mp make_pair

const int DX[] = {1, 0, -1, 0, 1, 1, -1, -1}, DY[] = {0, 1, 0, -1, 1, -1, 1, -1};
const int MOD = (int)1e9 + 7;
const int INF = 999999999;

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

  cin >> a >> b;
  cout << max(max(a + b, a - b), a * b) << endl;

}



Measured memory_bytes: 384
N,X = [int(i) for i in input().split()]
L = [int(i) for i in input().split()]

if N == 1:
    print(abs(L[0]-X))
else:
    P = [0]*N
    P[0] = abs(min(L) - X)
    for i in range(1,N):
        P[i] = abs(L[i]-L[i-1])

    def gcd( x, y ):
        while y != 0:
            x, y = y, x%y
        return x

    ans = gcd(P[0],P[1])

    for i in range(2,N):
        ans = gcd(ans,P[i])

    print(ans)


Measured memory_bytes: 14252
N,M = map(int,input().split())

ans = 0
i_list = []

for i in range(N):
	tmp_list = list(map(int,input().split()))
	i_list.extend(tmp_list[1:])

for i in range(1,M+1):
  if i_list.count(i) == N:
    ans += 1

print(ans)

Measured memory_bytes: 3060
#include<iostream>
#include<cmath>
#include<numeric>
#include<functional>
#include<string>
#include<algorithm>
#include<vector>
#include<map>
#include<iomanip>
#define rep(i,n) for(int i=0;i<n;i++)
#define rep1(i,n) for(int i=1;i<n;i++)
#define ll long long
#define modbig (10*10*10*10*10*10*10*10*10+7)
using namespace std;

int main(){
  int x,y,z;
  cin>>x>>y>>z;
  cout<<z<<" "<<x<<" "<<y;
}

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

#include <iostream>
#include <iomanip>
#include <sstream>
#include <stdint.h>
#include <string.h>

#define _USE_MATH_DEFINES
#include <math.h>

#include <vector>
#include <list>
#include <set>
#include <map>
#include <unordered_map>
#include <unordered_set>
#include <queue>
#include <stack>
#include <string>

#include <algorithm>
#include <functional>
#include <bitset>
#include <functional>
#include <chrono>
#include <random>

#define sqr(x) (x) * (x)

typedef unsigned int u32;
typedef int i32;
typedef unsigned long long int u64;
typedef long long int i64;
typedef uint16_t u16;
typedef int16_t i16;
typedef uint8_t u8;
typedef int8_t i8;

using namespace std;
using namespace std::chrono;

const i64 mod = 1000000007ll;
const i64 smod = 998244353ll;
const i64 inf = 10000000000000007ll;

const double eps = 1e-10;

// del

const i64 MAXN = 300005;

i64 n;

i64 a[MAXN];

int main() {
    ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); cout.precision(15); cout.setf(ios::fixed);


    i64 a;
    cin >> a;

    for (i64 i = 1; i <= 9; i++) {
        for (i64 j = 1; j <= 9; j++) {
            if (i * j == a) {
                cout << "Yes" << endl;
                return 0;
            }
        }
    }

    cout << "No" << endl;

    return 0;
}

Measured memory_bytes: 256
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
//--<for debugging>
#define whatis(x) cout << #x << " is " << x<<" ";
#define whatisl(x) cout << #x << " is " << x<<"\n";
#define parr(array,end)for(ll loop=0;loop<end;loop++)cout<<array[loop]<<" ";cout<<"\n";
#define lline cout<<"\n";
#define errorl(args...) { string _s = #args; replace(_s.begin(), _s.end(), ',', ' '); stringstream _ss(_s); istream_iterator<string> _it(_ss); err(_it, args);cout<<"\n";}
#define error(args...) { string _s = #args; replace(_s.begin(), _s.end(), ',', ' '); stringstream _ss(_s); istream_iterator<string> _it(_ss); err(_it, args);cout<<"___";}
    
void err(istream_iterator<string> it) {}
template<typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
    cout << *it << " = " << a <<" , ";
    err(++it, args...);
}
//--</for debugging> 
#define rep(i, begin, end) for (__typeof(end) i = (begin) - ((begin) > (end)); i != (end) - ((begin) > (end)); i += 1 - 2 * ((begin) > (end)))
#define testcase ll tt; cin >> tt; while(tt--)
#define boost ios_base::sync_with_stdio(false);cin.tie(NULL);
#define pb push_back 
#define vars ll h,i,j,k,d,l,p,q,r,x,y,a,b,c,v,var,n,m,z,s,ans,ind1,ind2,flag,limit1,limit2,limit3,mod;
#define vll vector<ll>
#define pll pair<ll,ll>
#define sll set<ll>
#define pint pair<int,int>
const ll INF=1e9+7;
const ll MOD=1e9+7;
int main()
{
    boost vars
    cin>>n>>m;
    s=0;
    for(i=0;i<m;i++)
    {
        cin>>v;
        s+=v;
    }
    cout<<max(-1ll,n-s);
}

Measured memory_bytes: 3656
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <cmath>
#include <algorithm>
#include <vector>
#include <numeric>
#include <queue>
#include <stack>
#include <map> 
#include <set>
#include <string>
#include <functional>
#include <list>
#include <random>
#include <time.h>
#include <iomanip>
#include <assert.h>
#include <numeric>
#include <new>
#include <sstream>
#include <complex>
#define BIT(nr) (1ULL << (nr))
#define int long long
#define ll long long
#define double long double
#define mod 1000000007
#define MAXN (int)1e+5 * 2+1
#define LL_MAX 9223372036854775807	
#define LL_HALFMAX 9223372036854775807 / 2	
#define MIN -(9223372036854775807 / 2)
#define REP(i,a,n) for(int i=(a); i<(int)(n); i++)
#define rep(i,n) REP(i,0,n)
#define FOR(it,c) for(__typeof((c).begin()) it=(c).begin(); it!=(c).end(); ++it)
#define ALLOF(c) (c).begin(), (c).end()
#define REPS(i,x) for(int i=1;i<=(int)(x);i++)
#define RREP(i,x) for(int i=((int)(x)-1);i>=0;i--)
#define RREPS(i,x) for(int i=((int)(x));i>0;i--)
#define repl(i,a,b) for(int i=(int)(a);i<(int)(b);i++)
#define mp make_pair
template<typename T1, typename T2> inline void chmin(T1& a, T2 b) { if (a > b) a = b; }
template<typename T1, typename T2> inline void chmax(T1& a, T2 b) { if (a < b) a = b; }

using namespace std;

using Weight = int;
using Flow = int;
struct Edge {
	int src, dst;

	// libalgo のものに追加、メンバを追加するだけなので互換性は崩さないはず、逆辺のG[e.dstの]インデックスを保持
	int rev;
	Weight weight;
	Flow cap;
	Edge() : src(0), dst(0), weight(0) {}
	Edge(int s, int d, Weight w) : src(s), dst(d), weight(w) {}
	bool operator<(const Edge& right) const {
		return dst < right.dst;
	}
};

using Edges = std::vector<Edge>;
using Graph = std::vector<Edges>;
using Array = std::vector<Weight>;
using Matrix = std::vector<Array>;

void add_edge(Graph& g, int a, int b, Weight w = 1) {
	g[a].emplace_back(a, b, w);
	g[b].emplace_back(b, a, w);
}
void add_arc(Graph& g, int a, int b, Weight w = 1) { g[a].emplace_back(a, b, w); }

int dx[4] = { 0, 1, 0, -1 }; // x軸方向への変位
int dy[4] = { 1, 0, -1, 0 }; // y軸方向への変位

signed main() {
	int V, E;
	cin >> V >> E;
	Graph G(V);
	
	vector<vector<int>> dist(V, vector<int>(V, LL_HALFMAX));

	rep(i, E) {
		int s, t, d;
		cin >> s >> t >> d;
		add_arc(G, s, t, d);
		dist[s][t] = d;
	}

	rep(i, V) {
		dist[i][i] = 0;
	}

	// ワーシャルフロイド
	rep(k, V) {
		rep(i, V) {
			rep(j, V) {
				chmin(dist[i][j], dist[i][k] + dist[k][j]);
			}
		}
	}

	// 2回目で更新があったらNEGCYCLE
	bool negs = false;
	rep(k, V) {
		rep(i, V) {
			rep(j, V) {
				if (dist[i][j] > dist[i][k] + dist[k][j]) {
					negs = true;
					chmin(dist[i][j], dist[i][k] + dist[k][j]);
				}
			}
		}
	}

	if (negs) {
		cout << "NEGATIVE CYCLE\n";
	}
	else {
		rep(i, V) {
			rep(j, V) {
				if (dist[i][j] > 2 * 100000000000) {
					cout << "INF";
				}
				else {
					cout << dist[i][j];
				}
				if (j != V - 1) {
					cout << " ";
				}
			}
			cout << "\n";
		}
	}

	return 0;
}


Measured memory_bytes: 3340
using System;

namespace abc142
{
    class Program
    {
        static void Main(string[] args)
        {
            double N = double.Parse(Console.ReadLine());
            double ans;
            if(N == 1){
                ans = 1;
            }else if(N%2 == 0){
                ans = 0.5;
            }else{
                double cnt = 0;
                for(int i=1; i<=N;i++){
                    if(i%2==1){
                        cnt++;
                    }
                }
                ans = cnt/N;
            }
            Console.WriteLine(ans);
        }
    }
}


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

#define int long long
#define pb push_back
#define ins insert
#define pii pair<int,int>
#define fs first
#define sc second
#define mpr make_pair
#define iter iterator
#define all(X) X.begin(),X.end()
#define sau(X) sort(all(X)),X.resize(unique(all(X))-X.begin())
#define exist(X,ele) binary_search(all(X),ele)

template<typename T1,typename T2>
ostream& operator << (ostream& out,const pair<T1,T2>& X)
{
	out<<"{"<<X.fs<<","<<X.sc<<"}";
	return out;
}

template<typename T>
ostream& operator << (ostream& out,const vector<T>& X)
{
	for(auto i:X) out<<i<<" ";
	return out;
}

inline void cmin(int& x,int y)
{
	x=min(x,y);
}

inline void cmax(int& x,int y)
{
	x=max(x,y);
}

const int inf=5000000000000000ll;

main()
{
	ios::sync_with_stdio(false);
	
	int N; cin>>N;
	vector<int> a(N);
	for(int i=0;i<N;i++) cin>>a[i];
	int mina=inf,cnt=0;
	for(int i=0;i<N;i++)
	{
		if(a[i]<=mina) cnt++;
		cmin(mina,a[i]);
	}
	cout<<cnt<<endl;
	
	return 0;
}






















Measured memory_bytes: 1792
//ITP1_8 Ring

#include<iostream>
#include<string.h>
#include<stdio.h>

using namespace std;

int main(){
	string s, ss, p;
	cin >> s;
	cin >> p;
	ss = s + s;  //文字列の連結
//	cout << s.length();
	int flag;
	for(int i = 0; i < s.length(); i++){  //文字列sのどの位置から
		flag = 1;
//		cout << "i:" << i << endl;
		for(int j = 0; j < p.length(); j++){
//			cout << "j:" << j << endl;
//			cout << "ss:" << ss[i+j] << endl;
//			cout << "p:" << p[j] << endl;
			if(ss[i + j] != p[j]){
//				cout << "!!!" << endl;
				flag = 0;
				break;
			}
		}
		if(flag == 1) break;
	}
	if(flag == 1) cout << "Yes" << endl;
	else cout << "No" << endl;
	return 0;
}

Measured memory_bytes: 1188
def f(i):
  if i%2==0:
    return i//2
  else:
    return i*3+1
 
a=[]
a.append(int(input()))
i=1
while True:
  b=f(a[-1])
  i+=1
  if b in a:
    print(i)
    break
  a.append(b)

Measured memory_bytes: 3060
// -> limitless <- //
#include <bits/stdc++.h>
#define IO ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0);
#define PB push_back
#define MP make_pair
using namespace std;
typedef  long long ll;
    ll gcd(ll a, ll b){
    if(b == 0)return a;
    gcd(b,a%b);
    }
int main()
{
  ll n;
  cin >> n;
  int mx = -1 , mn =1e9+1;
    for(int i = 0; i<n ;i++ ){
        int a;
        cin >> a;
        mx = max(mx, a);
        mn = min(mn, a);
    }
    cout << mx-mn <<endl;
    return 0;
}





Measured memory_bytes: 256
import math
x1, y1, x2, y2 = map(int, input().split())
l = ((x1 - x2)**2 + (y1 - y2)**2)**0.5
theta = math.atan2(y2 - y1, x2 - x1)
theta_rev = theta + 0.5 * math.pi
l_cos_theta_rev = l * math.cos(theta_rev)
l_sin_theta_rev = l * math.sin(theta_rev)

x3 = round(x2 + l_cos_theta_rev)
y3 = round(y2 + l_sin_theta_rev)
x4 = round(x1 + l_cos_theta_rev)
y4 = round(y1 + l_sin_theta_rev)

print(x3,y3,x4,y4)

Measured memory_bytes: 3316
#include <bits/stdc++.h> //C++の標準ライブラリを一行で一括でインクルードする
#include <math.h> //数学関数と数学定数を利用する
#define rep(i,n) for (int i = 0; i < (n); i++)
using namespace std;
typedef long long ll;
template<class T> void chmax(T &a,T b) { if (a<b) a=b;}
template<class T> void chmin(T &a,T b) { if (a>b) a=b;}

bool ans;
int n,m;
vector<vector<int>> g;

void dfs(int pos, int time){
  if(pos == n){
    ans = true;
    return;
  }
  if(time == 2){
    return;
  }
  
  for(auto next_pos:g[pos]){
    dfs(next_pos,time+1);
  }

}

int main() {
  cin >> n >> m;
  g.resize(n+1);
  ans = false;
  
  rep(i,m){
    int a, b;
    cin >> a >> b;
    g[a].push_back(b);
  }
  
  dfs(1,0);
  
  if(ans) cout << "POSSIBLE";
  else cout << "IMPOSSIBLE";

  return 0;
}


Measured memory_bytes: 8576
primes=[]
def getprime():
    for i in range(2,10000):
        ok=True
        for p in primes:
            if i%p==0:
                ok=False
        if ok:
            primes.append(i)
 
def check(n):
    for p in primes:
        if n<=p:
            return True
        if n%p==0:
            return False
    return True
 
getprime()
n=int(input())
ns=[]
ans=0
for i in range(n):
    a=int(input())
    if check(a):
        ans+=1
print(ans)


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

int main() {
    int n;
    long long m;
    cin >> n >> m;
    vector<int> a(n);
    for (int i = 0; i < n; i++) cin >> a[i];
    sort(a.rbegin(), a.rend());
    int lb = -1, ub = 2e5+1;
    while (ub - lb > 1) {
        int t = (lb + ub) / 2;
        long long count = 0;
        for (int i = 0; i < n; i++)
            count += a.rend() - lower_bound(a.rbegin(), a.rend(), t-a[i]);
        if (count < m) ub = t;
        else lb = t;
    }
    vector<long long> s(n+1, 0);
    for (int i = 0; i < n; i++) s[i+1] = s[i] + a[i];
    long long count = 0, ans = 0;
    for (int i = 0; i < n; i++) {
        int j = a.rend() - lower_bound(a.rbegin(), a.rend(), ub-a[i]);
        ans += (long long)a[i] * j + s[j];
        count += j;
    }
    ans += lb * (m - count);
    cout << ans << endl;
}


Measured memory_bytes: 1408
#include <iostream>
#include <map>
#include <string>

using namespace std;


int main() {
    int N;
    cin >> N;
    map<char, int> mp;
    map<char, int> pre_mp;
    for(int n=0; n<N; n++) {
        string str;
        cin >> str;

        if(n==0) {
            for(int i=0; i<str.size(); i++) {
                mp[str[i]]++;
            }
            
            continue;
        }
        
        pre_mp.clear();
        for(auto i=mp.begin(); i!=mp.end(); i++) {
            pre_mp[i->first]=i->second;
        }
        
        mp.clear();
        for(int i=0; i<str.size(); i++) {
            if(pre_mp.count(str[i]) && pre_mp[str[i]]>mp[str[i]]) {
                mp[str[i]]++;
            }
        }
    }
    
    for(auto i=mp.begin(); i!=mp.end(); i++) {
        for(int j=0; j<i->second; j++) {
            cout << i->first;
        }
    }
    cout << endl;

    return 0;
    
}

Measured memory_bytes: 256
#include<iostream>
using namespace std;
long mod=1e9+7,dp[51][51][2505];
int N,K;
// https://jeonggyun.tistory.com/243
main()
{
	cin>>N>>K;
    if (K%2==1) {
      cout << 0 << '\n';
      return 0;
    }
	dp[0][0][0]=1;
	for(int i=0;i<N;i++)for(int j=0;j<=i;j++)
	{
		for(int k=0;k<=K-2*j+2;k++)
		{
			if(dp[i][j][k]==0)continue;
			(dp[i+1][j][k+2*j]+=dp[i][j][k]*(2*j+1)%mod)%=mod;
			if(j>0)(dp[i+1][j-1][k+2*(j-1)]+=dp[i][j][k]*j*j%mod)%=mod;
			(dp[i+1][j+1][k+2*(j+1)]+=dp[i][j][k])%=mod;
		}
	}
	cout<<dp[N][0][K]<<endl;
}

Measured memory_bytes: 15096
n, A, B, C = map(int, input().split())
l = [int(input()) for _ in range(n)]
inf = float("inf")
def dfs(cur, a, b, c):
    if cur == n:
        return abs(a-A) + abs(b-B) + abs(c-C) - 30 if min(a, b, c) > 0 else inf
    ret0 = dfs(cur+1, a, b, c)
    ret1 = dfs(cur+1, a+l[cur], b, c) + 10
    ret2 = dfs(cur+1, a, b+l[cur], c) + 10
    ret3 = dfs(cur+1, a, b, c+l[cur]) + 10
    return min(ret0, ret1, ret2, ret3)

print(dfs(0, 0, 0, 0))


Measured memory_bytes: 3064
n, a = [int(i) for i in input().split()]
x = [int(i) for i in input().split()]

max_x = max(x+[a])

y = [0]
for x in x:
    y.append(x-a)

dp = [[0]*(2*n*max_x+1) for i in range(n+1)]
dp[0][n+max_x] = 1

for j in range(n+1):
    for t in range(2*n*max_x+1):
        if j == 0 and t == n*max_x:
            dp[j][t] = 1
        elif j >= 1 and (t-y[j] < 0 or t-y[j] > 2*n*max_x):
            dp[j][t] = dp[j-1][t]
        elif j >= 1 and 0 <= t-y[j] <= 2*n*max_x:
            dp[j][t] = dp[j-1][t] + dp[j-1][t-y[j]]
        else:
            dp[j][t] = 0

print(dp[n][n*max_x]-1)

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

int main() {
  int N;
  cin >> N;
  int ans = 0;
  for (int i = 0; i < N; i++){
    int l, r;
    cin >> l >> r;
    ans+= r - l + 1;
  }
  cout << ans << endl;
}

Measured memory_bytes: 3616
#include <iostream>
#include <iomanip>
#include <cmath>
#include <cstdio>
#include <algorithm>
#include <vector>
#include <memory>
#include <cstring>
#include <cassert>
#include <numeric>
#include <sstream>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <cctype>
#include <unordered_map>
#include <unordered_set>
using namespace std;

#define REP2(i, m, n) for (int i = (int)(m); i < (int)(n); i++)
#define REP(i, n) REP2(i, 0, n)
#define ALL(S) (S).begin(), (S).end()
typedef long long ll;

class UnionFind{
  int num_components;
  std::vector<int> parent;
  std::vector<int> weight;
  std::vector<int> rank;
public:
  UnionFind(int N) : num_components(N),
                     parent(std::vector<int>(N)),
                     weight(std::vector<int>(N, 1)),
                     rank(std::vector<int>(N, 0)){
    for(int i = 0; i < N; i++) parent[i] = i;
  }
  
  int find(int x){
    if(x == parent[x]) return x;
    else return parent[x] = find(parent[x]);
  }
  
  int size(int x){
    return weight[find(x)];
  }

  
  bool same(int x, int y){
    return find(x) == find(y);
  }
    
  void unite(int x, int y){
    x = find(x);
    y = find(y);
    if(x == y) return;
    
    num_components--;
    if(rank[x] < rank[y]){
      weight[y] += weight[x];
      parent[x] = y;
    }else{
      weight[x] += weight[y];
      parent[y] = x;
      if(rank[x] == rank[y]) rank[y]++;
    }
  }
  
  int count(){
    return num_components;
  }
};


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

  int n, m;
  while (cin >> n >> m && n + m){
    vector<tuple<int, int, int> > es;
    
    REP(i, m){
      int s, t, c;
      cin >> s >> t >> c;
      es.push_back(make_tuple(s - 1, t - 1, c));
    }
    
    int ub = 1010;
    int lb = -1;

    while (ub - lb > 1){
      int mb   = (lb + ub) >> 1;
      int cost = 0;

      UnionFind uf(n);

      REP(i, m) if (get<2>(es[i]) <= mb){
        uf.unite(get<0>(es[i]), get<1>(es[i]));
      }
      
      REP(i, m) if (get<2>(es[i]) > mb && !uf.same(get<0>(es[i]), get<1>(es[i]))){
          uf.unite(get<0>(es[i]), get<1>(es[i]));
        cost++;
      }

      if (cost * 2 > n - 1){
        lb = mb;
      } else {
        ub = mb;
      }
    }
    
    cout << ub << endl;
  }
  
  return 0;
}

Measured memory_bytes: 1432
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
#define mp make_pair
 
const int INF = (int)1e6;
const int N = 2020;
int n;
int ans = INF;
int a[N], b[N];
char s[N];
int toL[N], toR[N];
int m;
pii c[N];
 
int main()
{
//	freopen("input.txt", "r", stdin);
//	freopen("output.txt", "w", stdout);
 
	scanf("%s", s);
	n = strlen(s);
	for (int i = 0; i < n; i++)
		a[i] = (int)(s[i] == '1');
	scanf("%s", s);
	for (int i = 0; i < n; i++)
		b[i] = (int)(s[i] == '1');
 
	bool allEq = true, allZ = true;
	for (int i = 0; i < n; i++) {
		allEq &= (a[i] == b[i]);
		allZ &= (b[i] == 0);
	}
	if (allEq) {
		printf("0\n");
		return 0;
	}
	if (allZ) {
		printf("-1\n");
		return 0;
	}
 
	toR[n] = INF;
	for (int i = n - 1; i >= 0; i--) {
		toR[i] = toR[i + 1];
		if (b[i] == 1)
			toR[i] = i;
	}
	for (int i = n - 1; i >= 0; i--) {
		if (toR[i] != INF) break;
		toR[i] = n + toR[0];
	}
	toL[0] = (b[0] == 1 ? 0 : INF);
	for (int i = 1; i < n; i++) {
		toL[i] = toL[i - 1];
		if (b[i] == 1)
			toL[i] = i;
	}
	for (int i = 0; i < n; i++) {
		if (toL[i] != INF) continue;
		toL[i] = toL[n - 1] - n;
	}
 
	/*
	for (int i = 0; i < n; i++)
		printf("%d %d\n", toL[i], toR[i]);
	*/
 
	for (int sh = -n; sh <= n; sh++) {
		int res = abs(sh);
		m = 0;
		for (int i = 0; i < n; i++) {
			int q = (i + sh + n) % n;
			if (a[i] == b[q]) continue;
			res++;
			if (sh <= 0) {
				if (i - toL[i] <= -sh) continue;
				c[m].first = toR[i] - i;
				c[m].second = q - toL[q];
				m++;
			} else {
				if (toR[i] - i <= sh) continue;
				c[m].first = i - toL[i];
				c[m].second = toR[q] - q;
				m++;
			}
		}
		c[m++] = mp(0, 0);
		sort(c, c + m);
		int bst = c[m - 1].first;
		int mx = 0;
		for (int i = m - 1; i > 0; i--) {
			mx = max(mx, c[i].second);
			bst = min(bst, c[i - 1].first + mx);
		}
		ans = min(ans, res + 2 * bst);
	}
	printf("%d\n", ans);
 
	return 0;
}

Measured memory_bytes: 384
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const ll INF = 1e18;
const ll MAX = 25;
const ll NIL = -1;

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
template<class T> inline bool chmax(T& a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template<class T> inline bool chmin(T& a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
int ctoi(char c) {
  if (c >= '0' && c <= '9') {
    return c - '0';
  }
  return 0;
}
ll factorial(ll n) {
  if (n == 1) {
    return 1;
  }
  ll ans = factorial(n-1);
  return ans*n;
}
ll gcd(ll a, ll b) {
  if (a < b) {
    ll tmp = a;
    a = b;
    b = tmp;
  }
  ll r = a%b;
  while(r != 0) {
    a = b;
    b = r;
    r = a%b;
  }
  return b;
}
ll lcm(ll a, ll b) {
  return (a/gcd(a, b))*b;
}

struct Node {
  ll parent;
  ll left;
  ll right;
};

vector <Node> t(MAX);
vector <ll> d(MAX);
vector <ll> h(MAX);

void setDepth(ll u, ll p) {
  d.at(u) = p;
  if (t.at(u).right != NIL) {
    setDepth(t.at(u).right, p+1);
  }
  if (t.at(u).left != NIL) {
    setDepth(t.at(u).left, p+1);
  }
  return;
}

ll setHeight(vector <ll> h, ll u) {
  ll h1 = 0, h2 = 0;
  if (t.at(u).right != NIL) {
    h1 = setHeight(h, t.at(u).right) + 1;
  }
  if (t.at(u).left != NIL) {
    h2 = setHeight(h, t.at(u).left) + 1;
  }
  h.at(u) = max(h1, h2);
  return h.at(u);
}

void print(ll u) {
  cout << "node " << u << ": ";
  cout << "parent = " << t.at(u).parent << ", ";
  ll sibling;
  if (t.at(u).parent == NIL) {
    sibling = NIL;
  } else {
    if (t.at(t.at(u).parent).left != u) {
      sibling = t.at(t.at(u).parent).left;
    } else {
      sibling = t.at(t.at(u).parent).right;
    }
  }
  cout << "sibling = " << sibling << ", ";
  ll degree = 0;
  if (t.at(u).left != NIL) degree++;
  if (t.at(u).right != NIL) degree++;
  cout << "degree = " << degree << ", ";
  cout << "depth = " << d.at(u) << ", ";
  cout << "height = " << setHeight(h, u) << ", ";
  if (t.at(u).parent == NIL) {
    cout << "root" << endl;
  } else if (t.at(u).left==NIL && t.at(u).right == NIL) {
    cout << "leaf" << endl;
  } else {
    cout << "internal node" << endl;
  }
  return;
}

int main() {
  ll n;
  cin >> n;
  rep(i, n) {
    t.at(i).parent = NIL;
    t.at(i).left = NIL;
    t.at(i).right = NIL;
  }
  ll id, l, r;
  rep(i, n) {
    cin >> id >> l >> r;
    t.at(id).left = l;
    t.at(id).right = r;
    if (l != NIL) t.at(l).parent = id;
    if (r != NIL) t.at(r).parent = id;
  }
  ll tmp;
  rep(i, n) {
    if (t.at(i).parent == NIL) tmp = i;
  }
  setDepth(tmp, 0);
  rep(i, n) print(i);
}


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

int main(){
    int a, b;
    cin >> a >> b;
    if(a<10 && b < 10)cout << a*b << endl;
    else cout << -1 << endl;
    return 0;
}

Measured memory_bytes: 256
#include<iostream>
#include<string>
#include<algorithm>
#include<vector>
#include<iomanip>
#include<math.h>
#include<complex>
#include<queue>
#include<deque>
#include<stack>
#include<map>
#include<set>
#include<bitset>
using namespace std;
#define REP(i,m,n) for(int i=(int)m ; i < (int) n ; ++i )
#define rep(i,n) REP(i,0,n)
typedef long long ll;
typedef pair<int,int> pint;
typedef pair<ll,int> pli;
const int inf=1e9+7;
const ll longinf=1LL<<60 ;
const ll mod=1e9+7 ;

int main(){
    string s;
    cin>>s;
    int n=s.size();
    s='0'+s;
    rep(i,n+1)if(s[i]!=s[n-i]){
        cout<<-1<<endl;
        return 0;
    }
    if(s[1]=='0'){
        cout<<-1<<endl;
        return 0;
    }
    int num=0;
    rep(i,n/2+1){
        if(s[i]=='1'){
            ++num;
            cout<<num<<" "<<num+1<<endl;
        }
    }
    num++;
    int now=1;
    rep(i,n/2+1){
        if(i==0)continue;
        if(s[i]=='1')now++;
        else cout<<++num<<" "<<now<<endl;
    }
    REP(i,num+1,n+1)cout<<now<<" "<<i<<endl;
    return 0;
}


Measured memory_bytes: 1660
def rec(digit, flag, k):
    limit = int(N[digit])
    if k > K:
        return 0
    if digit == len(N) - 1:
        if k == K:
            return 1
        elif k == K - 1:
            return limit if flag else 9
        else:
            return 0
    if flag:
        if limit == 0:
            return rec(digit + 1, 1, k)
        else:
            return rec(digit + 1, 0, k + 1) * (limit - 1) + rec(digit + 1, 1, k + 1) + rec(digit + 1, 0, k)
    else:
        return rec(digit + 1, 0, k + 1) * 9 + rec(digit + 1, 0, k)

N = input()
K = int(input())
print(rec(0, 1, 0))

Measured memory_bytes: 61400
//
//  ROIGold.cpp
//  Main calisma
//
//  Created by Rakhman on 05/02/2019.
//  Copyright © 2019 Rakhman. All rights reserved.
//
 
#include <cstring>
#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <queue>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <cassert>
#include <iterator>
 
#define ios ios_base::sync_with_stdio(0), cout.tie(0), cin.tie(0);
#define S second
#define F first
#define pb push_back
#define nl '\n'
#define NL cout << '\n';
#define EX exit(0)
#define all(s) s.begin(), s.end()
#define no_answer return cout << 0, 0;
#define FOR(i, start, finish, k) for(int i = start; i <= finish; i += k)
 
const int MXN = 1e7 + 200;
const long long MNN = 2e3 + 520;
const long long MOD = 1e9 + 7;
const long long INF = 1e18;
const int OO = 1e9 + 500;
 
typedef long long llong;
typedef unsigned long long ullong;
 
using namespace std;

llong n, k, can[30][30], dp[MXN];

void add(llong& a, llong b){
    a += b;
    //if(a < 0) a += MOD;
    while(a >= MOD) a -= MOD;
}

int main () {
    ios;
    cin >> n;
    for(int i = 0; i < n; i++){
        for(int j = 0; j < n; j++){
            cin >> can[i][j];
        }
    }
    dp[0] = 1;
    for(int i = 0; i < (1 << n); i++){
        int a = __builtin_popcount(i);
        for(int j = 0; j < n; j++){
            if(can[a][j] && !(i & (1 << j))){
                int mask = i ^ (1 << j);
                add(dp[mask], dp[i]);
            }
        }
    }
    cout << dp[(1 << n) - 1];
}


Measured memory_bytes: 18688
#include<iostream>
#include<vector>
#include <cmath>
#include <map>
#include <cctype>
#include <algorithm>
using namespace std;
int main(void){
    int a[3] ;
    for(int i = 0; i < 3 ; i++ ){
        cin >> a[i] ;
    }

    sort(a,a+3);
    int count = 3 ;
    for(int j = 0; j < 2 ; j++){
        if(a[j] == a[j+1]){
            count--;
        }
    }

    cout << count << endl;


    return 0;
}

Measured memory_bytes: 256
#include<bits/stdc++.h>
using namespace std;
#define int long long
const int INF=1LL<<55LL; 
int cnt;
void merge(int *A,int left,int mid,int right){
  int n1=mid-left;
  int n2=right-mid;
  int L[n1+1],R[n2+1];
  for(int i=0;i<n1;i++) L[i]=A[left+i];
  for(int i=0;i<n2;i++) R[i]=A[mid +i];
  L[n1]=INF;
  R[n2]=INF;
  int i=0,j=0;
  for(int k=left;k<right;k++){
    cnt++;
    if(L[i]<=R[j]) A[k]=L[i++];
    else A[k]=R[j++];
  }
}
void mergeSort(int *A,int left,int right){
  if(left+1<right){
    int mid=(left+right)/2;
    mergeSort(A,left,mid);
    mergeSort(A,mid,right);
    merge(A,left,mid,right);
  }
}
signed main(){
  cin.tie(0);
  ios::sync_with_stdio(0);
  int n;
  cin>>n;
  int S[n];
  for(int i=0;i<n;i++) cin>>S[i];
  cnt=0;
  mergeSort(S,0,n);
  for(int i=0;i<n;i++) cout<<S[i]<<" \n"[i==n-1];
  cout<<cnt<<endl;
  return 0;
}

Measured memory_bytes: 10936
#include <iostream>
#include <vector>
using namespace std;
typedef long long ll;
int D;
int v=27;
vector<int> c(v,0);
vector<vector<int>> s;
vector<int> t;
vector<int> last(v,0);

void init(){
    cin >> D;
    s.resize(D+1);
    t.resize(D+1);
    for(int i=0;i<D+1;i++){
        s[i].resize(v);
    }

    for(int i=1;i<27;i++){
        cin >> c.at(i);
    }
    for(int i=1;i<D+1;i++){
        for(int j=1;j<27;j++){
            cin >> s.at(i).at(j);
        }
    }

    for(int i=1;i<D+1;i++){
        cin >> t.at(i);
    }

}

void init_last(){
  for(int i=0;i<v;i++){
    last.at(i) = 0;
  }
}

int main(){
    init();

    int Q;
    cin >> Q;
    for(int i=0;i<Q;i++){
      	ll ans=0;
        int d,q;
        cin >> d >> q;
        t.at(d) = q;
      	init_last();
      	/*
      	cout << "schedule" << endl;
      	for(int i=1;i<D+1;i++){
          cout << t.at(i) << " ";
        }
      	cout << endl;
        */
        for(int i=1;i<D+1;i++){
            ans += s.at(i).at(t.at(i));
            last.at(t.at(i)) = i;
            for(int j=1;j<v;j++){
                ans -= c.at(j) * (i - last.at(j));
            }
            //cout << ans << endl;
        }
        cout << ans << endl;
    }
  
  	return(0);
}

Measured memory_bytes: 3664
def lower_bound(arr, v)
  l = -1; r = arr.size
  while l + 1 < r
    m = (l + r) / 2
    if arr[m] <= v then l = m
    else r = m
    end
  end
  l
end
n = gets.to_i
arr = []
n.times{ arr << gets.to_i }
sum = Array.new(2*n+1, 0)
(2*n).times{ |i| sum[i+1] += sum[i] + arr[i%n] }
l = 0; r = sum[n] / 3 + 1
while l + 1 < r
  m = (l + r) / 2
  f = nil
  n.times do |i|
    a = lower_bound(sum[i,n], m + sum[i] - 1) + i + 1
    if a < i+n
      b = lower_bound(sum[a...i+n], m + sum[a] - 1) + a + 1
      if b < i+n && sum[i+n] - sum[b] >= m
        l = m
        f = true
        break
      end
    end
  end
  r = m unless f 
end
p l

Measured memory_bytes: 11316
#include <iostream>
#include <vector>
#define For(i,x,y) for(register int i=(x); i<=(y); i++)
using namespace std;
int main()
{
    int n, m,sum,x;
    vector<int>v;
    cin>>n>>m;
    sum=0;
    For(i,1,n)
    {
        cin>>x;
        sum+=x;
        v.push_back(x);
    }
    sum=(sum+m*4-1)/(m*4);
    int cnt=0;
    int len=v.size();
    For(i,0,len-1)if(v[i]>=sum)cnt++;
    if(cnt>=m)cout<<"Yes"<<endl;
    else cout<<"No"<<endl;
    return 0;
}


Measured memory_bytes: 3632
#include <iostream>
#include <tuple>
#include <map>
#include <vector>
#include <algorithm>
#include <cmath>
#include <limits>
using namespace std;
void init();
#define REP(var, num) for(sll var = 0; var < num; var++)
typedef signed long long sll;
typedef unsigned long long ull;
/*
numeric_limits<sll>::max()
*/

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

    vector<sll> counts(m, 0);
    REP(i, n) {
        sll k;
        cin >> k;
        REP(j, k) {
            sll a;
            cin >> a;
            counts[a - 1]++;
        }
    }

    sll r = 0;
    REP(i, m) {
        if (counts[i] == n) {
            r++;
        }
    }

    cout << r << endl;
    return 0;
}

void init() {
    cin.tie(0);
    ios::sync_with_stdio(false);
}

Measured memory_bytes: 256
#include <iostream>
#include <vector>
#include <set>
#include <map>
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <string>
#include <stack>
#include <queue>
#include <unordered_set>
#include <unordered_map>
#include <cstdlib>
#include <ctime>
#include <deque>
#include <bitset>
#include <fstream>

using namespace std;

#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define pii pair <int, int>
#define pll pair <long long, long long>
#define vi vector <int>
typedef long long ll;
typedef unsigned long long ull;
typedef unsigned int uint;

const int K = 105;
const int N = K * K;
int x, y, a[N], col[K][K], h, w, n;

void get_next()
{
	if(x % 2 == 0)
	{
		if(y != w - 1)
		{
			y++;
			return;
		}
		x++;
		return;
	}
	if(y != 0)
	{
		y--;
		return;
	}
	x++;
}

int main()
{
	ios:: sync_with_stdio(false);
	cin >> h >> w >> n;
	for(int i = 0; i < n; i++)
		cin >> a[i];
	int cur = 0;
	for(int i = 0; i < h * w; i++)
	{
		while(a[cur] == 0)
			cur++;
		col[x][y] = cur + 1;
		a[cur]--;
		get_next();
	}
	for(int i = 0; i < h; i++, cout << "\n")
		for(int j = 0; j < w; j++)
			cout << col[i][j] << " ";
	return 0;
}


Measured memory_bytes: 384
import itertools
N, Q, row, col = input(), [], range(8), range(8)
for _ in range(N):
    r, c = map(int, raw_input().split())
    Q.append((r, c))
    row.remove(r)
    col.remove(c)
for l in itertools.permutations(col):
    queen = Q + list(zip(row, l))
    if not any(any((r1 != r2 or c1 != c2) and (r1 == r2 or c1 == c2 or r1 + c1 == r2 + c2 or r1 - c1 == r2 - c2) for r2, c2 in queen) for r1, c1 in queen):
        print '\n'.join(''.join('Q' if (r, c) in queen else '.' for c in range(8)) for r in range(8))
        break

Measured memory_bytes: 6552
import java.util.*;

public class Main{
    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);

        int a = sc.nextInt();
        int b = sc.nextInt();

        int[] sa = new int[b];
        for ( int i = 0;  i < b;  i++ ){
            sa[i] = a;
        }

        int[] sb = new int[a];
        for ( int i = 0;  i < a;  i++ ){
            sb[i] = b;
        }

        if(sa.length>sb.length){
            for(int in = 0; in<sa.length; in++){
            System.out.print(sa[in]);
            }
        } else {
            for(int in = 0; in<sb.length; in++){
                System.out.print(sb[in]);
                }
            
        }

        sc.close();



    }
}


Measured memory_bytes: 21460
#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <stack>
#include <queue>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <ctime>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;

static const double EPS = 1e-8;
static const double PI = 4.0 * atan(1.0);
static const double PI2 = 8.0 * atan(1.0);

#define REP(i,n)	for(int i=0;i<(int)n;++i)
#define ALL(c)		(c).begin(),(c).end()
#define CLEAR(v)	memset(v,0,sizeof(v))
#define MP(a,b)		make_pair((a),(b))
#define ABS(a)		((a)>0?(a):-(a))
#define FOR(i,s,n)	for(int i=s;i<(int)n;++i)

int main(int argc, char **argv) {
	int N;
	cin >> N;
	cout << ((N + 1) * N / 2) << endl;
	return 0;
}


Measured memory_bytes: 256
fn main() {
    let s: String = read_line_as_value();
    let iter = s.chars();
    let rev_iter = s.chars().rev();

    let mut doubled_ans = 0;
    for (i, j) in iter.zip(rev_iter) {
        if i != j {
            doubled_ans += 1;
        }
    }
    println!("{}", doubled_ans / 2)
}

fn read_line_as_value<T: std::str::FromStr>() -> T {
    let mut input = String::new();
    let res = std::io::stdin().read_line(&mut input);
    if let Err(error) = res {
        panic!("io error at stdin.read_line: {}", error);
    };
    match input.trim().parse() {
        Ok(val) => val,
        _ => panic!("error while parsing input: {}", input)
    }
}

Measured memory_bytes: 2132
#include<stdio.h>

int main() {
	int n, y;
	scanf("%d%d", &n, &y);
	y /= 1000;
	for (int i = 0; i <= n; i++) {
		for (int j = 0; j <= n-i; j++) {
			if (9 * i + 4 * j == y - n) {
				printf("%d\t%d\t%d", i, j, n - i - j);
				return 0;
			}
		}
	}
	printf("-1 -1 -1");

	return 0;
}

Measured memory_bytes: 128
// AOJ 1316: The Sorcerer's Donut
// 2017.12.4 bal4u@uu

#include <stdio.h>
#include <string.h>

#define HASHSIZ 500009
typedef struct { unsigned x; int w; } HASH;
HASH hash[HASHSIZ+2], *hashend = hash+HASHSIZ;			// Hash table
char ans[500]; int len;
char buf[1000];

void lookup(unsigned x, int w)
{
	HASH *tp = hash + ( x % HASHSIZ);

	while (tp->x) {
		if (w == tp->w && x == tp->x) {
			if (w > len || (w == len && memcmp(buf, ans, w) < 0))
				memcpy(ans, buf, w), len = w;
			return;
		}
		if (++tp == hashend) tp = hash;
	}
	tp->x = x, tp->w = w;
}

#define M 4294967291
int h, w, sr, sc, dir;
char m[10][22];
int mv[8][2] = {
	{1,1},{-1,1},{-1,-1},{1,-1},
	{0,1},{0,-1},
	{1,0},{-1,0}};

void rec(int r, int c, unsigned x, int len)
{
	int r2, c2;

	buf[len++] = m[r][c];
	x = (((long long)x << 5) | (m[r][c]-0x40)) % M;
	lookup(x, len);

	r2 = r + mv[dir][0]; if (r2 < 0) r2 = h-1; else if (r2 >= h) r2 = 0;
	c2 = c + mv[dir][1]; if (c2 < 0) c2 = w-1; else if (c2 >= w) c2 = 0;

	if (r2 == sr && c2 == sc) return;
	rec(r2, c2, x, len);
}

int main()
{
	int r;

	while (scanf("%d%d", &h, &w) && h > 0) {
		memset(hash, 0, sizeof(hash));
		for (r = 0; r < h; r++) scanf("%s", m[r]);

		len = 0;
		for (sr = 0; sr < h; sr++) for (sc = 0; sc < w; sc++) for (dir = 0; dir < 8; dir++) {
			if (len > h && (dir == 6 || dir == 7)) continue;
			if (len > w && (dir == 4 || dir == 5)) continue;
			rec(sr, sc, 0, 0);
		}

		if (len < 2) puts("0");
		else ans[len] = 0, puts(ans);
	}
	return 0;
}

Measured memory_bytes: 5612
import java.util.Arrays;
import java.util.Scanner;

public class Main {
	Scanner sc = new Scanner(System.in);
	int cnt[];
	int ans[];
	int info[][];
	boolean judge;
	int n, x, m;
	int max = 0;

	void run() {
		n = sc.nextInt();
		x = sc.nextInt();
		m = sc.nextInt();
		cnt = new int[n + 1];
		ans = new int[n + 1];
		info = new int[m][3];
		judge = false;
		for (int i = 0; i < m; i++) {
			info[i][0] = sc.nextInt();
			info[i][1] = sc.nextInt();
			info[i][2] = sc.nextInt();
		}
		Arrays.fill(ans, -1);
		dfs(0);
		if (ans[1] == -1) {
			System.out.println(-1);
		} else {
			for (int j = 1; j < n; j++) {
				System.out.print(ans[j] + " ");
			}
			System.out.println(ans[n]);
		}
	}

	void dfs(int oriCnt) {
		if (oriCnt == n + 1) {
			judge = false;
			for (int i = 0; i < m; i++) {
				int sum = 0;
				for (int j = info[i][0]; j <= info[i][1]; j++) {
					sum += cnt[j];
				}
				if (sum == info[i][2]) {
					judge = true;
				} else {
					judge = false;
				}
				if (!judge) {
					return;
				}
			}
			if (judge) {
				int sum = 0;
				for (int j = 1; j <= n; j++) {
					sum += cnt[j];
				}
				if (sum >= max) {
					for (int k = 1; k <= n; k++) {
						ans[k] = cnt[k];
					}
				}
			}
			return;
		} else {
			for (int i = 0; i <= x; i++) {
				cnt[oriCnt] = i;
				dfs(oriCnt + 1);
			}
		}
	}

	public static void main(String[] args) {
		new Main().run();
	}
}

Measured memory_bytes: 23976
import sys
import itertools
sys.setrecursionlimit(1000000000)
from heapq import heapify,heappop,heappush,heappushpop
import math
import collections
MOD = 10**9 + 7

array = []
n,k = map(int,input().split())
for i in range(n):
    a,b = map(int,input().split())
    array.append([a,b])
array.sort()
sumli = 0
for i in range(len(array)):
    sumli += array[i][1]
    if sumli>=k:
        break
print(array[i][0])

Measured memory_bytes: 20436
#include<cstdio>
#include<sstream>
#include<cstdlib>
#include<cctype>
#include<cmath>
#include<algorithm>
#include<set>
#include<queue>
#include<stack>
#include<list>
#include<iostream>
#include<fstream>
#include<numeric>
#include<string>
#include<assert.h>
//#include<unordered_map>
//#include<unordered_set>
#include<vector>
#include<cstring>
#include<map>
#include<iterator>
#include <iomanip>
#include<bits/stdc++.h>

using namespace std;
#define lead_zero(x)    __builtin_clzll(x)
#define trail_zero(x)   __builtin_ctz(x)
#define total_1s(x)     __builtin_popcount(x)
#define ll long long int
#define pi 3.14159265358979323846
#define filein freopen("input.txt", "r", stdin)
#define forn(i,n) for(int i=0;i<n;i++)
#define maxx 1000000005
#define nullptr NULL


int main()
{
   int n;
   cin>>n;
   ll a[n+5];
   for(int i=0;i<n;i++)
   {
       cin>>a[i];
   }
  ll ans=a[0];
  for(int i=1;i<n;i++)
  {
      ans=(__gcd(a[i],ans));

  }
  cout<<ans<<endl;


}


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

/*
  ll n , m ;
  cin >> n >> m ;
  ll a , b , c ;
  cin >> a >> b >> c ;
  vector<ll> A(n) ;
  cout << "Yes" << endl ;
  cout << "No"  << endl ;
*/

int main(){
    ll n ;
    cin >> n ;
    vector<ll> A(n) ;
    rep(i,n) cin >> A.at(i) ;
    ll count = 0 ;
    map<ll,ll> mp ;
    rep(i,n) mp[ A.at(i) + i + 1 ]++ ;
    rep(j,n) count += mp[(j+1) - A.at(j) ] ;
    cout << count << endl ; 
}


Measured memory_bytes: 27328