akhauriyash/Code-Regression huggingface dataset:
#include <bits/stdc++.h>
#define _GLIBCXX_DEBUG
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define repi(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
using namespace std;
typedef long long ll;

#define zero_pad(num) setfill('0') << right << setw(num)

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

  vector<ll> P(M);
  vector<ll> Y(M);
  vector<vector<ll>> X(N);

  rep(i, M){
    cin >> P[i] >> Y[i];
    P[i]--;
    X[P[i]].push_back(Y[i]);
  }

  rep(i, N){
    sort(X[i].begin(), X[i].end());
  }

  rep(i, M){
    ll upper = P[i] + 1;
    ll lower = lower_bound(X[P[i]].begin(), X[P[i]].end(), Y[i]) - X[P[i]].begin() + 1;
    cout << zero_pad(6) << upper << zero_pad(6) << lower << endl;
  }
}

Measured memory_bytes: 10292
import java.io.*;
public class Main{
	public static void main(String args[]){
    int ans1 = 1, ans2 = 0;
    BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
    try{
      String line = reader.readLine();
      for(String splitted: line.split(" ")){
        ans1 *= Integer.parseInt(splitted);
        ans2 += Integer.parseInt(splitted) * 2;
      }
		}catch(Exception e){
      ans1 = 0;
    }
		System.out.println(ans1 + " " + ans2);
	}
}


Measured memory_bytes: 24296
N = int(input())
Alist = list(map(int,input().split()))#iの上司

result = [0]*N

for i in range(N-1):#最後の社員に部下いない
    result[Alist[i]-1] +=1#上司の順番の要素に+1

for i in range(N):
    print(result[i])

Measured memory_bytes: 32216
#define _USE_MATH_DEFINES

#include <iostream>	//cin, cout
#include <vector>	//vector
#include <algorithm> //sort,min,max,count
#include <string>	//string,getline
#include <ios>		//fixed
#include <iomanip>	//setprecision
#include <utility> //swap
#include <cstdlib>	//abs(int)
#include <cmath>	//sqrt
#include <sstream>	//stringstream,getline
#include <cmath>	//ceil,M_PI
#include <numeric>	//gcd

using namespace std;

int main() {

	int a;
	cin >> a;

	cout << a * a << endl;

	return 0;

}

Measured memory_bytes: 3632
use proconio::{fastout, input};

#[fastout]
fn main() {
    input! {n: u128,k: u128};
    let mut k_sum: u128 = k;
    let mut count = 1;

    while n >= k_sum {
        k_sum *= k;
        count += 1;
    }

    println!("{}", count)
}


Measured memory_bytes: 2172
function Main(input) {
	var str = input;
	str = str.split("\n")[0];
    var n = Number(str);
    var ans = 0;
   	for(var i = 1; i <= n; i++){
	if(i %3 !==0 && i %5!==0){
		ans = ans + i;
		}
	}
   console.log(ans);
}
 //*おまじない
Main(require("fs").readFileSync("/dev/stdin", "utf8"));


Measured memory_bytes: 34952
import java.util.Arrays;
import java.util.Comparator;
import java.util.Scanner;

public class Main {
    static int gcd(int a, int b) {
        if (a % b != 0) {
            return gcd(b, a % b);
        } else {
            return b;
        }

    }

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int count = 0;
        int n = sc.nextInt();
        int x = sc.nextInt();
      
        if (n % x == 0) {
            System.out.println(0);
        } else {
            System.out.println(1);
        }
    }
}

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

using namespace std;

int main() {
  for (int i = 0; i < 5; i++) {
    int _in;
    
    cin >> _in;
    
    if (_in == 0) {
      cout << i + 1 << endl;
    }
  }
  
  return 0;
}

Measured memory_bytes: 3520
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
const int inf = 1e9;

void getfname(vector<int> &fname){
	string input;
	cin >> input;
	bool isnum = false;
	for(int i=0; i<(int)input.length(); i++){
		if('0' <= input[i] && input[i] <= '9'){
			if(isnum){
				fname.back() *= 10;
				fname.back() += input[i]-'0';
			}else{
				fname.push_back(input[i]-'0');
			}
			isnum = true;
		}else{
			fname.push_back(input[i] +inf);
			isnum = false;
		}
	}
}

int main(){
	int n;
	cin >> n;
	vector<vector<int> > fname(n+1);
	getfname(fname[0]);
	for(int i=1; i<n+1; i++){
		getfname(fname[i]);
		if(fname[i] < fname[0]){
			cout << "-" << endl;
		}else{
			cout << "+" << endl;
		}
	}
	 return 0;
}


Measured memory_bytes: 3264
#include<bits/stdc++.h>
using namespace std;
#include <ext/pb_ds/assoc_container.hpp> 
#include <ext/pb_ds/tree_policy.hpp> 
#include <functional> // for less 
using namespace __gnu_pbds; 
#define mod 1000000007
#define int long long
#define pb push_back
#define boost ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define inp(n) for(i=0;i<n;i++) cin>>A[i];
#define inz(A) memset(A,0,sizeof(A))
#define lb lower_bound
#define ub upper_bound
#define all(v) v.begin(),v.end()
#define T 1000000
#define F first
#define S second
#define pi pair<int,int>
#define vi vector<int>
#define mii map<int,int>
#define vp vector<pair< int,int> >
#define mpi map<pair<int,int> ,int>
#define lower(s) transform(all(s),s.begin(),::tolower)
#define endl "\n"
typedef tree<int, null_type, less<int>, rb_tree_tag, 
         tree_order_statistics_node_update> pbds;
int add(int a, int b) {a=(a%mod + b%mod)%mod;return a;}
int sub(int a, int b) {a =(a%mod - b%mod + mod)%mod ;return a;}
int mul(int a, int b) {return (a%mod * b%mod) % mod;}
 
/* brownsands :Arpit */		

void solve()
{    string s;int i;
    cin >> s;
    for(i=0;i<3;i++)
    if(s[i]=='7')
    {
        cout<<"Yes";
        return;
    }
    cout<<"No";
    return;
}
signed main()
{ 
  ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
  int test=0,t;
  //  cin>>t;
    t=1;
   while(test++ <t){         
   	//cout<<"Case# "<<test<<": ";
        solve();}
  cerr << "\nTime : " << (double)clock() / (double)CLOCKS_PER_SEC << "s\n";
  }


Measured memory_bytes: 3840
#include<iostream>
#include<sstream>
#include<ios>
#include<iomanip>
#include<algorithm>
#include<vector>
#include<list>
#include<queue>
#include<cstdlib>
#include<cmath>
#include<queue>
#include<deque>
#include<map>
#include<string>
#include<numeric>
#include<set>
#define rep(i, n) for(int i = 0; i < (int)(n); i++)
#define rep2(i, m, n) for(int i = (int)(m); i < (int)(n); i++)
#define rep_inv(i, n, m) for(int i = (int)(n); i > (int)(m); i--)
using namespace std;
using vi = vector<int>;
using vl = vector<long>;
using vc = vector<char>;
using vvi = vector<vector<int>>;
using vvl = vector<vector<long>>;
using vvc = vector<vector<char>>;
using pii = pair<int, int>;
using pll = pair<long, long>;
using vpii = vector<pii>;
using vpll = vector<pll>;

