akhauriyash/Code-Regression huggingface dataset:
import sys,math
try:sys.stdin,sys.stdout=open('input.txt','r'),open('out.txt','w')
except:pass
from sys import stdin,stdout;mod=int(1e9 + 7);from statistics import mode
from collections import *;from math import ceil,floor,inf,factorial,gcd,log2,sqrt,log
ii1=lambda:int(stdin.readline().strip())
is1=lambda:stdin.readline().strip()
iia=lambda:list(map(int,stdin.readline().strip().split()))
isa=lambda:stdin.readline().strip().split()
# print('{:.3f}'.format(1),round(1.123456789,4))
# sys.setrecursionlimit(500000)
def lcm(a,b): return (a*b)//gcd(a,b)
def setbits(n):return bin(n).count('1')
def resetbits(n):return bin(n).count('0')
def modinv(n,p):return pow(n,p-2,p)
def ncr(n,r):
    num,den=1,1;r=min(n,n-r)
    for i in range(r):num*=(n-i);den*=(i+1)
    return num//den
def ncr_p(n, r, p):
    num,den=1,1;r=min(r,n-r)
    for i in range(r):num = (num * (n - i)) % p ;den = (den * (i + 1)) % p
    return (num * modinv(den,p)) % p
def isPrime(num) :
    if num<=1:return False
    if num==2 or n==3:return True
    if (num % 2 == 0 or num % 3 == 0) :return False
    m = int(num**0.5)+1
    for i in range(5,m,6):
        if (num % i == 0 or num % (i + 2) == 0) :return False
    return True
def bin_search(s, low, high, val):
    while low <= high:
        mid = low + (high - low) // 2;
        if s[mid] == val:return mid
        elif s[mid] < val:low = mid + 1
        else:high = mid - 1
    return -1
def sumofdigit(num):
    count=0;
    while num : count+=num % 10;num //= 10;
    return count;
def inputmatrix():
    r,c=iia();mat=[0]*r;
    for i in range(r):mat[i]=iia();
    return r,c,mat;
def prefix_sum(n,s):
    for i in range(1,n):s[i]+=s[i-1]
    return s;
def binomial(n, k):
    if 0 <= k <= n:
        ntok = 1;ktok = 1
        for t in range(1, min(k, n - k) + 1):ntok *= n;ktok *= t;n -= 1
        return ntok // ktok
    else:return 0
def divisors(n):
    res = [];
    for i in range(1,ceil(sqrt(n))+1):
        if n%i == 0:
            if i==n//i:res.append(i)
            else:res.append(i);res.append(n//i)
    return res;
# code here

a,b,c,d=iia()
ans=max(a*c,a*d,b*c,b*d)
print(ans)




Measured memory_bytes: 77468
#include <bits/stdc++.h>
using namespace std;
#define  rep(i, n) for(int i = 0; i < n; i++) 
#define  all(x) (x).begin(),(x).end()     // 昇順ソート
#define  rall(v) (v).rbegin(), (v).rend() // 降順ソート
#define  INF 1LL << 60
typedef long long int LL;
typedef long long int ll;
#define pll pair<ll, ll>
#define F first
#define S second
const int MOD = 1000000007;
template<class T> bool chmax(T &a, const T &b) { if (a<b) { a=b; return true; } return false; }
template<class T> bool chmin(T &a, const T &b) { if (a>b) { a=b; return true; } return false; }
//sort(all(x))とするとソートできるよ
// 10^x は pow(10,(x)) 
// 任意のlogは　対数の底の変換を使う    log(N) / log(任意の底)

int main(){
    string S;cin >> S;
    int Q;cin >> Q;
    deque<char> str;
    bool pos = true; // true で順　false で逆
    rep(i,S.size()){
        str.push_back(S[i]);
    }


    rep(i,Q){
        int t;cin >> t;
        if(t == 1){
            if(pos)pos = false;
            else pos = true;
        }
        else if(t == 2){
            int k;cin >> k;
            char c;cin >> c;
            if(k == 1){
                if(pos)str.push_front(c);
                else str.push_back(c);
            }
            else if(k == 2){
                if(pos)str.push_back(c);
                else str.push_front(c);
            }
        }
    }

    if(pos){
        rep(i,str.size())cout << str[i];;
    }
    else for(int i = str.size()-1;i >= 0;--i){
        cout << str[i];
    }
    cout << endl;
}


Measured memory_bytes: 896
#include <bits/stdc++.h>
using namespace std;
using Graph = vector<vector<int>>;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
const long long X=1000000000+7;

int main() {
  int n,ans=0;
  cin>>n;
  rep(i,5){
    if(i%2==0){
      if(n<pow(10,i+1)){
        ans+=n-pow(10,i)+1;
        break;
      }
      else ans+=pow(10,i+1)-pow(10,i);
    }
    else{
      if(n<pow(10,i+1)) break;
    }
  }
  cout<<ans<<endl;
}

Measured memory_bytes: 256
from sys import stdin, setrecursionlimit
input = stdin.buffer.readline

from collections import defaultdict

H, W, M = map(int, input().split())
hw = [list(map(int, input().split())) for _ in range(M)]

cnt = defaultdict(int)
cnt_h = defaultdict(int)
cnt_w = defaultdict(int)

for h, w in hw:
    cnt[(h, w)] += 1

for h, w in hw:
    cnt_h[h] += 1
    cnt_w[w] += 1
cnt_h = list(cnt_h.items())
cnt_w = list(cnt_w.items())
cnt_h.sort(key = lambda x: -x[1])
cnt_w.sort(key = lambda x: -x[1])

max_h = []
max_w = []
for h, n in cnt_h:
    if n == cnt_h[0][1]:
        max_h.append(h)
for w, n in cnt_w:
    if n == cnt_w[0][1]:
        max_w.append(w)

answer = cnt_h[0][1] + cnt_w[0][1]
for mh in max_h:
    for mw in max_w:
        if cnt[(mh, mw)] == 0:
            print(answer)
            exit()

print(answer - 1)

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

using namespace std;

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

#define ALL(obj)	(obj).begin(),(obj).end()

typedef unsigned int 			uint;
typedef unsigned long long int	ull;
typedef long long int 			ll;

typedef pair<int,int>			P;
typedef vector<int>				V;
typedef vector<V>				VV;

#undef _P
#define _P(...) (void)printf(__VA_ARGS__)
//#define LINF		(ll)4223372036854775807
#define INF 		1010101010
				  //2147483647 (Max int)
#define MINF		-2000000007
#define MAX 		200050

const ll LINF = (ll)1e18+7;
const int MOD = 1e9+7;
// テンプレ終了
//====================================================================//

//
int main(){
	// 入力
	int A,B,Q;
	cin >> A >> B >> Q;
	vector<ll> s(A+2),t(B+2);
	REP(i,A)cin>>s[i+1];
	REP(i,B)cin>>t[i+1];
	s[0]=-LINF;s[A+1]=LINF;
	t[0]=-LINF;t[B+1]=LINF;
	//sort(ALL(s));
	//sort(ALL(t));

	REP(i,Q){
		ll x;cin >> x;
		int s0 = lower_bound(ALL(s), x) - s.begin();
		int t0 = lower_bound(ALL(t), x) - t.begin();
		ll ab = LINF;
		REP(j,2)REP(k,2){
			ab = min(ab,(ll)abs((ll)x-s[s0-j])+abs((ll)s[s0-j]-t[t0-k]));
			ab = min(ab,(ll)abs((ll)x-t[t0-k])+abs((ll)t[t0-k]-s[s0-j]));
		}
		cout << ab << endl;
	}


	return 0;
}




Measured memory_bytes: 2944
#include <bits/stdc++.h>
#define INF 5000000000000000000
#define ll long long
#define pll pair<ll, ll>
using namespace std;

//=============ford_fulkerson============================
ll MAX_V = 101;
struct edge {ll to, cap, rev;};
vector<vector<edge>> G(MAX_V);
vector<bool> used(MAX_V, false);

void add_edge(ll from, ll to, ll cap) {
  G.at(from).push_back((edge){to, cap, (ll)G.at(to).size()});
  G.at(to).push_back((edge){from, 0ll, (ll)G.at(from).size() - 1});
}

ll dfs(ll v, ll t, ll f) {
  if (v == t) {
    return f;
  }
  used.at(v) = true;
  for (ll i = 0; i < G.at(v).size(); ++i) {
    edge &e = G.at(v).at(i);
    if (!used.at(e.to) && e.cap > 0) {
      ll d = dfs(e.to, t, min(f, e.cap));
      if (d > 0) {
        e.cap -= d;
        G.at(e.to).at(e.rev).cap += d;
        return d;
      }
    }
  }
  return 0;
}

ll max_flow(ll s, ll t) {
  ll flow = 0;
  while (true) {
    used = vector<bool>(MAX_V, false);
    ll f = dfs(s, t, INF);
    if (f == 0) {
      return flow;
    }
    flow += f;
  }
}
//=================================================

int main() {
  ll V, E;
  cin >> V >> E;
  for (ll i = 0; i < E; ++i) {
    ll u, v, c;
    cin >> u >> v >> c;
    add_edge(u, v, c);
  }
  cout << max_flow(0, V - 1) << "\n";
}



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

using namespace std;
using ll=long long;
using ull=unsigned long long;
using pii=pair<int,int>;

#define INF LONG_MAX
#define MOD 1000000007
#define rng(a) a.begin(),a.end()
#define rrng(a) a.end(),a.begin()

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

  int N,M;
  cin>>N>>M;
  vector<pii>A(M);
  for(int i=0;i<M;i++)cin>>A[i].second>>A[i].first;
  sort(rng(A));
  int memo=-1;
  int ans=0;
  for(int i=0;i<M;i++){
    if(memo<A[i].second)memo=A[i].first-1,ans++;
  }
  cout<<ans<<endl;
  return 0;
}


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