int main(){
  int K, X;
  cin >> K >> X;

  if(500*K >= X){
    cout << "Yes" << endl;
  } else{
    cout << "No" << endl;
  }
  
  return 0;
}

Measured memory_bytes: 256
#import<iostream>
#import<map>
#import<queue>
#import<cstdlib>
#import<cstring>
using namespace std;struct B{int z[2];char s[14];bool operator<(const B&r)const{return strcmp(s,r.s)<0;}};main(){int a[13][5]={{2,-1},{2,5,-1},{0,1,3,6,-1},{2,7,-1},{5,-1},{1,4,6,9,-1},{2,5,7,10,-1},{3,6,8,11,-1},{7,-1},{5,10,-1},{6,9,11,12,-1},{7,10,-1},{10,-1},},gx[]={2,1,2,3,0,1,2,3,4,1,2,3,2},gy[]={0,1,1,1,2,2,2,2,2,3,3,3,4},v,d,s,i,j;B g={0,0,"0123456789:;0"};for(;;){map<B,int>m;queue<B>q;B b={-1,-1};for(i=0;i<13;i++){cin>>v;if(v<0)return 0;b.s[i]=48+v;if(!v)b.z[b.z[0]!=-1]=i;}b.s[13]=0;m[b]=1;for(q.push(b);!q.empty();q.pop()){B&b=q.front();if(!strcmp(b.s,g.s))break;d=0;for(i=13;i--;){v=b.s[i]-48;if(v)d+=abs(gx[v]-gx[i])+abs(gy[v]-gy[i]);}s=m[b];if(s+d<22)for(j=2;j--;)for(i=0;~a[b.z[j]][i];i++){B n=b;n.z[j]=a[b.z[j]][i];if(n.z[j]-n.z[1-j]){swap(n.s[b.z[j]],n.s[n.z[j]]);if(!m[n])m[n]=s+1,q.push(n);}}}printf(m[g]?"%d\n":"NA\n",m[g]-1);}}

Measured memory_bytes: 1480
#include <iostream>
#include <iomanip>
#include <random>
#include <algorithm>
#include <vector>
#include <set>
#include <map>
#include <cmath>
#include <numeric>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <sstream>
#include <bitset>
#include <cassert>
#include <fstream>
#include <queue>

#define len(X) ((int)(X).size())

#ifdef __LOCAL
	#define DBG(X) cout << #X << "=" << (X) << '\n';
	#define SAY(X) cout << (X) << '\n';
#else
	#define DBG(X)
	#define SAY(X)
#endif
 
using namespace std;

using ll = long long int;
using ull = unsigned long long int;
using ld  = long double;
using pii = pair<int, int>;
using pll = pair<ll, ll>;

const int INT_INF = (int)(2e9);
const ll  LL_INF  = (ll)(2e18);

const int NIL = -1;
static mt19937 _g(time(nullptr));

inline ll randint(ll a, ll b) { ll w = (_g() << 31LL) ^ _g(); return a + w % (b - a + 1); }
inline void fast_io() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); };
template<typename T> inline T sign(T x) { return T(x > 0) - T(x < 0); }
template<typename T, typename S> inline ostream& operator<<(ostream& os, const pair<T, S> p) { cout << "[" << p.first << ";" << p.second << "]"; return os; }
template<typename T> inline ostream& operator<<(ostream& os, const vector<T>& v) { for(auto el: v) cout << el << " "; return os; }
template<typename T> inline T fetch() { T ret; cin >> ret; return ret; }
template<typename T> inline vector<T> fetch_vec(int sz) { vector<T> ret(sz); for(auto& elem: ret) cin >> elem; return ret; }

void solve() {
	int n = fetch<int>();
	int answ = 0;

	for(int i = 1; i <= n; ++i) {
		int j = n - i;
		answ += (i < j);
	}

	cout << answ << '\n';
}

int main() {
	fast_io();
	solve();

	return 0;
}


Measured memory_bytes: 256
#include <stdio.h>

int main(int argc, const char * argv[]) {
   
    char array[100]={'\0'},buff[1024];
    int n,i,j,c,b = 0,count = 0;
    
    char moziretu[10][6]={{},{'.',',','!','?',' '},{'a','b','c'},{'d','e','f'},{'g','h','i'},{'j','k','l'},{'m','n','o'},{'p','q','r','s'},{'t','u','v'},{'w','x','y','z'}};
    scanf("%d",&n);
    for (i=0; i<n; i++) {
        
        scanf("%s",buff);
        
        for (j=0; buff[j]!='\0'; j++) {
            
            switch (buff[j]) {
                case '1':
                    if (b>4) {
                        b=0;
                    }
                    b++;
                    
                    
                    break;
                case '2':
                    if (b>2) {
                        b=0;
                    }
                    b++;
                    
                    
                    
                    break;
                    
                case '3':
                    if (b>2) {
                        b=0;
                    }
                    b++;
                    
                    break;
                    
                case '4':
                    if (b>2) {
                        b=0;
                    }
                    b++;
                    
                    break;
                    
                case '5':
                    if (b>2) {
                        b=0;
                    }

                    b++;
                    
                    break;
                    
                case '6':
                    if (b>2) {
                        b=0;
                    }
                    b++;
                    
                    break;
                    
                case '7':
                    if (b>3) {
                        b=0;
                    }
                    b++;
                    
                    break;
                    
                case '8':
                    if (b>2) {
                        b=0;
                    }
                    b++;
                    
                    break;
                    
                case '9':
                    if (b>3) {
                        b=0;
                    }
                    b++;
                    
                    break;
                    
                case '0':
                    
                    if (b!=0) {
                        //kakutei//????????????array?????\??????
                        array[count] = moziretu[(int)buff[j-1]-48][b-1];
                        count++;
                        b=0;
                    }
                    
                    break;
                    
                default:
                    
                    break;
            }
        }
        printf("%s\n",array);
        
        count=0;
        b=0;
        
        for (j =0; j < 100; j++) {
            array[j] = '\0';
        }
        
        for (j = 0; j < 1024; j++) {
            buff[j] = '\0';
        }
        
        
    }
    
    return 0;
}

Measured memory_bytes: 588
#define __USE_MINGW_ANSI_STDIO 0
#include <bits/stdc++.h>

using namespace std;
using ll = long long;
#define int ll
using VI = vector<int>;
using VVI = vector<VI>;
using PII = pair<int, int>;

#define FOR(i, a, n) for (ll i = (ll)a; i < (ll)n; ++i)
#define REP(i, n) FOR(i, 0, n)
#define ALL(x) x.begin(), x.end()
#define PB push_back

const ll LLINF = (1LL<<60);
const int INF = (1LL<<30);
const int MOD = 1000000007;

template <typename T> T &chmin(T &a, const T &b) { return a = min(a, b); }
template <typename T> T &chmax(T &a, const T &b) { return a = max(a, b); }
template <typename T> bool IN(T a, T b, T x) { return a<=x&&x<b; }
template<typename T> T ceil(T a, T b) { return a/b + !!(a%b); }
template<class S,class T>
ostream &operator <<(ostream& out,const pair<S,T>& a){
  out<<'('<<a.first<<','<<a.second<<')';
  return out;
}
template<class T>
ostream &operator <<(ostream& out,const vector<T>& a){
  out<<'[';
  REP(i, a.size()) {out<<a[i];if(i!=a.size()-1)out<<',';}
  out<<']';
  return out;
}

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

VI c[105];
int sum[105], m[105], idx[105];
signed main(void)
{
  cin.tie(0);
  ios::sync_with_stdio(false);

  while(true) {
    int n, d;
    cin >> n >> d;
    if(!n) break;
    int num = 0;
    memset(sum, 0, sizeof(sum));
    memset(idx, 0, sizeof(idx));
    REP(i, n) {
      cin >> m[i];
      num += m[i];
      c[i].resize(m[i]);
      REP(j, m[i]) {
        cin >> c[i][j];
        sum[i] += c[i][j];
      }
      reverse(ALL(c[i]));
    }

    REP(_, num) {
      // 条件を満たす国
      int index = -1;
      REP(i, n) {
        if(idx[i]==m[i]) continue;
        // 条件を満たすか
        sum[i] -= c[i][idx[i]];
        if(*max_element(sum, sum+n)-d <= sum[i]) index = i;
        sum[i] += c[i][idx[i]];
      }
      // 減らせる国がない
      if(index==-1) {
        cout << "No" << endl;
        goto end;
      }
      // 減らす
      sum[index] -= c[index][idx[index]++];
    }
    cout << "Yes" << endl;

    end:;
  }

  return 0;
}



Measured memory_bytes: 3212
/*
* Note: This template uses some c++11 functions , so you have to compile it with c++11 flag.
*       Example:-   $ g++ -std=c++11 c++Template.cpp
*
* Author : Akshay Pratap Singh
* Handle: code_crack_01
*
*/
 
/********   All Required Header Files ********/
#include <iostream>
#include <string>
#include <vector>
// #include <unordered_map>
#include <algorithm>
#include <sstream>
#include <queue>
#include <deque>
#include <bitset>
#include <iterator>
#include <list>
#include <stack>
#include <map>
#include <set>
#include <functional>
#include <numeric>
#include <utility>
#include <limits>
#include <time.h>
#include <math.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include <climits>

using namespace std;

/*******  All Required define Pre-Processors and typedef Constants *******/
#define SCD(t) scanf("%d",&t)
#define SCLD(t) scanf("%ld",&t)
#define SCLLD(t) scanf("%lld",&t)
#define SCC(t) scanf("%c",&t)
#define SCS(t) scanf("%s",t)
#define SCF(t) scanf("%f",&t)
#define SCLF(t) scanf("%lf",&t)
#define MEM(a, b) memset(a, (b), sizeof(a))
#define FOR(i, j, k, in) for(int i=j ; i<k ; i+=in)
#define RFOR(i, j, k, in) for(int i=j ; i>=k ; i-=in)
#define REP(i, j) FOR(i, 0, j, 1)
#define RREP(i, j) RFOR(i, j, 0, 1)
#define all(cont) cont.begin(), cont.end()
#define rall(cont) cont.end(), cont.begin()
#define FOREACH(it, l) for (auto it = l.begin(); it != l.end(); it++)
#define IN(A, B, C) assert( B <= A && A <= C)
#define MP make_pair
#define PB push_back
#define INF (int)1e9
#define EPS 1e-9
#define PI 3.1415926535897932384626433832795
#define MOD 1000000007
#define read(type) readInt<type>()
typedef long long int ll;
typedef unsigned long long int  ull;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<string> vs;
typedef vector<pii> vii;
typedef vector<ll> vll;
typedef vector<vi> vvi;

int main() {

    int N; cin >> N;
    vi p(N);
    REP(i, N) {
        cin >> p[i];
        p[i]--;
    }

    ll res = 0;
    int i = 0;
    while (i < N-1) {
        if( i == p[i] ) {
            swap(p[i], p[i+1]);
            i+=2;
            res++;
        } else {
            i++;
        }
    }
    if ( N-1 == p[N-1] ) res++;

    cout << res << endl;
    return 0;

}


Measured memory_bytes: 640
import math
from decimal import Decimal
a, b, x = map(int, input().split())
a, b, x = Decimal(a), Decimal(b), Decimal(x)

min_bai = math.ceil(a/x)  # xの倍数で最小
max_bai = math.floor(b/x)  # xの倍数で最大
x_bai = max_bai - min_bai + 1

# print(min_bai)  # debug
# print(max_bai)  # debug
print(x_bai)

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

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

using namespace std;

typedef long long ll;

int K[2000],S[100000],N,M,L;
ll sum[100000],memo[2000][2000];

ll getCost(int i,int j){
  ll value = ((K[i]-1>=0)?sum[K[i]-1]:0LL);
  ll cost = sum[K[j]] - value;
  return floor((double)cost/(double)L);
}

ll dfs(int p1,int p2){
  int v = 1 + max(p1,p2);
  if( v == N-1 ) return getCost(p1,N-1) + getCost(p2,N-1);
  if( memo[p1][p2] != -1 ) return memo[p1][p2];
  return memo[p1][p2] = min(getCost(p1,v)+dfs(v,p2),
                            getCost(p2,v)+dfs(p1,v));
}

int main(){

  cin >> N >> M >> L;
  rep(i,N) { cin >> K[i]; --K[i]; }
  rep(i,M) cin >> S[i];
  sort(K,K+N);
  sum[0] = S[0];
  REP(i,1,M) sum[i] = sum[i-1] + S[i];
  rep(i,N) rep(j,N) memo[i][j] = -1;
  cout << dfs(0,0) << endl;
  return 0;
}

Measured memory_bytes: 33692
#include<stdio.h>
int main(){
    long long int n,s,i,j,max,d[100010],a;
    while(scanf("%lld",&n),n){
        for(i=s=0;i<n;i++){
            scanf("%lld",&a);
            s+=a;
        }
        for(i=0;i<n-1;i++){
            scanf("%lld",&a);
            for(j=i;j&&d[j-1]<a;j--)d[j]=d[j-1];
            d[j]=a;
        }
        //for(i=0;i<n-1;i++)printf("%d ",d[i]);printf("\n");
        for(i=max=0;i<n;i++){
            if(max<s*(n-i))max=s*(n-i);
            s+=d[i];
        }
        printf("%lld\n",max);
    }
    return 0;
}

Measured memory_bytes: 1116
import sys
input = sys.stdin.readline
from collections import *

class BIT:
    def __init__(self, n):
        self.n = n
        self.bit = [0]*(n+1)
    
    def add(self, i, x):
        i += 1
        
        while i<=self.n:
            self.bit[i] += x
            i += i&(-i)

    def acc(self, i):
        s = 0
        
        while i>0:
            s += self.bit[i]
            i -= i&(-i)
        
        return s

N, Q = map(int, input().split())
c = list(map(int, input().split()))
prev = [-1]*N
ys = defaultdict(list)

for i in range(N):
    if prev[c[i]-1]!=-1:
        ys[prev[c[i]-1]].append(i)
    
    prev[c[i]-1] = i

que = defaultdict(list)

for i in range(Q):
    l, r = map(int, input().split())
    que[l-1].append((i, r-1))
    
idx = 0
bit = BIT(N)
ans = [-1]*Q

for x in range(N-1, -1, -1):
    for y in ys[x]:
        bit.add(y, 1)
    
    for i, r in que[x]:
        ans[i] = r-x+1-bit.acc(r+1)

for ans_i in ans:
    print(ans_i)

Measured memory_bytes: 263804
n,k = map(int, input().split())
a = list(map(int, input().split()))
d = {}
for num,i in enumerate(a):
  d[num+1] =  i