#define rep(i,n) for(int i = 0;i < (n);i++)
#define N_Contest (26)

using namespace std;
using ll = long long;
using P = pair<int,int>;


void calc_primefactorization(long long in, vector<vector<long long> >& out){
	
	long long N = in, Ntmp;
	long long p,e,p_init = 2;
	vector<long long> out_tmp(2);
	
	if( in == 1 ){
		out_tmp[0] = 1;
		out_tmp[1] = 1;
	}
	else{
		while( 1 ){

			Ntmp = N;
				
			p = N;
			e = 1;

			for(long long i = p_init;i * i <= N;i++){
						
				if( Ntmp % i == 0 ){
					e = 0;
					while( 1 ){
						if( Ntmp % i != 0 ){
							break;
						}
						else{
							Ntmp = Ntmp / i;
							e++;
						}
					}
					p = i;
					break;
				}
				
			}
			
			out_tmp[0] = p;
			out_tmp[1] = e;
			out.push_back(out_tmp);
			
			for(int i = 0;i < e;i++){
				N = N / p;
			}
			
			if( N == 1 ){
				break;
			}
			
		}
	}
	
}


int main(){
	
	int N;
	
	cin >> N;
	
	vector<string> S(N), Sref(4);
	vector<int> Count(4);
	Sref[0] = "AC";
	Sref[1] = "WA";
	Sref[2] = "TLE";
	Sref[3] = "RE";
	
	for(int i = 0;i < N;i++){
		cin >> S[i];
		for(int j = 0;j < 4;j++){
			if( S[i] == Sref[j] ){
				Count[j]++;
				break;
			}
		}
	}
	
	for(int i = 0;i < 4;i++){
		cout << Sref[i] << " x " << Count[i] << endl;
	}
	
	
	return 0;
	
}


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

using namespace std;

int main(){
    string a,b,c;
    cin >> a >> b >> c;
    transform(a.begin(),a.begin()+1,a.begin(),::toupper);
    transform(b.begin(),b.begin()+1,b.begin(),::toupper);
    transform(c.begin(),c.begin()+1,c.begin(),::toupper);
    cout << a[0] << b[0] << c[0] << endl;
    return 0;
}


Measured memory_bytes: 256
def main():
    s = input()

    a = int(s[0])
    b = int(s[1])
    c = int(s[2])
    d = int(s[3])

    for i in range(2):
        tmp = 0
        if i % 2 == 0:
            tmp = a + b 
        else:
            tmp = a - b
        for j in range(2):
            if j % 2 == 0:
                hoge1 = tmp
                tmp += c
            else:
                tmp = hoge1
                tmp -= c
            for k in range(2):
                if k % 2 == 0:
                    hoge = tmp
                    tmp += d
                else:
                    tmp = hoge
                    tmp -= d
                if tmp == 7:
                    op1 = '+' if i % 2 == 0 else '-'
                    op2 = '+' if j % 2 == 0 else '-'
                    op3 = '+' if k % 2 == 0 else '-'
                    print(str(a)+op1+str(b)+op2+str(c)+op3+str(d)+'=7')
                    exit()
            

    # print(i,j,k,tmp)

if __name__ == '__main__':
    main()

Measured memory_bytes: 38256
from collections import deque

H, W, K = map(int, input().split())
sx, sy, tx, ty = map(lambda a: int(a) - 1, input().split())
INF = 10**18

A = [input() for _ in range(H)]

minDist = [[INF] * W for _ in range(H)]
minDist[sx][sy] = 0
que = deque([(0, sx, sy)])

while que:
    dist, nx, ny = que.popleft()
    if nx == tx and ny == ty:
        break

    for i in range(1, K + 1):
        if nx + i >= H:
            break
        if minDist[nx + i][ny] <= dist or A[nx + i][ny] == '@':
            break
        if minDist[nx + i][ny] > dist + 1:
            minDist[nx + i][ny] = dist + 1
            que.append((dist + 1, nx + i, ny))
    for i in range(1, K + 1):
        if nx - i < 0:
            break
        if minDist[nx - i][ny] <= dist or A[nx - i][ny] == '@':
            break
        if minDist[nx - i][ny] > dist + 1:
            minDist[nx - i][ny] = dist + 1
            que.append((dist + 1, nx - i, ny))
    for i in range(1, K + 1):
        if ny + i >= W:
            break
        if minDist[nx][ny + i] <= dist or A[nx][ny + i] == '@':
            break
        if minDist[nx][ny + i] > dist + 1:
            minDist[nx][ny + i] = dist + 1
            que.append((dist + 1, nx, ny + i))
    for i in range(1, K + 1):
        if ny - i < 0:
            break
        if minDist[nx][ny - i] <= dist or A[nx][ny - i] == '@':
            break
        if minDist[nx][ny - i] > dist + 1:
            minDist[nx][ny - i] = dist + 1
            que.append((dist + 1, nx, ny - i))

ans = minDist[tx][ty]
print(ans if ans < INF else -1)


Measured memory_bytes: 36712
#include<bits/stdc++.h>
#define INF 1e9
#define llINF 1e18
#define MOD 1000000007
#define pb push_back
#define mp make_pair 
#define F first
#define S second
#define ll long long
#define vi vector<ll>
#define vvi vector<vi>
#define BITLE(n) (1LL<<((ll)n))
#define SHIFT_LEFT(n) (1LL<<((ll)n))
#define SUBS(s,f,t) ((s).substr((f)-1,(t)-(f)+1))
#define ALL(a) (a).begin(),(a).end()
#define EPS 1e-15
using namespace std;
ll toint(string s){
  ll ret = 0;
  ll kake = 1;
  reverse(ALL(s));
  ll n = s.size();
  if(s.back() == '-')n--;
  for(int i=0;i<n;i++){
    ret += kake*(s[i] - '0');
    kake *= 10;
  }
  if(s.back() == '-')
    return -ret;
  return ret;
}
void solve(ll n){
  ll le = -INF-10;
  ll ri = INF+10;
  vi A(n+2);
  A[0] = -llINF+10;
  vector<bool>hoge(n+2);
  A[0] = llINF+10;
  if(n%2){
    A[n+1] = llINF+10;
  }else A[n+1] = -llINF+10;
  for(int i=1;i<=n;i++){
    string s;cin>>s;
    if(s == "x"){
      hoge[i] = true;
      A[i] = -llINF;
    }else{
      A[i] = toint(s);
    }
  }
  for(int i=1;i<=n;i++){
    if(hoge[i]){
      if(hoge[i-1]||hoge[i+1]){
	cout<<"none"<<endl;return;
      }
      if(i%2==0){
	le = max({le,A[i-1]+1,A[i+1]+1});
      }else{
	ri = min({ri,A[i-1]-1,A[i+1]-1});
      }
    }else{
      if(hoge[i] == false && hoge[i-1] == false && hoge[i+1] == false){
	if(i%2){
	  if(A[i] >= A[i-1] || A[i] >= A[i+1]){cout<<"none"<<endl;return;}
	}else{
	  if(A[i] <= A[i-1] || A[i] <= A[i+1]){cout<<"none"<<endl;return;}
	}
      }
    }
  }
  if(le == -INF-10 && ri == INF+10){
 
    for(int i=1;i<=n;i++){
      if(i%2){
	if(A[i] >= A[i-1] || A[i] >= A[i+1]){cout<<"none"<<endl;return;}
      }else{
	if(A[i] <= A[i-1] || A[i] <= A[i+1]){cout<<"none"<<endl;return;}
      }
    }
    cout<<"ambiguous"<<endl;
  }else{
    if(le == -INF-10||ri == INF+10){cout<<"ambiguous"<<endl;}
    // cout<<le<<" "<<ri<<endl;
    else if(le == ri)cout<<le<<endl;
    else if(le > ri){
      cout<<"none"<<endl;
    }else{
      cout<<"ambiguous"<<endl;
    }
  }
}
int main(){
  cin.tie(0);
  ios::sync_with_stdio(false);
  ll n;
  while(cin>>n,n){
    solve(n);
  }
  return 0;
}



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

const int MAXN = 1e6+5;

long long a, b, c, d;

long long max(long long a, long long b) { return a > b ? a : b; }

int main() {
    ios::sync_with_stdio(false); cin.tie(0);
    cin >> a >> b >> c >> d;
    cout << max(max(a*c, b*d), max(a*d, b*c)) << "\n";
    return 0;
}


Measured memory_bytes: 3688
#include<stdio.h>
#define N 3001
int main()
{
	int a,b,c,d,n,i;
	scanf("%d",&n);
	d=0;
	b=0;
	for(i=1;i<=n;i++)
	{
		scanf("%d",&a);
		b+=a-1;
	}
	printf("%d",b);
	return 0;
}

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

const int MAX = 10000000;
const int MOD = 1000000007;

long long fac[MAX], finv[MAX], inv[MAX];

void COMinit() {
    fac[0] = fac[1] = 1;
    finv[0] = finv[1] = 1;
    inv[1] = 1;
    for (int i = 2; i < MAX; i++){
        fac[i] = fac[i - 1] * i % MOD;
        inv[i] = MOD - inv[MOD%i] * (MOD / i) % MOD;
        finv[i] = finv[i - 1] * inv[i] % MOD;
    }
}

long long COM(int n, int k){
    if (n < k) return 0;
    if (n < 0 || k < 0) return 0;
    return fac[n] * (finv[k] * finv[n - k] % MOD) % MOD;
}

int main() {
  COMinit();
  int x,y;
  cin >> x >> y;
  if( (2*x-y)%3==0 && (2*y-x)%3==0 ){
    int a=(2*x-y)/3;
    int b=(2*y-x)/3;
    cout << COM(a+b,a) << endl;
  }
  else
    cout << 0 << endl;
}

Measured memory_bytes: 234624
#include <bits/stdc++.h>
using namespace std;
const int maxn=3000+5;
const int maxm=3000+5;
int a[maxn][maxm],head[maxm];
int vis[maxm];
int n,m;
bool ban[maxm];
int main()
{
    scanf("%d%d",&n,&m);
    for(int i=1;i<=n;i++)
        for(int j=1;j<=m;j++)
            scanf("%d",&a[i][j]);
    memset(ban,0,sizeof(ban));
    int ans=n;
    for(int i=1;i<=n;i++) head[i]=1;
    int tim=0;
    while(1)
    {
        if(++tim==m) break;
        memset(vis,0,sizeof(vis));
        for(int i=1;i<=n;i++) vis[a[i][head[i]]]++;
        int tmpmax=0,maxx=0;
        for(int i=1;i<=m;i++)
            if(maxx<vis[i]) maxx=vis[i],tmpmax=i;
        ans=min(ans,maxx);
        if(tmpmax!=0) ban[tmpmax]=true;
        bool owari=false;
        for(int i=1;i<=n;i++){
            while(ban[a[i][head[i]]])
            {
                head[i]++;
                if(head[i]==m) {owari=true;break;}
            }
        }
        if(owari) break;
    }
    printf("%d\n",ans);
    return 0;
}