t = {}
t_ = {}
i = 1
j = 0
while(i not in t):
  t[i] = j
  t_[j] = i
  i = d[i]
  j += 1
a = 1
b = t[i]
if k < b+1:
  ans = t_[k]
else:
  k = (k-b)%(len(t.keys())-b)
  ans = t_[b+k]
print(ans)

Measured memory_bytes: 72696
#include <bits/stdc++.h>
using namespace std;
#define repl(i, l, r) for (ll i = (l); i < (r); i++)
#define rep(i, n) repl(i, 0, n)
#define fi first
#define se second
#define all(x) (x).begin(), (x).end()
#define CST(x) cout << fixed << setprecision(x)
using ll = long long;
const ll MOD = 1000000007;
const int inf = 1e9 + 10;
const ll INF = 4e18 + 10;
const int dx[9] = {1, 0, -1, 0, 1, -1, -1, 1, 0};
const int dy[9] = {0, 1, 0, -1, 1, 1, -1, -1, 0};
int main() {
	cin.tie(0);
	cout.tie(0);
	ios::sync_with_stdio(false);

	ll n, m, k;
	cin >> n >> m >> k;
	ll a[n + 1], b[m + 1];
	a[0] = b[0] = 0;
	rep(i, n) {
		cin >> a[i + 1];
		a[i + 1] += a[i];
	}
	rep(i, m) {
		cin >> b[i + 1];
		b[i + 1] += b[i];
	}
	ll ans = 0;
	rep(i, n + 1) {
		if (k < a[i]) continue;
		ll now = i - 1 + upper_bound(b, b + m + 1, k - a[i]) - b;
		ans = max(ans, now);
	}
	cout << ans << endl;
	return 0;
}

Measured memory_bytes: 6776
import java.util.*;

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

    long ans = 0;
    for(int i=1; i<=N; i++){
        if(i%3!=0 && i%5!=0){
            ans += i;   
        }
    }        
    System.out.println(ans);
    }
}

Measured memory_bytes: 36376
n,m=map(int,input().split())
from collections import defaultdict
d=defaultdict(list)
py=[]
for i in range(m):
    p,y=map(int,input().split())
    d[p].append(y)
    py.append((p,y))

for k in d.keys():
    d[k]=sorted(d[k])

from bisect import bisect_right
for i in range(m):
    p=py[i][0]
    y=py[i][1]
    x=bisect_right(d[p], y)
    print('0'*(6-len(str(p)))+str(p)+'0'*(6-len(str(x)))+str(x))


Measured memory_bytes: 86744
import java.util.*;
import java.io.*;
import java.math.*;
 
public class Main {
    public static void main(String[] args) throws Exception {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int[][] tb = new int[10][10];
        for(int i = 1; i <= n; i++){
            String tmp = String.valueOf(i);
            int t = Character.getNumericValue(tmp.charAt(0));
            int b = Character.getNumericValue(tmp.charAt(tmp.length()-1));
            tb[t][b]++;
        }
        int ans = 0;
        for(int i = 1; i <= n; i++){
            String tmp = String.valueOf(i);
            int t = Character.getNumericValue(tmp.charAt(0));
            int b = Character.getNumericValue(tmp.charAt(tmp.length()-1));
            ans += tb[b][t];
        }
        System.out.println(ans);
    }
}

Measured memory_bytes: 39476
import itertools
import math

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

al = [i for i in range(N)]
bl = [i for i in range(M)]

mod = (pow(10,9) + 7)
result = 0
if abs(N - M) > 1:
    result = 0
else:
    a_perm = math.factorial(N) % mod
    b_perm = math.factorial(M) % mod
    if abs(N - M) == 1:
        result = (a_perm * b_perm) % mod
    elif abs(N - M) == 0:
        result = ((a_perm * b_perm) * 2) % mod

print(result)



Measured memory_bytes: 12088
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MOD=1e9+7;
const int INF=1e9;
const ll LINF=1e18;

int n,m,d[1000],len=INF;
vector<int> e[1000],ans;

bool dfs(int v){
    bool flag=false;
    for(int u:e[v]){
        if(d[u]==0&&d[v]+1<len){
            len=d[v]+1;
            ans.clear();
            flag=true;
        }
        if(d[u]==INF){
            d[u]=d[v]+1;
            if(dfs(u))flag=true;
        }
    }
    if(flag){
        ans.push_back(v+1);
        return true;
    }
    return false;
}

int main(){
    cin>>n>>m;
    for(int i=0;i<m;i++){
        int a,b;
        cin>>a>>b;
        --a,--b;
        e[a].push_back(b);
    }
    for(int i=0;i<n;i++){
        fill(d,d+n,INF);
        d[i]=0;
        dfs(i);
    }
    if(ans.empty())cout<<-1<<endl;
    else {
        cout<<ans.size()<<endl;
        for(int v:ans){
            cout<<v<<endl;
        }
    }
}


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

#define rep(inc, bgn, end) for (int inc = bgn; inc < end; ++inc)
#define repe(inc, bgn, end) for (int inc = bgn; inc <= end; ++inc)

using ll = long long;
using namespace std;

//  -d
int main() {
    int n;
    cin >> n;

    int mxval = 0;
    rep(i, 0, n) {
        int val = 0;
        cin >> val;
        if (val == mxval + 1) { mxval++; }
    }

    // そもそも1がない
    if (mxval == 0) {
        cout << -1 << endl;
        return 0;
    }

    cout << n - mxval << endl;
}



Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
int main(){
  int N;
  cin >>N;
  vector<int> A(N,0),B(N,0),X(N,0),Y(N,0);
  for(int i=0; i<2; i++){
    for(int j=0; j<N; j++){
      int a;
      cin >>a;
      if(i==0) A[j]=a;
      else B[j]=a;
    }
  }
  X=A, Y=B;
  for(int i=0; i<N-1; i++){
    X[i+1]+=X[i];
    Y[i+1]+=Y[i];
  }
  int ans=0, Z=X[N-1]+Y[N-1];
  for(int i=0; i<N; i++){
    if(i==0) ans=max(ans,X[i]+Y[N-1]);
    else ans=max(ans,X[i]+Y[N-1]-Y[i-1]);
  }
  cout <<ans <<endl;
}

Measured memory_bytes: 256
/*
题意：构造一棵树，使得每一个1<=i<=n满足有以下性质之一：1.割掉某一条边能形成大小为i的连通块，2. 割掉任意一条边都不能形成大小为i的连通块
割掉叶子一定能割出大小为1的连通块，若a能割出，则n-a也能割出 
从小到大对能割出来的连通块构造（类似huffman树），使得每一条割掉以后不是掉一个叶子的边都恰好对应所需，不够就使劲挂叶子。 
*/
#include<bits/stdc++.h>
using namespace std;
int n,now,sc;
const int N=100005; 
char s[N];
int main()
{
	scanf("%s",s+1);n=strlen(s+1);
	if (s[1]=='0'||s[n]=='1') return puts("-1"),0;
	for (int i=1;i<n;i++)
	  if (s[i]-'0'+s[n-i]-'0'==1) return puts("-1"),0;  
	now=sc=1;
	for (int i=1,j=0;i<=n;i++)
	  if (s[i]=='1')
	  {
	  	 for (int k=1;k<=i-j-1;k++) printf("%d %d\n",now,++sc);
		 printf("%d %d\n",now,++sc);now=sc;
	  	 j=i;
	  }
	while (sc<n) printf("%d %d\n",now,++sc);
	return 0;
}