Measured memory_bytes: 3200
n = gets.strip.to_i
as = gets.strip.split.map(&:to_i)

l = r = 0
xor = sum = as[0]
result=0

while r<n

  if xor==sum
    result += (r-l+1)
    if r < n
      break if r==n-1
      r += 1
      xor = xor^as[r]
      sum += as[r]
    else
      p"#"
      p [xor, as[l]]
      xor = xor^as[l]
      sum -= as[l]      
      l += 1
    end
  else
    if l <= r
      xor = xor^as[l]
      sum -= as[l]
      l += 1
    else
      p"########"
      r += 1
      xor = xor^as[r]
      sum += as[r]
    end
  end
end

p result

Measured memory_bytes: 16652
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define rep(i, a, b) for (int i = (a); i < (int)(b); ++i)
#define rrep(i, a, b) for (int i = ((int)a) - 1; i >= ((int)a); --i)
#define all(x) x.begin(), x.end()
#define sz(x) ((int)x.size())
using pii = pair<int, int>;
using vi = vector<int>;
using vvi = vector<vi>;

void dump_impl(string s) {}
template <class H, class... T>
void dump_impl(string s, H head, T... tail) {
    int p = 0;
    rep(i, 0, sz(s)) {
        char x = s[i];
        if (p == 0 && x == ',') {
            cerr << "=" << head << ", ";
            dump_impl(s.substr(i + 1), tail...);
            return;
        } else {
            cerr << x;
            if (x == '(') p++;
            if (x == ')') p--;
        }
    }
}
#ifdef LOCAL
#define dump(...) do { cerr << "\x1b[33;1m"; dump_impl(#__VA_ARGS__ ",", __VA_ARGS__); cerr << "\x1b[0m" << endl; } while (0) 
#else
#define dump(...)
#endif

constexpr int DY[8] = {-1, 0, 1, 0, -1, 1, 1, -1};
constexpr int DX[8] = {0, 1, 0, -1, 1, 1, -1, -1};

void solve(int N, int M) {
    vi C(M);
    rep(i, 0, M) cin >> C[i];
    vi X(N);
    rep(i, 0, N) cin >> X[i];

    vvi dp(N + 1, vi(256, 1e9));
    dp[0][128] = 0;
    rep(i, 0, N) {
        rep(j, 0, 256) {
            rep(k, 0, M) {
                int jj = j + C[k];
                jj = max(jj, 0LL);
                jj = min(jj, 255LL);
                dp[i + 1][jj] = min(dp[i + 1][jj], dp[i][j] + (jj - X[i]) * (jj - X[i]));
            }
        }
    }
    int ans = 1e9;
    rep(i, 0, 256) {
        ans = min(ans, dp[N][i]);
    }
    cout << ans << endl;
}

signed main() {
    while (1) {
        int N, M;
        cin >> N >> M;
        if (N == 0 && M ==0) break;
        solve(N, M);
    }
}


Measured memory_bytes: 43668
#include <iostream>
#include <vector>
#include <math.h>
#include <algorithm>
using namespace std;

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

	int cnt;
	if (N < 10) {
		cnt = N;
	}
	else if (N < 100) {
		cnt = 9;
	}
	else if (N < 1000) {
		cnt = 9 + (N - 99);
	}
	else if (N < 10000) {
		cnt = 9 + 900;
	}
	else if (N < 100000) {
		cnt = 9 + 900 + (N - 9999);
	}
	else {
		cnt = 9 + 900 + 90000;
	}

	cout << cnt << endl;

	return 0;
}


Measured memory_bytes: 256
//^
#include<cstring>
#include<cstdio>
#include<vector>
#define min(a,b) (a<b?a:b)
#define max(a,b) (a>b?a:b)
using std::vector;
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=(x<<3)+(x<<1)+ch-'0';ch=getchar();}
    return x*f;
}
#define MaN 200005
#define MaI 262144
int n,m,num[MaN],f[MaN],t[MaI*2+5],a[MaN];
vector<int> v[MaN];
inline void rw(int x,int v){for(x+=MaI+1;t[x]=min(t[x],v),x>>=1;);}
inline int query(int l,int r){
    int res=1e9;
    for(l+=MaI,r+=MaI+2;l^r^1;l>>=1,r>>=1){
        if(~l&1) res=min(res,t[l+1]);
        if( r&1) res=min(res,t[r-1]);
    }
    return res;
}
int main(){
    n=read();
    for(int i=1;i<=n;++i) num[i]=num[i-1]+1-(a[i]=read()),f[i]=1e9;
    m=read();memset(t,0x3f,sizeof t);
	int l;
    for(int i=1;i<=m;++i) l=read(),v[l-1].push_back(read());
    for(int i=0;i<n;++i){
        f[i+1]=min(f[i+1],f[i]+a[i+1]);
        for(int j=0;j<v[i].size();j++){
            int w=min(f[i]+num[v[i][j]]-num[i],query(i,v[i][j])+num[v[i][j]]);
            f[v[i][j]]=min(f[v[i][j]],w); rw(v[i][j],w-num[v[i][j]]);
        }
    }
    printf("%d\n",f[n]);
}

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

using namespace std;

typedef long long ll;

const int N=2*1e5+50;
ll n,d,a,k,cumm[N],ans;
pair<ll,ll> arr[N];

int bin(int ind){
    int low=ind,high=n-1,mid,ans;
    while(low<=high){
        mid=low+(high-low)/2;
        if (arr[ind].first+(2*d)>=arr[mid].first){
            ans=mid;
            low=mid+1;
        }else
            high=mid-1;
    }
    return ans;
}

int main()
{
    scanf("%lld%lld%lld",&n,&d,&a);
    for (int i=0;i<n;i++)
        scanf("%lld%lld",&arr[i].first,&arr[i].second);
    sort(arr,arr+n);
    for (int i=0;i<n;i++){
        k+=cumm[i];
        arr[i].second-=k*a;
        if (arr[i].second>0){
            int num=bin(i);
            ans+=(arr[i].second+a-1)/a;
            cumm[num+1]-=(arr[i].second+a-1)/a;
            k+=(arr[i].second+a-1)/a;
        }
    }
    printf("%lld",ans);

    return 0;
}


Measured memory_bytes: 8304
#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include <queue>
#include <stack>
#include <set> 
#include <map>
#include <bitset>
#include <cctype>
#include <cmath>
#include <cstring>
#include <iomanip>
#define REP(i,n) for(int i=0; i<(n); ++i)
#define FOR(i,a,b) for(int i=(a); i<(b); ++i)
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
const int INF = 100100100;
const ll INFLL = 100100100100100100;
const int MOD = (int)1e9 + 7;
const ll MODLL = (ll)1e9 + 7;
const double EPS = 1e-9;
 