Measured memory_bytes: 1536
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
#define LL long long
#define mod 1000000007
#define G() Cr=getchar()
int Xr;char Cr;
inline int rd(){   //读入优化
    Xr=0,G();
    while(Cr<'0'||Cr>'9')G();
    while(Cr>='0'&&Cr<='9')Xr=(Xr<<3)+(Xr<<1)+Cr-'0',G();
    return Xr;
}
int n;
int ans;
int main(){
    n=rd();
    ans+=n/500*1000;
    n%=500;
    ans+=n/5*5;
    cout<<ans;
}


Measured memory_bytes: 256
import java.util.Scanner;




public class Main {

    public static void main(String[] args) {

    	Main main=new Main();

    	main.run();

    }

    void run() {
    	Scanner sc=new Scanner(System.in);

    	String[] Odd=sc.next().split("");
    	String[] Even=sc.next().split("");

    	int length=Odd.length+Even.length;

    	StringBuilder result=new StringBuilder();

    	for(int i=0;i<Odd.length;i++) {
    		result.append(Odd[i]);

    		if(i<Even.length) {
    			result.append(Even[i]);
    		}
    	}
    	System.out.println(result.toString());
    }

}



Measured memory_bytes: 23892
n,c,k=map(int,input().split())
t=[]
for i in range(n):
  T=int(input())
  t.append(T)

t.sort()
ans=1
pas=0
depa=t[0]+k

for i in range(n):
  if c==pas or depa<t[i]:
    ans+=1
    pas=1
    depa=t[i]+k
  else:
    pas+=1
print(ans)


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

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

    int x,y,c=0;

    cin>>x>>y;
    rep(i,x+1){
        if(y==4*x-2*i){
            ++c;
        }
    }
    if(c==0) cout<<"No"<<endl;
    else cout<<"Yes"<<endl;

    return 0;
}

Measured memory_bytes: 3652
#include <bits/stdc++.h>
#define rep(i,n) for(int i = 0; i < n; i++)
#define per(i,n) for(int i = n-1; i >= 0; i--)
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
typedef pair<long long, long long> Pll;
typedef vector<int> vi;
typedef vector<ll> vll;

int main() {
  int n, k;
  cin >> n >> k;
  vi a(n);
  rep(i,n) cin >> a.at(i);
  map<int, int> mp;
  rep(i,n) mp[a.at(i)]++;
  vi vec;
  for(auto p : mp) {
    int key = p.first;
    vec.push_back(mp[key]);
  }
  sort(vec.begin(), vec.end());
  int ans = 0;
  int m;
  m = vec.size();
  rep(i,m-k) ans += vec.at(i);
  cout << ans << endl;
  return 0;
}

Measured memory_bytes: 11516
function Main(input) {
	tmp = input.split("\n");
	tmp_in = tmp[0].split(" ");
	var n = parseInt(tmp_in[0]);
	var m = parseInt(tmp_in[1]);
	var r = 0;
	data = tmp[1].split(" ");
	var max = 0;
	for(var i = 0;i < data.length;i++){
		max += parseInt(data[i]);
	}
	var min = max/(4*m);
	for(var i = 0;i < data.length;i++){
		if(data[i] >= min){
			r++;
		}
	}
	if(r >= m){
		console.log("Yes");
	}else{
		console.log("No");
	}
}

Main(require("fs").readFileSync("/dev/stdin", "utf8"));

Measured memory_bytes: 21064
#include <stdio.h>

int main(void) {
    int i, j, h, b, num[2][4];
    while(~scanf("%d", &num[0][0])) {
        for(i = 1; i < 8; i++) {
            scanf("%d", &num[i / 4][i % 4]);
        }
        for(i = h = 0; i < 4; i++) {
            num[0][i] - num[1][i] || h++;
        }
        for(i = b = 0; i < 4; i++) {
            for(j = 0; j < 4; j++) {
                num[0][i] - num[1][j] || b++;
            }
        }
        printf("%d %d\n", h, b - h);
    }
    return 0;
}

Measured memory_bytes: 596
using System;
using System.Collections.Generic;
class Program
{
	static void Main(string[] args)
	{
      var l = new List<int>();
		// 整数の入力
		// スペース区切りの整数の入力
		string[] input = Console.ReadLine().Split(' ');
		int b = int.Parse(input[0]);
		int c = int.Parse(input[1]);
      int d = int.Parse(input[2]);
		// 文字列の入力
		//出力
      int a=b;
      if(b>c){a=c;}
      for(int i=a;i>=1;i--){
      	if(b%i==0 && c%i==0){
      		l.Add(i);
        }
      }
      int p = l[d-1];
		Console.WriteLine(p);
	}
}

Measured memory_bytes: 13140
/**
 *　　　　　　  　　┏┓　　 　┏┓
 * 　　　　　  　　┏┛┗━━━━━━━┛┗━━━┓
 * 　　　　　  　　┃　　　　　　　┃ 　
 * 　　　　　  　　┃　　　━　　 　┃
 * 　　　　　  　　┃　＞　　　＜　┃
 * 　　　　　  　　┃　　　　　　　┃
 * 　　　　　  　　┃...　⌒　... 　┃
 * 　　　　　　  　┃              ┃
 * 　　　　　　  　┗━┓          ┏━┛
 * 　　　　　　　　　┃          ┃　Code is far away from bug with the animal protecting　　　　　　　　　　
 * 　　　　　　　　　┃          ┃   神兽保佑,代码无bug
 * 　　　　　　　　　┃          ┃　　　　　　　　　　　
 * 　　　　　　　　　┃          ┃  　　　　　　
 * 　　　　　　　　　┃          ┃
 * 　　　　　　　　　┃          ┃　　　　　　　　　　　
 * 　　　　　　　　　┃          ┗━━━┓
 * 　　　　　　　　　┃              ┣┓
 * 　　　　　　　　　┃              ┏┛
 * 　　　　　　　　　┗┓┓┏━━━━━━━━┳┓┏┛
 * 　　　　　　　　　　┃┫┫       ┃┫┫
 * 　　　　　　　　　　┗┻┛       ┗┻┛
 */ 
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<map>
#include<vector>
#define mem(a,b) memset(a,b,sizeof(a))
#define ll long long
#define inf 1000000000
#define maxn 1005
#define maxm 100005
#define eps 1e-10
#define for0(i,maxn) for(int i=1;i<=(maxn);++i)
#define for1(i,maxn) for(int i=1;i<=(maxn);++i)
#define for2(i,x,y) for(int i=(x);i<=(y);++i)
#define for3(i,x,y) for(int i=(x);i>=(y);--i)
#define mod 1000000007
using namespace std;
inline int read()
{
    int x=0,f=1;char ch=getchar();
    while(ch<'0'||ch>'9') {if(ch=='-') f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9') {x=10*x+ch-'0';ch=getchar();}
    return x*f;
}
int main()
{
    int a,b,c;
    a=read();b=read();c=read();
    int x[3];
    x[0]=abs(a-b);x[1]=abs(a-c);x[2]=abs(b-c);
    sort(x,x+3);
    cout<<x[0]+x[1]<<endl;
}

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

int n, m, p[200005], ans = 1;

int pa(int x){
    if (p[x] < 0) return x;
    return p[x] = pa(p[x]);
}

int main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cin >> n >> m;
    memset(p, -1, sizeof(p));
    for (int i = 0, a, b; i < m; i++){
        cin >> a >> b;
        int x = pa(a), y = pa(b);
        if (x != y){
            p[x] += p[y];
            p[y] = x;
            ans = max(ans, -p[x]);
        }
    }
    cout << ans << "\n";
}


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