int main () {
    
    int n,k; cin >>n>>k;
    vector<int> a(n); REP(i,n) cin >> a[i];
    sort(a.begin(), a.end());

    vector<int> nums;
    for (int i = 0; i < (int)a.size();) {
        int j = i;
        while (j < (int)a.size() && a[i] == a[j]) {
            j++;
        }
        nums.push_back(j-i);
        i = j;
    }

    sort(nums.begin(), nums.end());
    int ans = 0;
    int count = 0;
    while (k < nums.size() - count) {
        ans += nums[count];
        count++;
    }
    cout << ans << endl;
    return 0;
}


Measured memory_bytes: 2168
a,b,h,m = map(int,input().split())
ans = 0
 
import math
 
rada = (2*math.pi/12)*(h+m/60)
radb = (2*math.pi/60)*m
 
if rada >= radb:
  theta = rada - radb
else:
  theta = radb - rada
  
if theta < math.pi:
  pass
else:
  theta = 2*math.pi - theta
 
ans = math.sqrt(a**2+b**2 - 2*a*b*math.cos(theta))
 
print(ans)

Measured memory_bytes: 9524
<?php
fscanf(STDIN, '%d %d', $n, $x);
$m_arr = [];
for($i = 0; $i < $n; $i++) {
    fscanf(STDIN, '%d', $m);
    $x -= $m;
    $m_arr[] = $m;
}
$min = min($m_arr);
echo floor($x / $min) + $n. "\n";

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

int main() {
  int n,k,x,s=0;
    cin>>n>>k;
  for(int i=0;i<n;i++){
    cin>>x;
    if(k/2<x) s+=k-x;
    else s+=x;
  }
  cout<<2*s<<endl;
}

Measured memory_bytes: 256
import java.util.Scanner;


public class Main {
    public static void main(String[] args){
    	Scanner input = new Scanner(System.in);
    	while(input.hasNextInt()){
    		int a = input.nextInt();
    		int b = input.nextInt();
    		int gcd_num = gcd(a,b);
    		System.out.println(gcd_num+" "+(a/gcd_num)*b);
    	}
    }
    
    public static int gcd(int x,int y){
    	int r;
        while((r = x % y) != 0) // yで割り切れるまでループ
        {
            x = y;
            y = r;
        }
        return y;
    }
}

Measured memory_bytes: 17268
import java.io.IOException;
import java.io.InputStream;
import java.util.NoSuchElementException;

public class Main {
	public static void main(String[] args) {
		FastScanner sc = new FastScanner();
		int X = sc.nextInt();
		int Y = sc.nextInt();
		int ans = X + Y/2;
		System.out.println(ans);
	}
}

class FastScanner {
    private final InputStream in = System.in;
    private final byte[] buffer = new byte[1024];
    private int ptr = 0;
    private int buflen = 0;

    private boolean hasNextByte() {
        if (ptr < buflen) {
            return true;
        } else {
            ptr = 0;
            try {
                buflen = in.read(buffer);
            } catch (IOException e) {
                e.printStackTrace();
            }
            if (buflen <= 0) {
                return false;
            }
        }
        return true;
    }

    private int readByte() {
        if (hasNextByte())
            return buffer[ptr++];
        else
            return -1;
    }

    private static boolean isPrintableChar(int c) {
        return 33 <= c && c <= 126;
    }

    public boolean hasNext() {
        while (hasNextByte() && !isPrintableChar(buffer[ptr]))
            ptr++;
        return hasNextByte();
    }

    public String next() {
        if (!hasNext())
            throw new NoSuchElementException();
        StringBuilder sb = new StringBuilder();
        int b = readByte();
        while (isPrintableChar(b)) {
            sb.appendCodePoint(b);
            b = readByte();
        }
        return sb.toString();
    }

    public long nextLong() {
        if (!hasNext())
            throw new NoSuchElementException();
        long n = 0;
        boolean minus = false;
        int b = readByte();
        if (b == '-') {
            minus = true;
            b = readByte();
        }
        if (b < '0' || '9' < b) {
            throw new NumberFormatException();
        }
        while (true) {
            if ('0' <= b && b <= '9') {
                n *= 10;
                n += b - '0';
            } else if (b == -1 || !isPrintableChar(b)) {
                return minus ? -n : n;
            } else {
                throw new NumberFormatException();
            }
            b = readByte();
        }
    }

    public int nextInt() {
        long nl = nextLong();
        if (nl < Integer.MIN_VALUE || nl > Integer.MAX_VALUE)
            throw new NumberFormatException();
        return (int) nl;
    }

    public double nextDouble() {
        return Double.parseDouble(next());
    }
}

Measured memory_bytes: 20180
#include <bits/stdc++.h>
#define f(i,a,b) for(int i=a;i<b;i++)
#define REP(i,n) f(i,0,n)
#define fd(i,a,b) for(int i=a;i>=b;i--)
#define pb push_back
#define w(t) int t; cin>>t; while(t--)
#define ff first
#define ss second
#define ii pair<int,int>
#define vi vector<int>
#define vii vector<ii>
#define vll vector<long long>
#define sz(x) int((x).size())
#define all(x) (x).begin(), (x).end()   
#define MAX 100002
#define MOD 1000000007
#define ll long long
 
using namespace std;


int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    string s,t;
    cin>>s>>t;
    int n=s.size();
    int m=t.size();
    vector<vector<int>>D(n+1,vector<int>(m+1,0));
    for(int i=n-1;i>=0;--i)
    {
        for(int j=m-1;j>=0;--j)
        {
            if(s[i]==t[j])
            {
                D[i][j]=1+D[i+1][j+1];
            }
            else{
                D[i][j]=max(D[i][j+1],D[i+1][j]);
            }
        }
    }
    string ans="";
    int p1=0,p2=0;
    while(p1<n && p2<m)
    {
        if(s[p1]==t[p2])
        {
            ans+=s[p1];
            ++p1;
            ++p2;
        }
        else{
            if(D[p1][p2]==D[p1][p2+1])
            {
                p2++;
            }
            else{
                p1++;
            }
        }
    }
    cout<<ans<<"\n";
    return 0;

 
 
}
 

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

using namespace std;
using ll = long long;
using P = pair<int, int>;


int main() {
	string s;
	cin >> s;
	if (s[1] == 'R') {
		if (s[0] == 'R') {
			if (s[2] == 'R') {
				cout << 3 << endl;
				return 0;
			}
			else {
				cout << 2 << endl;
				return 0;
			}
		}
		else {
			if (s[2] == 'R') {
				cout << 2 << endl;
				return 0;
			}
			else {
				cout << 1 << endl;
				return 0;
			}
		}
	}
	else {
			if (s[0] == 'R'){
				cout << 1 << endl;
				return 0;
			}
			if (s[2] == 'R') {
				cout << 1 << endl;
				return 0;
			}
			else {
				cout << 0 << endl;
				return 0;
			}
	}

	return 0;
}

Measured memory_bytes: 3592
from fractions import gcd

if __name__ == '__main__':
    n = gcd(*map(int, input().split()))
    a = i = 1
    while i * i < n:
        i += 1
        a += n % i < 1
        while n % i < 1:
            n /= i
    print(a + (n > 1))


Measured memory_bytes: 5048
#include <vector>
#include <list>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <deque>
#include <algorithm>
#include <utility>
#include <functional>
#include <sstream>
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <ctime>
#include <climits>
#include <cassert>
using namespace std;
inline int toInt(string s) {int v; istringstream sin(s);sin>>v;return v;}
template<class T> inline string toString(T x) {ostringstream sout;sout<<x;return sout.str();}
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef long long ll;
#define ALL(a) (a).begin(),(a).end()
#define RALL(a) (a).rbegin(),(a).rend()
#define EXIST(s,e) ((s).find(e)!=(s).end())
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n) FOR(i,0,n)
#define EACH(t,i,c) for(t::iterator i=(c).begin(); i!=(c).end(); ++i)
const double EPS = 1e-10;
const double PI  = acos(-1.0);

const int MAX=INT_MAX/20;
int main(){
	int t;
	cin>>t;
	REP(data,t){
		int n,k;
		cin>>n>>k;
		vi x(n);
		REP(i,n)cin>>x[i];

		vi d(n-1);
		ll total=0;
		REP(i,n-1){
			d[i]=x[i+1]-x[i];
			total+=d[i];
		}

		sort(ALL(d),greater<int>());

		REP(i,min(k-1,n-1)){
			total-=d[i];
		}

		cout<<total<<endl;
	}
}

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

class Main {
	public static void main(String[] args) {
		Scanner stdIn = new Scanner(System.in);
		while (true) {
			int sum = 0;
			String s = stdIn.nextLine();
			if(s.charAt(0) == '0')
				break;
			for (int i=0; i<s.length(); i++) {
				sum += Integer.parseInt(""+s.charAt(i));
			}
			System.out.println(sum);
		}
	}
}

Measured memory_bytes: 19040
import collections
import math
import operator as op
from functools import reduce
import numpy as np
import math
import bisect
import heapq


MOD = 1000000007


def gcd(a, b):
    while b:
        a, b = b, a % b
    return a


def lcm(a, b):
    return a * b // gcd(a, b)


def get_inputs(cast_func=None):
    if cast_func is None:
        return input().split()
    else:
        return list(map(cast_func, input().split()))


def get_input(cast_func=None):
    if cast_func is None:
        return input()
    else:
        return cast_func(input())


def is_prime(n):
    if n == 1:
        return False
    for i in range(2, int(n**0.5)+1):
        if n % i == 0:
            return False
    return True


def main():
    N, K = get_inputs(int)
    H = get_inputs(int)

    if K >= N:
        print(0)
    else:
        # K < N
        print(sum(sorted(H)[:N-K]))


if __name__ == '__main__':
    main()


Measured memory_bytes: 34208
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int INF = 1e14;
const int ZERO = 0;

signed main() {
    int N;
    cin >> N;
    int ret = INF;
    for(int i = 1;i * i <= N;i++) {
        if(N % i == 0) {
            int A = N / i;
            int B = i;
            ret = min(ret,A - 1 + B - 1);
        }
    }
    cout << ret << endl;
}

Measured memory_bytes: 256
import heapq
from collections import deque
from enum import Enum
import sys
import math
from _heapq import heappush, heappop
import copy

BIG_NUM = 2000000000
HUGE_NUM = 99999999999999999
MOD = 1000000007
EPS = 0.000000001
sys.setrecursionlimit(3**12)

SIZE = 13
POW = [1]*SIZE

for i in range(1,SIZE):
    POW[i] = POW[i-1]*4

N = int(input())
memo = {'A':1,'G':2,'C':3,'T':4}
DICT = [False]*POW[12]

def getNUM(tmp_str):
    global POW,memo
    ret = -1
    for i in range(len(tmp_str)):
        ret += memo[tmp_str[i]]*POW[i]
    return ret


for _ in range(N):
    command,tmp_str = map(str,input().split())
    if command[0] == 'i':
        DICT[getNUM(tmp_str)] = True
    else:
        if DICT[getNUM(tmp_str)]:
            print("yes")
        else:
            print("no")



Measured memory_bytes: 137404
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<vector>
#include<functional>
#include<algorithm>
#include<stdlib.h>
#include<string.h>
#include<cmath>
#include<deque>
#include<set>
using namespace std;
typedef long long ll;
#define rep(i,a,b) for(int i=a;i<b;i++)
#define all(_x) _x.begin(), _x.end()
#define r_sort(_x) sort(_x.begin(), _x.end(), std::greater<int>())
#define INF 1LL << 60
#define MAX 1000000000
const double PI = 3.14159265358979323846;
int gcd(int a, int b) { return a % b == 0 ? b : gcd(b, a % b); }
int main() {
	int R;
	scanf("%d", &R);
	printf("%f", 2 * R * PI);
	return 0;
}


Measured memory_bytes: 1748
package main

import (
	f "fmt"
)

func main() {
	var a, b, c int

	f.Scan(&a, &b, &c)

	if a == b && a != c {
		f.Println("Yes")
	} else if a == c && a != b {
		f.Println("Yes")
	} else if b == c && b != a {
		f.Println("Yes")
	} else {
		f.Println("No")
	}
}


Measured memory_bytes: 512
import sys

stdin = sys.stdin

ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
ns = lambda: stdin.readline().rstrip()  # ignore trailing spaces


li = []
n = int(input())
for i in range(5):
    li.append(int(input()))
if n <= min(li):
    print(5)