int main() {
  int a,b,c,d,e;
  cin>>a>>b>>c>>d>>e;
  int f=min(min(min((a-1)%10,(b-1)%10),min((c-1)%10,(d-1)%10)),(e-1)%10); 
  int time=0;
  time+=(10-a%10)%10+a+(10-b%10)%10+b+(10-c%10)%10+c+(10-d%10)%10+d+(10-e%10)%10+e;
  cout<<time-(10-(f+1))<<endl;
}

Measured memory_bytes: 256
#include <iostream>
#include <vector>
#include <string>
#include <stack>
#include <map>
#include <algorithm>
#include <iomanip>
#include <cmath>
#include <queue>
using namespace std;

#define MAXV 1000000000
#define MOD 1000000007
using ll = long long;

bool operator<(const pair<int, int>& a, const pair<int, int>& b) {
	if (a.first == b.first) {
		return a.second < b.second;
	}
	return a.first < b.first;
}

struct Node {
	int to, cost;
};

int main() {

	int N;
	while (cin >> N) {
		if (N == 0) break;

		int m; cin >> m;
		vector<vector<Node>> G(110);
		int mincost[110];
		bool used[110];

		for (int i = 0; i < N; ++i) {
			mincost[i] = 1e9;
			used[i] = false;
		}
		for (int i = 0; i < m; ++i) {
			int a, b, d;
			scanf("%d,%d,%d", &a, &b, &d);

			Node e1, e2;
			e1.to = a;
			e2.to = b;
			
			d /= 100;
			d--;
			e1.cost = e2.cost = d;

			G[a].push_back(e2);
			G[b].push_back(e1);
		}

		priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> q;

		mincost[0] = 0;

		q.push({ 0, 0 }); //最小コスト 現在位置
		int res = 0;

		while (q.size()) {
			pair<int, int> p = q.top();
			q.pop();

			int v = p.second;
			if (used[v]) continue;

			used[v] = true;
			res += p.first;
			for (int i = 0; i < G[v].size(); ++i) {
				Node e = G[v][i];
				if (used[e.to]) continue;
				q.push({ e.cost, e.to });
			}
		}
		cout << res << endl;
	}
	int a; cin >> a;
}


Measured memory_bytes: 3300
#include<stdio.h>
#define M 105

int Ad[M][M];
int d[M];
int Q[M];
char color[M];
void enqueue(int);
int dequeue(void);
int n,head=0,tail=0;

int main(){

  int i,j,m,u,k;

  scanf("%d",&n);

  for(i=1;i<M;i++){
    Q[i]=0;
    for(j=1;j<M;j++){
      Ad[i][j]=0;
    }
  }

  for(i=1;i<=n;i++){
    scanf("%d%d",&u,&k);
    color[i]='W';
    d[i]=-1;
    if(k!=0){for(j=1;j<=k;j++){
      scanf("%d",&Ad[u][j]);
      }
    }
  }
     
  color[1]='G';
  d[1]=0;
  enqueue(1);

  while(head!=tail){
    u = dequeue();
    m = 1;
    while(Ad[u][m]!=0){
      if(color[Ad[u][m]]=='W'){
	color[Ad[u][m]]='G';
        d[Ad[u][m]]=d[u]+1;
        enqueue(Ad[u][m]);}
        m++;
      }
    color[u]='B';
  }
  
  for(i=1;i<=n;i++){
    printf("%d %d\n",i,d[i]);
  }
 
  return 0;
}


void enqueue(int y){
  Q[tail]= y;
  if(tail+1==n-1) tail=0;
  else tail++;
}

int dequeue(void){
  int x;
  x = Q[head];
  if(head+1==n-1) head=0;
  else head++;

  return x;
}

Measured memory_bytes: 644
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[][] wsv = new long[n][3];
		for (int i = 0; i < n; i++) {
			long w = sc.nextLong();
			long s = sc.nextLong();
			long v = sc.nextLong();
			wsv[i][0] = w;
			wsv[i][1] = s;
			wsv[i][2] = v;
		}

		Arrays.sort(wsv, Comparator.comparingLong(a -> a[0] + a[1]));

		long[] dp = new long[20001];
		for (int i = 1; i <= n; i++) {
			for (int j = (int) wsv[i - 1][1]; j >= 0; j--) {
				dp[j + (int) wsv[i - 1][0]] = Math.max(
						dp[j + (int) wsv[i - 1][0]], dp[j] + wsv[i - 1][2]);
			}
		}

		long answer = 0;
		for (int i = 0; i < 20001; i++) {
			answer = Math.max(answer, dp[i]);
		}

		System.out.println(answer);
	}
}


Measured memory_bytes: 30648
x,k,d = map(int,input().split())

x = abs(x)
syo = x//d
kk = k - syo
xx = x - syo*d

if k <= syo:
    ans = x-d*k
else:
    if kk%2 == 0:
        ans = xx
    else:
        ans = abs(xx-d)

print(ans)

Measured memory_bytes: 9144
#include <iostream>
using namespace std;
#include <string>
#include <vector>
#include <algorithm>
#include <map>
typedef long long ll;
#include <cmath>
#include <iomanip>
#include <set>
#include <queue>
const int MOD = 1000000007;


int main(int argc, char *argv[])
{
	int n,m;
	cin>>n>>m;
	vector<int> cnt(n+1),top(n+1);
	for (int i=1;i<=n;i++)
	{
		cnt[i]=1;
		top[i]=i;
	}
	int ret=1;
	for (int i=0;i<m;i++)
	{
		int a,b;
		cin>>a>>b;
		while (a!=top[a]) a=top[a];
		while (b!=top[b]) b=top[b];
		
		if (a>b) {int c=b;b=a;a=c;}		
		if (a!=b)
		{
			cnt[a]+=cnt[b];
			if (cnt[a]>ret) ret=cnt[a];
			top[b]=a;
		}
	}
	/*
	for (int i=1;i<=n;i++)
	{
		cerr<<i<<":"<<top[i]<<":"<<cnt[i]<<endl;
	}
	for (int i=1;i<=n;i++)
	{
		if (top[i]==i)
		cerr<<i<<":"<<top[i]<<":"<<cnt[i]<<endl;
	}
	*/
	cout<<ret<<endl;
}


Measured memory_bytes: 4832
n = input()
cnt = len(n) -1
for i in range(2 ** cnt):
    op = ["-"] * cnt
    for j in range(cnt):
        if ((i >> j) & 1):
            op[cnt - 1 - j] = "+"

    formula = ""
    for p_n,p_o in zip(n,op+[""]):
        formula += (p_n + p_o)
    if eval(formula) == 7:
        print(formula + "=7")
        break


Measured memory_bytes: 3060
#include<bits/stdc++.h>
using namespace std;
int main()
{
	string s;
	cin>>s;
	for(int i=0;i<s.size();i++)
	{
		if(s[i]=='?')
			s[i]='D';
	}
	cout<<s<<"\n";
	return 0;
}