else:
    if n % min(li) == 0:
        print(5 + n // min(li) -1)
    else:
        print(5 + n // min(li))

Measured memory_bytes: 3064
// J'aime
// Chemise Blanche

#include <bits/stdc++.h>
using namespace std;

#define int long long

#define ii pair<int,int>
#define fi first
#define sc second

#define all(x) (x).begin(),(x).end()
#define dbg(x) cerr << __LINE__ << " > " << #x << " = " << (x) << endl

void MAIN() {
  int n; cin >> n;
  cout << (n + 1000 - 1) / 1000 * 1000 - n << '\n';
}

signed main() {
#ifdef _DEBUG
  // freopen("in" , "r", stdin );
  // freopen("out", "w", stdout);
#endif
  ios::sync_with_stdio(0); cin.tie(0);
  int T = 1;
  // cin >> T;
  while (T--) MAIN();
}


Measured memory_bytes: 3632
#include <iostream>
#include <math.h>
#include <stdio.h>

using namespace std;

int main(){
	double s;
	int x,h;
	double c;

	while(cin>>x){
		cin>>h;
		if(x==0&&h==0) break;
		c = sqrt(h*h + x*x/(double)4);
		printf("%.6f\n",(double)(x*x+2*x*c));
	}
	return 0;
}

/*
Sample Input
6
4
7
9
0
0
Output for the Sample Input
96.000000
184.192455 
*/

Measured memory_bytes: 1192
#include<bits/stdc++.h>
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define int long long
#define vec(a,n) vector<int> (a)((n))
#define Vec(a,n) vector<string> (a)((n))
#define P pair<int,int>
#define PQ(n) priority_queue<P,vector<P>,greater<P>> (n)
#define pq(n) priority_queue<int> (n)
using namespace std;
 
signed main(){
 
    int a,b;
    cin >> a ;
    if(a == 3 || a == 5 || a == 7){
        cout << "YES";
    }
    else{
        cout << "NO";
    }
    return 0;
}

Measured memory_bytes: 256
import java.util.*;

class Main {
	Scanner sc;
	String S;
	
	private void calc() {
		sc = new Scanner(System.in);
		S = sc.next();
		StringBuilder sb = new StringBuilder();
		for (int i = 0; i < S.length(); i++) sb.append('x');
		System.out.println(sb);
	}

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


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

using namespace std;

int main()
{
    bool ok = true;
    map<long long, long long> mp;
    long long n, a;
    cin >> n;
    for(int i = 0; i < n; i++)
    {
        cin >> a;
        mp[a]++;
        if(mp[a] > 1)
            ok = false;
    }
    if(ok)
        cout << "YES";
    else cout << "NO";
}


Measured memory_bytes: 12800
N = gets.to_i
ary = gets.chomp.split(" ").map(&:to_i)

if !(ary.include?(1))
    puts -1
    exit
end

count = 0
i= 0
j = 1
while i < ary.length && j <= ary.length do
    if ary[i] != j  
        count += 1
        i += 1
    else
        i += 1
        j += 1
    end
end

puts count

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

#define TRACE(x) x
#define WATCH(x) TRACE(cout << #x" = " << x << endl)
#define WATCHR(a, b) TRACE(for (auto it=a; it!=b;) cout << *(it++) << " "; cout << endl)
#define WATCHC(V) TRACE({cout << #V" = "; WATCHR(V.begin(), V.end());})

#define sz(x) int((x).size())
#define all(x) (x).begin(), (x).end()

using ll = long long;
using vi = vector<int>;
using vvi = vector<vi>;
using vll = vector<ll>;
using vvll = vector<vll>;
using vb = vector<bool>;
using vs = vector<string>;

mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());

int dist[100][100][100];

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

    int A, B, C;
    cin >> A >> B >> C;

    memset(dist, 0xff, sizeof(dist));

    vvi moves = { { 0, 1, 1}, { 1, 0, 1 }, { 1, 1, 0 },
                  { 0, 0, 2}, { 0, 2, 0 }, { 2, 0, 0 } };

    dist[A][B][C] = 0;
    queue<tuple<int, int, int>> bfs;
    bfs.push(make_tuple(A, B, C ));
    for (; !bfs.empty(); bfs.pop()) {
        int a, b, c; tie(a, b, c) = bfs.front();
        if (a == b && b == c) { cout << dist[a][b][c] << endl; return 0; }
        for (auto m : moves) {
            int aa = a + m[0], bb = b + m[1], cc = c + m[2];
            if (aa < 100 && bb < 100 && cc < 100 && dist[aa][bb][cc] == -1) {
                dist[aa][bb][cc] = dist[a][b][c] + 1;
                bfs.push(make_tuple(aa, bb, cc ));
            }
        }
    }

    return 0;
}




Measured memory_bytes: 4224
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 1;
int par[N],n,b[N],m;
long long sum[N],v[N];
vector<int>g[N];
vector<pair<int,int>>temp;
bool vis[N];
int get(int u){return par[u] == u ? u : par[u] = get(par[u]);}
int main(){
	//freopen("readin.txt","r",stdin);
	scanf("%d%d",&n,&m);
	for(int i = 0,a;i < n;i++){
		par[i] = i;
		scanf("%d%d",&a,b + i);
		temp.push_back({a - b[i],i});
	}
	for(int i = 0,a,b;i < m;i++){
		scanf("%d%d",&a,&b);
		g[--a].push_back(--b);
		g[b].push_back(a);
	}
	sort(temp.begin(),temp.end());
	for(auto i : temp){
		int need = max(0,i.first),at = i.second;
		sum[at] = b[at];
		v[at] = need;
		vis[at] = 1;
		for(auto j : g[at]){
			int from = get(j);
			if(from == at || !vis[from])continue;
			sum[at]+=sum[from];
			if(sum[from] + v[from] >= need)v[at] = min(v[at],v[from]);
			else v[at] = min(v[at],need - sum[from]);
			// v[at] = min(v[at],max(v[from],need - sum[from]));
			par[from] = at;
		}
	}
	printf("%lld\n",sum[temp.back().second] + v[temp.back().second]);
}

Measured memory_bytes: 9076
from collections import deque
  
n = int(input())
dll = deque()
  
for i in range(n):
    input_line = input().split()
    if input_line[0] == "insert":
        dll.appendleft(input_line[1])
    elif len(dll) == 0:
        continue
    elif input_line[0] == "delete":
        try:
            dll.remove(input_line[1])
        except:
            pass
    elif input_line[0] == "deleteFirst":
        dll.popleft()
    else:
        dll.pop()
          
print(*dll)

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

using namespace std;

const int MOD=1000000007;
int n;
typedef long long int ll;

int gcd(int a,int b)
{
	if (b==0) return a;
	return gcd(b,a%b);
}

template<class T,class K>
pair<T,K> mp(T a,K b)
{
	return make_pair(a,b);
}

ll mult(ll a,ll b)
{
	return (a*b)%MOD;
}

ll pot(ll n,ll k)
{
	if (k==0) return 1;
	ll a=pot(n,k/2);
	a=mult(a,a);
	if (k%2) return mult(a,n);
	else return a;
}

ll divide(ll a,ll b)
{
	return mult(a,pot(b,MOD-2));
}

ll sub(ll a,ll b)
{
	return (a-b+MOD)%MOD;
}

int a,b;

int main()
{
	std::ios_base::sync_with_stdio(false);
	cin>>n>>a>>b;
	if (abs(a-b)%2==0) cout<<"Alice"<<endl;
	else cout<<"Borys"<<endl;
}



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

using namespace std;

int main() {

    int n, m;

    while(cin >> n >> m, n | m) {

        vector<int> circle;

        for(int i = 1; i <= n; ++i) {
            circle.push_back(i);
        }

        for(int i = m; circle.size() != 1; i = (i + m - 1) % circle.size() + 1) {
            circle.erase(circle.begin() + i - 1);
            --i;
        }

        cout << circle[0] << endl;

    }

}

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

int main(void){
	
	int stack[100] = {0};
	int top = 0;
	char input, dump;
	
	for(;;){
		scanf("%c", &input);
		if( input == '\n' ) break;
		else if( input >= '0' && input <= '9' ) stack[top] = (stack[top]*10+input-'0');
		else if( input == ' ' ) {
			if( stack[top] != 0 ) top++;
		}
		else {
			if( input == '+' ) stack[top-2] += stack[top-1];
			else if( input == '-' ) stack[top-2] -= stack[top-1];
			else if( input == '*' ) stack[top-2] *= stack[top-1];
			else stack[top-2] /= stack[top-1];
			top--;
			stack[top] = 0;
		}
	}
	
	printf("%d\n", stack[0]);
	
}


Measured memory_bytes: 2100
// 失敗するからこそ そこから立ち向かって行く強さがあってそんな強さが本当の強さだと私は思うから
#ifdef DAIJOBU
#include "/home/v-o_o-v/deb.h"
#define deb(x...) cerr << "[" << #x << "] = ["; _print(x)
#else
#include <bits/stdc++.h>
#define deb(x...)
#endif 

using namespace std;

#define mp make_pair
#define mt make_tuple
#define fi first
#define se second
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define forn(i, n) for (int i = 0; i < (int)(n); ++i)
#define for1(i, n) for (int i = 1; i <= (int)(n); ++i)
#define ford(i, n) for (int i = (int)(n) - 1; i >= 0; --i)
#define fore(i, a, b) for (int i = (int)(a); i <= (int)(b); ++i)

mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
 
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<pii> vpi;
typedef long long lint;
typedef double ld;

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

    int n;
    cin >> n;
    set<string> s;
    while(n--){
        string t;
        cin >> t;
        s.insert(t);
    }
    cout << s.size();
    return 0;
}
// Write Here


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

int main () {
	string s;
	cin >> s;
	if(s == "AAA" || s == "BBB") {
		cout << "No" << "\n";
	}else {
		cout << "Yes" << "\n";
	}
	return 0;
}

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

using namespace std;
typedef long long ll;
#define int ll

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; }

#define REP(i,n) for(int i=0;i<n;++i)
#define SORT(name) sort(name.begin(), name.end())
#define ZERO(p) memset(p, 0, sizeof(p))
#define MINUS(p) memset(p, -1, sizeof(p))
#if 1
#  define DBG(fmt, ...) printf(fmt, ##__VA_ARGS__)
#else
#  define DBG(fmt, ...)
#endif

const ll LLINF = (1LL<<60);
const int INF = (1LL<<30);
const double DINF = std::numeric_limits<double>::infinity();
const int MOD = 1000000007;
const int MAX_N = 100010;

int N;
vector<string> S;

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

    cin >> N;
    S.resize(N);
    REP(i, N) {
        cin >> S[i];
    }

    // first: 下がるときの最下層
    // second: 最終的な 0 からの差分
    vector< pair<int, int> > V;
    V.assign(N, make_pair(0, 0));
    // V の作成
    REP(i, N) {
        REP(j, S[i].length()) {
            if(S[i][j] == ')') {
                if (V[i].second > 0) {
                    V[i].second--;
                } else {
                    V[i].first++;
                }
            }
            else {
                V[i].second++;
            }
        }
    }
    REP(i, N) {
        V[i].second -= V[i].first;
    }
    // V[i].second が正のものと負のもので分ける
    vector< pair<int, int> > V1, V2;
    REP(i, N) {
        if(V[i].second >= 0) { V1.push_back(V[i]); }
        else { V2.push_back(V[i]); }
    }

    // V1 のチェック
    SORT(V1);
    int cur1 = 0;
    for(auto& v : V1) {
        if(cur1 >= v.first) {
            cur1 += v.second;
        }
        else {
            // cur が一瞬でも 0 を下回るなら無理
            printf("No\n");
            return 0;
        }
    }

    // V2 のチェック
    // V2 を V1 のように登っていく側の値に変換
    REP(i, V2.size()) {
       pair<int, int> tmp = make_pair(abs(V2[i].first) - abs(V2[i].second), abs(V2[i].second));
       V2[i] = tmp; 
    }
    // V2 も V1 同様にチェック
    SORT(V2);
    int cur2 = 0;
    for(auto& v : V2) {
        if(cur2 >= v.first) {
            cur2 += v.second;
        }
        else {
            printf("No\n");
            return 0;
        }
    }


    if(cur1 == cur2) {
        printf("Yes\n");
        return 0;
    }

    printf("No\n");
    return 0;
}


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

int gcd(int x, int y);

const int INF = 1001001001;

/*struct mint {
    ll x; // typedef long long ll;
    mint(ll x = 0) :x((x% mod + mod) % mod) {}
    mint operator-() const { return mint(-x); }
    mint& operator+=(const mint a) {
        if ((x += a.x) >= mod) x -= mod;
        return *this;
    }
    mint& operator-=(const mint a) {
        if ((x += mod - a.x) >= mod) x -= mod;
        return *this;
    }
    mint& operator*=(const mint a) { (x *= a.x) %= mod; return *this; }
    mint operator+(const mint a) const { return mint(*this) += a; }
    mint operator-(const mint a) const { return mint(*this) -= a; }
    mint operator*(const mint a) const { return mint(*this) *= a; }
    mint pow(ll t) const {
        if (!t) return 1;
        mint a = pow(t >> 1);
        a *= a;
        if (t & 1) a *= *this;
        return a;
    }

    // for prime mod
    mint inv() const { return pow(mod - 2); }
    mint& operator/=(const mint a) { return *this *= a.inv(); }
    mint operator/(const mint a) const { return mint(*this) /= a; }
};
istream& operator>>(istream & is, const mint & a) { return is >> a.x; }
ostream& operator<<(ostream & os, const mint & a) { return os << a.x; }*/


int main() {
    ll n;
    cin >> n;
    ll ans;
    ans = 1;
    vector<unsigned long long> a(n);
    int flag = 0;
    for (ll i = 0; i < n; i++) {
        cin >> a[i];
        if (a[i] == 0) {
            flag = 1;
        }
    }
    if (flag == 1) {
        cout << 0 << endl;
        return 0;
    }
    for (ll i = 0; i < n; i++) {
        //unsigned long long a;
        //cin >> a;
        if (ans * a[i] > 1000000000000000000 || ans * a[i] < ans) {
            cout << "-1" << endl;
            return 0;
        }
        ans *= a[i];
    }

    cout << ans << endl;
    return 0;
}

/*���C�u�����ő����*/
//���[�O���b�h�̌ݏ��@
int gcd(int x, int y) {
    int num[3];
    num[0] = (x > y) ? x : y;
    num[1] = (x <= y) ? x : y;
    num[2] = num[0] % num[1];

    while (num[2]) {
        num[0] = num[1];
        num[1] = num[2];
        num[2] = num[0] % num[1];
    }

    return num[1];
}

Measured memory_bytes: 4048