Measured memory_bytes: 3760
#include <algorithm>
#include <iostream>
#include <vector>
#include <utility>
using namespace std;
int main(){
  int N;
  cin >> N;
  long long int H[N], A[N], D[N], S[N], h, a, d, s, t, ans = 0LL;
  cin >> h >> a >> d >> s;
  for(int i = 0; i < N; ++i){
    cin >> H[i] >> A[i] >> D[i] >> S[i];
    A[i] -= d;
    A[i] = max(A[i],0LL);
    if(S[i] < s) ans -= A[i];
  }
  vector< pair<double, int> > V;
  for(int i = 0; i < N; ++i){
    if(a <= D[i] && A[i] > 0){
      cout << -1 << endl;
      return 0;
    }
    if(a > D[i]){
      t = (H[i]+a-D[i]-1)/(a-D[i]);
      V.push_back(make_pair(-(double)A[i]/t,i));
    }else{//A[i] == 0 && a == D[i]
      V.push_back(make_pair(0,i));
    }
  }
  sort(V.begin(),V.end());
  t = 0LL;
  for(int i = 0; i < N; ++i){
    //cout << V[i].first << " " << V[i].second << endl;
    int j = V[i].second;
    if(a != D[j]){
      t += (H[j]+a-D[j]-1)/(a-D[j]);
    }
    ans += t*A[j];
    //cout << t << "  " << ans << endl;
    if(ans >= h){
      cout << -1 << endl;
      return 0;
    }
  }
  if(ans >= h) cout << -1 << endl;
  else cout << ans << endl;
  return 0;
}

Measured memory_bytes: 5052
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
ll ans = -(1LL<<60);

int main()
{
ll n , k ;
cin >> n >> k ;
vector<ll> p(n);
rep(i, n){
  cin >> p[i];
}
vector<ll> c(n);
rep(i, n){
  cin >> c[i];
}
vector<vector<ll>> v;
vector<bool> see(n,0);
ll cnt=0;
rep(i,n){
  if(see[i])continue;
  ll now=i;
  vector<ll> t;
  v.push_back(t);
  while(!see[now]){
    see[now]=1;
    v[cnt].push_back(c[p[now]-1]);
    now=p[now]-1;
  }
  cnt++;
}
rep(i,cnt){
  ll tmp=0,tmp2=-(1LL<<60);
  ll su=accumulate(v[i].begin(), v[i].end(), 0LL);
/*   if(su>0){
    tmp+=su*(k/v[i].size());
    k=k%v[i].size();
  } */
  ll kk=min((ll)v[i].size(),k);
  rep(j,v[i].size()){
    ll q=0;
    rep(l,kk){
      q+=v[i][(j+l)%v[i].size()];
      tmp2=max(tmp2,q+max(0LL,(ll)(su*((k-l-1)/v[i].size()))));
    }
  }
  ans=max(ans,tmp2);
}
cout << ans << endl;
return 0;
}

Measured memory_bytes: 3816
#include <bits/stdc++.h>
#include <cxxabi.h>
using namespace std;
using ll=long long;
using vi=vector<int>;
using vvi=vector<vi>;
using pii=pair<int,int>;
#define rep(i,n) range(i,0,n)
#define range(i,a,n) for(int i=a;i<n;i++)
#define all(a) a.begin(),a.end()
#define LINF    ((ll)1ll<60)
#define INF     ((int)1<<30)
#define EPS     (1e-9)
#define MOD     (1000000007)
template<class S,class T>ostream& operator<<(ostream&os,pair<S,T>p){os<<"["<<p.first<<", "<<p.second<<"]";return os;};
template<class S>auto&operator<<(ostream&os,vector<S>t){bool b=1;for(auto s:t)os<<(exchange(b,0)||strlen(abi::__cxa_demangle(typeid(S).name(),0,0,0))>20?"":" ")<<s;return os<<endl;}

int main(){
	cin.tie(0);
   	ios::sync_with_stdio(false);
	
	int w,h;
	while(cin>>w>>h,w){
		vvi field(h+2,vi(w+2,-1));
		vector<pii> coor(2,{0,0});
		int n=1;
		range(i,1,h+1)range(j,1,w+1){
		    char c; cin>>c;
		    if(c=='.') field[i][j]=0;
		    if(c=='o') {field[i][j]=1;   coor[1]={i,j};}
		    if(c=='*') {field[i][j]=++n; coor.emplace_back(i,j);}
		}
		
		vvi dist(n,vi(n,INF));
		rep(i,n){
		    vvi isVisit(h+2,vi(w+2,-1));
		    queue<tuple<int,int,int>> q;
		    q.emplace(0,coor[i+1].first,coor[i+1].second);
		    while(!q.empty()){
		        int cost,y,x; tie(cost,y,x)=q.front(); q.pop();
		        if(isVisit[y][x]!=-1) continue;
		        isVisit[y][x]=cost;
		        int v[]={1,0,-1,0};
		        rep(j,4){
		            int ny=y+v[j], nx=x+v[j^1];
		            if(ny%(h+1)==0 or nx%(w+1)==0 or field[ny][nx]==-1) continue;
		            q.emplace(cost+1,ny,nx);
		        }
		    }
		    rep(j,n){
		        dist[i][j]=dist[j][i]=isVisit[coor[j+1].first][coor[j+1].second];
		    }
		}
		
		vvi dp(1<<n, vi(n,INF));
		dp[1][0]=0;
		
		range(s,1,1<<n) {    //集合sに対して
		    rep(curr,n) if(dp[s][curr]!=INF){  //最後に訪れた頂点に対して
		        rep(next,n) if((s&(1<<next))==0 and dist[curr][next]!=-1){    //次に行く頂点に対して
		            dp[s|(1<<next)][next]=min(dp[s|(1<<next)][next], dp[s][curr]+dist[curr][next]);
		            //cout<<"dp["<<bitset<10>(s|(1<<next)).to_string()<<"]["<<next<<"]="<<dp[s|(1<<next)][next]<<endl;
		        }
		    }
		}
		
		int ans=*min_element(all(dp[(1<<n)-1]));
		if(ans==INF) cout<<-1<<endl;
		else cout<<ans<<endl;
	}
	return 0;
}



Measured memory_bytes: 3168
s = input()
l = len(s)
ans = []
for i in range(l):
	if s[i]=='0':
		ans.append('0')
	elif s[i]=='1':
		ans.append('1')
	else:
		if ans!=[]:
			ans.pop(-1)

n = len(ans)
for i in range(n-1):
	print(ans[i], end="")
print(ans[n-1])


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

using namespace std;

typedef long long lli;
typedef double lld;
typedef vector<lli> vll;
typedef vector<bool> vbl;
typedef vector<double> vdl;
typedef vector<vector<lli>> mat;
typedef vector<vdl> mad;
// typedef unordered_map<lli,unordered_map<lli,lli>> graph;
typedef complex<double> cmp;
typedef vector<cmp> vcl;

const lli e5 = 1 << 20;
const lli mod = 998244353;
const lli e3 = 1 << 12;

lli n;
lli a[e5];
vll graph[e5];

void error(lli x=0){
  cerr << x << endl;
  cout << "NO" << endl;
  exit(0);
}

bool is_possible(lli a,lli b){
  if(b-a < 0) return false;
  return true;
}

lli dfs(lli x,lli prev){
  if(graph[x].size() == 1){
    return a[x];
  }else{
    vll mem;
    for(lli y : graph[x]){
      if(y == prev) continue;
      mem.push_back(dfs(y,x));
    }
    lli b = accumulate(mem.begin(),mem.end(),0ll);
    if(!is_possible(a[x],b)) error(1);
    lli b_pair = (b-a[x])*2;
    lli b_above = 2*a[x]-b;
    lli hoge = 0;
    for(lli m : mem){
      hoge += min(m,b_pair/2);
    }
    if(hoge < b_pair) error(2);
    return b_above;
  }
}

int main(){
  cin >> n;
  for(lli i = 1;i <= n;i++) cin >> a[i];
  if(n==2){
    if(a[1]==a[2]) cout << "YES" << endl;
    else error();
    return 0;
  }
  for(lli i = 0;i < n-1;i++){
    lli from,to;
    cin >> from >> to;
    graph[from].push_back(to);
    graph[to].push_back(from);
  }
  lli res_path;
  for(lli i = 1;i <= n;i++){
    if(graph[i].size() > 1){
      res_path = dfs(i,0);
      break;
    }
  }
  if(res_path != 0) error();
  cout << "YES" << endl;

}


Measured memory_bytes: 38016
#include <algorithm>
#include <climits>
#include <cmath>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <unordered_map>
#include <vector>

const int mod = 1e9 + 7;
const int kmax = 510000;

long long fact[kmax], fact_inv[kmax], inv[kmax];

int gcd(int a, int b)
{
    if (!b) {
        return a;
    }

    return gcd(b, a % b);
}

void init_comb()
{
    fact[0] = fact[1] = 1;
    fact_inv[0] = fact_inv[1] = 1;
    inv[1] = 1;
    for (int i = 2; i < kmax; i++) {
        fact[i] = fact[i - 1] * i % mod;
        inv[i] = mod - inv[mod % i] * (mod / i) % mod;
        fact_inv[i] = fact_inv[i - 1] * inv[i] % mod;
    }
}

long long comb(int n, int r)
{
    if (n < r) {
        return 0;
    }

    if (n < 0 || r < 0) {
        return 0;
    }

    return fact[n] * (fact_inv[r] * fact_inv[n - r] % mod) % mod;
}

template <typename T, T N>
class UnionFind {
    T parent_[N];
    T rank_[N];
    T size_[N];

public:
    UnionFind();
    T Root(T idx);
    bool IsSame(T x, T y);
    void Unite(T x, T y);
    T GetSize(T idx);
};

template <typename T, T N>
UnionFind<T, N>::UnionFind()
{
    for (T i = 0; i < N; i++) {
        parent_[i] = i;
        rank_[i] = 0;
        size_[i] = 1;
    }
}

template <typename T, T N>
T UnionFind<T, N>::Root(T idx)
{
    return parent_[idx] == idx ? idx : parent_[idx] = Root(parent_[idx]);
}

template <typename T, T N>
bool UnionFind<T, N>::IsSame(T x, T y)
{
    return Root(x) == Root(y);
}

template <typename T, T N>
void UnionFind<T, N>::Unite(T x, T y)
{
    x = Root(x);
    y = Root(y);
    if (x == y) {
        return;
    }

    if (rank_[x] < rank_[y]) {
        parent_[x] = y;
        size_[y] += size_[x];
    } else {
        parent_[y] = x;
        size_[x] += size_[y];
        if (rank_[x] == rank_[y]) {
            rank_[x]++;
        }
    }
}

template <typename T, T N>
T UnionFind<T, N>::GetSize(T idx)
{
    return size_[Root(idx)];
}

long long pow_mod(long long n, long long p, long long m)
{
    if (p == 0) {
        return 1;
    }

    if (p % 2 == 0) {
        long long t = pow_mod(n, p / 2, m);
        return t * t % m;
    }
    return n * pow_mod(n, p - 1, m) % mod;
}

long long nCr_mod(long long n, long long r)
{
    long long x = 1;
    for (long long i = n - r + 1; i <= n; i++) {
        x *= i;
        x %= mod;
    }

    long long a = 1;
    for (long long i = 1; i <= r; i++) {
        a *= i;
        a %= mod;
    }

    long long y = pow_mod(a, mod - 2, mod) % mod;

    return x * y % mod;
}

int main()
{
    long long a, b, c, k;
    std::cin >> a >> b >> c >> k;

    long long ans = std::min(a, k);
    k -= a + b;

    if (k > 0) {
        ans -= k;
    }

    std::cout << ans << std::endl;
}


Measured memory_bytes: 3640
#include <iostream>
#include <algorithm>
#include <iomanip>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <numeric>
#include <bitset>
#include <cmath>

static const int MOD = 1000000007;
using ll = long long;
using u32 = uint32_t;
using namespace std;

template<class T> constexpr T INF = ::numeric_limits<T>::max()/32*15+208;

int main() {
    int n, m, t;
    cin >> n >> m >> t;
    vector<int> v(n);
    for (auto &&i : v) scanf("%d", &i);
    int ans = max(0, v.front()-m)+max(0, t-v.back()-m);
    for (int i = 0; i < n-1; ++i) {
        ans += max(0, v[i+1]-v[i]-2*m);
    }
    cout << ans << "\n";
    return 0;
}


Measured memory_bytes: 3248
//                   ********** YATIN KWATRA **********

// AC BAKSHI RABBA

#include <bits/stdc++.h>
#define ll                      long long 
#define ld                      long double
#define ull                     unsigned long long 
#define FIO                     ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define endl                    "\n"
#define fo(i,a,b)               for(int i = a; i<=b ; i++)
#define rfo(i,a,b)              for(int i = a; i>=b ; i--)
#define vii                     vector<int>
#define vll                     vector<ll>
#define pq                      priority_queue
#define uom                     unordered_map                
#define all(v)                  v.begin(),v.end()
#define mp                      make_pair
#define pb                      push_back
#define pob                     pop_back
#define ff                      first
#define ss                      second
#define pii                     pair<int,int> 
#define pll                     pair<long,long>
#define mll                     map<ll,ll>
#define mii                     map<int,int>
#define vvii                    vector<vii>
#define vvll                    vector<vll>
#define mod                     1000000007
#define pi                      3.1415926535897932384626433832795

using namespace std;

void INPUT() 
{  
    #ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    #endif
}

/*
    
   -> Check For Overflows
   -> Out of Bounds
   -> Initialisations of global arrays and variables
   -> Its not Always Complex, Try to simplify
   -> write it down and try to solve
   -> Have Faith in urself and in ur approach
   -> If it seems totally out of reach, switch to next :)
   -> And Come Back later, if possible 
*/





void solve()
{ 
        ll n;
        cin >> n;
        
        vll v(n),val;
        
        fo(i,0,n-1) cin >> v[i];

        sort(all(v),greater<int>());

        val.pb(v[0]);

        int i = 1;


        while(val.size() < (n-1))
        {
          val.pb(v[i]);
          val.pb(v[i]);
          i++;
        }

        ll ans = 0LL;

        fo(i,0,n-2) ans += val[i];
        cout << ans << endl;
       
}
int main() {
    
    FIO
    INPUT();
    
    int t;
    t = 1;
    //cin >> t;
    
    while(t--)
    {
       solve();
    }
    return 0;
}


Measured memory_bytes: 6860