akhauriyash/Code-Regression huggingface dataset:
#include<bits/stdc++.h>
#define ll long long 
#define print(ds) for(auto i:ds){cout<<i<<" ";} cout<<endl;
#define printpair(pairs) cout<<pairs.first<<" "<<pairs.second<<endl;
#define p2v(v) for(int i=0; i<v.size();i++){ for(int j=0; j<v[i].size();j++){ cout<<v[i][j]<<" ";} cout<<endl;}
#define pp pair <ll, ll>
#define mod 1000000007
#define large 1e6
#define fastio ios_base::sync_with_stdio(false); cin.tie(NULL);
using namespace std;
int32_t main(){
    fastio;
    string s;
    cin>>s;
    int n= s.size();
    if(s[n-1]=='s'){
        s+= "es";
    }
    else s+='s';
    cout<<s<<endl;
}

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

 using namespace std;
 typedef long long ll;
 #define rep(i, n) for(int i = 0; i < (int)(n); i++)

 int gcm(int a, int b) {
   if (a < b) swap(a, b);

   if (a % b == 0) return b;
   else return gcm(b, a % b);
 }

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

   vector<int> A(N);
   rep(i, N) cin >> A.at(i);
   int ans = A.at(0);

   for (int i = 1; i < N; i++) {
     ans = gcm(ans, A.at(i));
   }

   cout << ans << endl;
 }


Measured memory_bytes: 640
#include <iostream>
#include <cstring>
#include <cstdio> 
#define ll long long
#define ull unsigned long long
#define ld long double
using namespace std;
const ll mod=1e9+7;
char s[200];
int k;
int main(){
	cin>>k;
	scanf("%s",s+1);
	int len=strlen(s+1);
	if(len<=k)	printf("%s",s+1);
	else{
		char tep[200];
		for(int i=1;i<=k;i++)
			printf("%c",s[i]);
		printf("...");	
			
	}




	return 0;
}


Measured memory_bytes: 3204
import java.util.Scanner;

public class Main {

 public static void main (String[] args){

   Scanner sc = new Scanner(System.in);

   int N = sc.nextInt();
   int K = sc.nextInt();
   int X = sc.nextInt();
   int Y = sc.nextInt();
   int sum1 = 0;
   int sum2 = 0;


   if (N >= K){
   sum1 =  X * K;
   sum2 =  Y * (N-K);
   System.out.println(sum1 + sum2);
    }

    else {
    sum1 =  X * N;
   System.out.println(sum1);
    }
 }
}


Measured memory_bytes: 21716
#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
int main(void)
{
	int N=0,M=0,D=0,a=1,all=0;
	cin>>N>>M>>D;
	char S[N][M]={0};
	for(int i=0;i<N;i++){
		scanf("%s",S[i]);
	}
	for(int i=0;i<N;i++){
		for(int j=0;j<M;j++){
				if(S[i][j]=='.')
				{
					while(S[i][j+a]=='.')
					{
						if(M==(a+j)){break;}
						if(a+1==D){all++;break;}
						if(S[i][j+a]=='#'){break;}
						a++;
					
					}
					a=1;
					while(S[i+a][j]=='.')
					{
						if(N==(a+i)){break;}
						if(a+1==D){all++;break;}
						if(S[i+a][j]=='#'){break;}
						a++;
						
					}
					a=1;
				}
		}
	}
	cout<<all<<endl;
	return 0;
}


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

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

    if (n >= 64)
        cout << 64 << endl;
    else if (n >= 32)
        cout << 32 << endl;
    else if (n >= 16)
        cout << 16 << endl;
    else if (n >= 8)
        cout << 8 << endl;
    else if (n >= 4)
        cout << 4 << endl;
    else if (n >= 2)
        cout << 2 << endl;
    else
        cout << 1 << endl;
}

Measured memory_bytes: 256
from collections import *
from heapq import *
from itertools import *
from fractions import gcd
import sys
from decimal import *
import copy
from bisect import *
input=lambda :sys.stdin.readline().rstrip()


S=list(input())
if S[2]==S[3] and S[4]==S[5]:
    print("Yes")
else:
    print("No")


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

typedef long long ll;
typedef unsigned long long ull;
//typedef long double ld;

typedef map<int, int> mii;
typedef pair<int, int> pii;
typedef pair<double,double> pdd;
typedef set<int> si;
typedef set<ll> sll;
typedef vector<int> vi;
typedef vector<pair<int, int > > vii;
typedef vector<ll> vll;
typedef vector<double> vd;
typedef vector<vi> vvi;
typedef vector<vii> vvii;
typedef vector<vll> vvll;

constexpr ll MOD = 1e9+7;
constexpr int INF = 1<<30;
constexpr ll  LINF = 1LL<<60;

#define Sort(x) sort(x.begin(),x.end())
#define Reverse(x) reverse(x.begin(),x.end())
#define ABS(a,b) ((a)<(b)?(b)-(a):(a)-(b))
#define nbit(n) (1ll<<(n))

// sort(begin(x), end(x), greater<ll>());
// lower_bound(a.begin(), a.end(), val): a[i] ≥ valである最左の位置
// upper_bound(a.begin(), a.end(), val): a[i] > valである最左の位置

// Returns minimum of a and b.
// If a is less b, a is set to b.
template<typename T>
T chmin(T& a, T b) {
  if (a > b) {
    a = b;
  }
  return a;
}

// Returns maximum of a and b.
// If a is less b, a is set to b.
template<typename T>
T chmax(T& a, T b) {
  if (a < b) {
    a = b;
  }
  return a;
}

// a = (a+b)%MOD;
void addmod(ll& a, ll b) {
  a = (a+b)%MOD;
}

template <typename T>
void dumpContents(const T& v, const string& msg)
{
  cerr << "### " << msg << " ###\n";
  for (const auto& x : v) {
    cerr << x << " ";
  }
  cerr << endl;
}

struct before_main_function {
  before_main_function() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    cout<<setprecision(20)<<fixed;
    #define endl "\n"
  }
} before_main_function;

// ========== end of template ==========
struct Event {
  ll from;
  ll to;
  ll distance;
};

int main(int argc, char** argv) {
  int N,Q;
  cin >> N >> Q;

  vector<Event> events;
  events.reserve(N);
  for (int i=0; i<N; i++) {
    ll S, T, X;
    cin >> S >> T >> X;
    // [X-S, X-T) X

    Event e {max({0ll, S-X}), max({0ll, T-X}), X};
    events.push_back(e);
  }

  sort(begin(events), end(events),
       [](const Event& lhs, const Event& rhs) {
         if (lhs.from == rhs.from) {
	   return lhs.distance < rhs.distance;
	 }

	 return lhs.from < rhs.from;
       });

  auto it = begin(events);

  auto cmp = [](const Event& lhs, const Event& rhs) {
    return lhs.distance > rhs.distance;
  };
  priority_queue<Event, vector<Event>, decltype(cmp)> q(cmp);

  for (int i=0; i<Q; i++) {
    ll D;
    cin >> D;

    while (it != end(events) && it->from <= D) {
      q.push(*it);
      it++;
    }

    while (!q.empty()) {
      if (q.top().to > D) break;

      q.pop();
    }

    if (q.empty()) {
      cout << -1 << endl;
    } else {
      cout << q.top().distance << endl;
    }
  }

  return 0;
}


Measured memory_bytes: 11888
#include<cstdio>
#include<bits/stdc++.h>
typedef long long int ll;
#define REP(i, n) for(int i = 0; i < (n); i++)
#define FOR_IN(i, a, b) for(int i = (a); i < (b); i++)
#define BETWEEN(x, a, b) ((x) >= (a) && (x) <= (b))
#define BIT(b, i) (((b) >> (i)) & 1)
#define LOG_F 1
#define LOG(...) if(LOG_F) fprintf(stderr, __VA_ARGS__)

using namespace std;

ll pow(int x, int n){
  return n == 0 ? 1 : x * pow(x, n - 1);
}


/*

 */
#define ALF 'z'-'a'+1
string s;
int num[ALF];

int main(){
  cin>>s;
  REP(i,s.size()){
    num[s[i]-'a']++;
  }

  REP(j,ALF){
    if(num[j]==0){
      cout<< (char)(j+'a') <<endl;
      return 0;
    }
  }
  cout<<"None\n";

  return 0;
}


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

public class Main {
	static Scanner sc = new Scanner(System.in);
	static int N, M;
	static long[] X;

	public static void main(String[] args) {
		N = sc.nextInt();
		M = sc.nextInt();
		X = new long[N];
		for (int i = 0; i < N; i++) {
			X[i] = sc.nextInt();
		}
		Edge[] es = new Edge[M];
		for (int i = 0; i < M; i++) {
			int A = Integer.parseInt(sc.next()) - 1;
			int B = Integer.parseInt(sc.next()) - 1;
			int Y = Integer.parseInt(sc.next());
			es[i] = new Edge(A, B, Y);
		}
		Arrays.sort(es, (e1, e2) -> Integer.compare(e1.w, e2.w));
		UnionFind uf = new UnionFind(N);
		int ans = 0;
		for (int i = 0; i < M; i++) {
			uf.union(es[i].a, es[i].b, es[i].w);
//			System.err.println(uf.validEdges);
			ans = Math.max(ans, uf.validEdges);
		}
		System.out.println(M - ans);
	}

	static class Edge {
		int a, b;
		int w;

		public Edge(int a, int b, int w) {
			this.a = a;
			this.b = b;
			this.w = w;
		}
	}

	static class Node {
		int size;
		long sumNode;
		int invalidCount;

		public Node(int size, long sumNode) {
			this.size = size;
			this.sumNode = sumNode;
		}

//		int validCount() {
//			if (edges.isEmpty()) return 0;
//			if (sumNode >= edges.last()) return edges.size();
//			return edges.subSet(-1, true, (int) sumNode, true).size();
//		}
	}

	static class UnionFind {
		Node[] set;
		int validEdges = 0;

		UnionFind(int n) {
			set = new Node[n];
			for (int i = 0; i < N; i++) {
				set[i] = new Node(-1, X[i]);
			}
		}

		void union(int a, int b, int edgeWeight) {
			int rtA = root(a);
			int rtB = root(b);
			if (rtA == rtB) {
				if (edgeWeight <= set[rtA].sumNode) {
					if (set[rtA].invalidCount > 0) throw new RuntimeException();
					validEdges++;
				} else {
					set[rtA].invalidCount++;
				}
				return;
			}
			set[rtA].size += set[rtB].size;
			set[rtA].sumNode += set[rtB].sumNode;
			if (edgeWeight <= set[rtA].sumNode) {
				validEdges += set[rtA].invalidCount + set[rtB].invalidCount + 1;
				set[rtA].invalidCount = 0;
			} else {
				set[rtA].invalidCount += set[rtB].invalidCount + 1;
			}
			set[rtB].size = rtA;
		}

		boolean find(int a, int b) {
			return root(a) == root(b);
		}

		int root(int a) {
			if (set[a].size < 0) {
				return a;
			} else {
				set[a].size = root(set[a].size);
				return set[a].size;
			}
		}

		int size(int a) {
			return -set[root(a)].size;
		}
	}
}


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

int main()
{
    string s;
    cin >> s;
    if (s == "RRR")
    {
        cout << 3 << endl;
    }
    else if (s == "RRS" || s == "SRR")
    {
        cout << 2 << endl;
    }
    else if (s == "SSS")
    {
        cout << 0 << endl;
    }
    else
    {
        cout << 1 << endl;
    }
    return 0;
}

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

const int INF=1<<30;

vector<int> dx={1, 0, -1, 0}, dy={0, 1, 0, -1};

int main(){
    int H, W; cin >> H >> W;
    int cnt=0;
    vector<pair<int, int>> black;
    vector<vector<char>> s(H, vector<char>(W));
    for(int i=0; i<H; i++){
        for(int j=0; j<W; j++){
            cin >> s[i][j];
            if(s[i][j] == '#'){
                black.push_back(make_pair(i, j));
            }
        }
    }
    int res=0;
    queue<pair<int, int>> que;
    vector<vector<int>> dist(H, vector<int>(W, -1));
    for(int i=0; i<black.size(); i++){
        int sx, sy;
        tie(sx, sy)=black[i];
        dist[sx][sy]=0;
        que.push(make_pair(sx, sy));
    }
    while(!que.empty()){
        int sx, sy;
        tie(sx, sy)=que.front();
        que.pop();
        for(int j=0; j<4; j++){
            int x=sx+dx[j];
            int y=sy+dy[j];
            if(x<0 || x>=H || y<0 || y>=W) continue;
            if(s[x][y] == '#') continue;
            if(dist[x][y] != -1) continue;
            dist[x][y]=dist[sx][sy]+1;
            res=max(res, dist[x][y]);
            que.push(make_pair(x, y));
        }
    }
    cout << res << endl;
    return 0;
}

Measured memory_bytes: 21224
n, m = gets.split.map(&:to_i)
a = []
n.times do
  a << gets.split.map(&:to_i)
end
b = []
m.times do |i|
  b << gets.to_i
end
n.times do |i|
  sum = 0
  m.times do |j|
    sum += a[i][j] * b[j]
  end
  puts sum
end

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

using namespace std;

typedef long long ll;

int main(){
  string s;
  cin>>s;

  ll c[256]={},n=s.size(),ans=1;

  
  for(ll i=0;i<n;i++){
    c[s[i]]++;
    ans+=i+1-c[s[i]];

  }

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


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

int N, A[105];

signed main(){
  
  cin>>N;

  for(int i=0;i<N;i++) cin>>A[i];
  
  int ans = 0, cnt = 1, color = -1;
  
  for(int i=0;i<N;i++){
    
    if( A[i] == color ) cnt++;
    else{
      color = A[i];
      cnt = 1;
    }
    
    if( cnt % 2 == 0 ) ans++;
    
  }
  
  cout<<ans<<endl;
  
  return 0;
}


Measured memory_bytes: 256
#include<stdio.h>
int main()
{
    int a[1000],n,i;
    scanf("%d",&n);
    for(i=0;i<n;i++)
    {
        scanf("%d",&a[i]);
    }
    for(i=n-1;i>=1;i--)
    {
        printf("%d ",a[i]);
    }
    printf("%d\n",a[0]);
    return 0;
}


Measured memory_bytes: 2048
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <memory.h>
#include <math.h>
#include <assert.h>
#include <queue>
#include <map>
#include <set>
#include <string>
#include <algorithm>
#include <iostream>
#include <functional>
#include <unordered_map>
#include <unordered_set>
#include <list>
#include <bitset>
 
using namespace std;
typedef pair<int, int> Pi;
typedef long long ll;
#define pii Pi
#define pll PL
#define Fi first
#define Se second
#define pb(x) push_back(x)
#define sz(x) ((int)(x).size())
#define rep(i, n) for(int i=0;i<n;i++)
#define all(x) (x).begin(), (x).end()
typedef tuple<int, int, int> t3;
typedef pair<ll, ll> PL;
typedef long double ldouble;
 
int get(int x, int y){
	printf("? %d %d\n", x, y);
	fflush(stdout);
	char c; cin >> c;
	return c == 'Y';
}
 
int Find(vector <int> v){
	if(sz(v) == 1)return v[0];
	int m = sz(v);
	vector <int> w;
	for(int i=0;i+1<m;i+=2){
		int t = get(v[i], v[i+1]);
		if(t)w.pb(v[i+1]);
	}
	if((sz(v) % 2 == 1) && sz(w) % 2 == 0)w.pb(v.back());
	return Find(w);
}
 
void solve(){
	int a, b; scanf("%d%d", &a, &b);
	if(a <= b){
		printf("Impossible\n");
		return;
	}
	int n = a + b;
	vector <int> v;
	for(int i=0;i<n;i++)v.pb(i);
	int t = Find(v);
	char ans[4040] = {};
	ans[t] = '1';
	for(int i=0;i<n;i++)if(i != t){
		ans[i] = get(t, i) + '0';
	}
	printf("! %s\n", ans); fflush(stdout);
}
 
int main(){
	int Tc = 1; // scanf("%d\n", &Tc);
	for(int tc=1;tc<=Tc;tc++){
		// printf("Case #%d: ", tc);
		solve();
	}
	return 0;
}

Measured memory_bytes: 848
#include <stdio.h>


int main(void){
    char S[100];
    int K;
    int N;

    scanf("%d", &N);
    scanf("%s", S);
    scanf("%d", &K);

    for(int i = 0; i < N; i++){
        if(S[i] == S[K-1]){
            printf("%c", S[K-1]);
        }else{
            printf("*");
        }
    }

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


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

struct UnionFind {
    vector<int> par;
    
    UnionFind(int n) : par(n, -1) { }
    void init(int n) { par.assign(n, -1); }
    
    int root(int x) {
        if (par[x] < 0) return x;
        else return par[x] = root(par[x]);
    }
    
    bool issame(int x, int y) {
        return root(x) == root(y);
    }
    
    bool merge(int x, int y) {
        x = root(x); y = root(y);
        if (x == y) return false;
        if (par[x] > par[y]) swap(x, y); // merge technique
        par[x] += par[y];
        par[y] = x;
        return true;
    }
    
    int size(int x) {
        return -par[root(x)];
    }
};

int main() {
    long long N, M; cin >> N >> M;
    vector<int> A(M), B(M);
    for (int i = 0; i < M; ++i) {
        cin >> A[i] >> B[i];
        --A[i];
        --B[i];
    }  
    UnionFind uf(N);
    long long cur = N * (N-1) / 2;
    vector<long long> res;
    for (int i = 0; i < M; ++i) {
        res.push_back(cur);
            
        int a = A[M-1-i], b = B[M-1-i];
        if (uf.issame(a, b)) continue;
        
        long long sa = uf.size(a), sb = uf.size(b);
        cur -= sa * sb;
        uf.merge(a, b);
    }
        
    reverse(res.begin(), res.end());
    for (int i = 0; i < res.size(); ++i) cout << res[i] <<endl;
}

Measured memory_bytes: 3444
#include <iostream>
#include <algorithm>
#include <cmath>
#include <queue>
#include <set>
#include <map>
#include <vector>
#include <string>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <queue>
#include <set>
#include <map>
#include <vector>
#include <string>
using namespace std;
typedef long long ll;
typedef long double ld;

int main(){
string a;
cin >> a ;
if(a.substr(3,1)==a.substr(2,1) && 
a.substr(5,1)==a.substr(4,1)){
  cout << "Yes" << endl;
}else{
  cout << "No" << endl;
}
}

Measured memory_bytes: 256
h, w, d = map(int, input().split())
x = [0 for _ in range(w*h)]
y = [0 for _ in range(w*h)]

for y_ in range(h):
    inp = list(map(int, input().split()))
    for x_, a in enumerate(inp):
        x[a-1] = x_
        y[a-1] = y_
    
table = [0 for _ in range(w*h)]
for d_ in range(1,d+1):
  a = d_
  b = d_ + d
  s = 0
  while b <= h*w:
    s += abs(x[a-1]-x[b-1])+abs(y[a-1]-y[b-1])
    table[b-1] = s
    b += d
    a += d
    
q = int(input())
for _ in range(q):
  l, r = map(int, input().split())
  print(table[r-1]-table[l-1])

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

long long n;
long long k;
long long ans=0ll;

bool flag_p[10000012];
long long yaku[10000012];
vector<int> list_p;
int main(){

	for(int i=0;i<=10000010;i++){
		flag_p[i] = false;
		yaku[i] = 1;
	}

	cin>>n;
	// make p
	list_p.push_back(2);
	flag_p[1] = true;
	for(int i=1;i<=n;i++){
		if(flag_p[i] == false){
			list_p.push_back(i);
			for(int j=i;j<=n;j+=i){
				flag_p[j] = true;
				int tt = j;
				long long tc = 1;
				while(tt%i == 0){
					tt/=i;
					tc++;
				}
				yaku[j]*=tc;
			}
		}

		// slv
		/*
		long long cnt = 1;
		int t = i;
		for(int j=0;j<list_p.size() && t!=1;j++){
			long long cc = 1;
			while(t%list_p[j] == 0 && t!=1){
				t /= list_p[j];
				cc++;
			}
			cnt *= cc;
		}	
		ans += cnt*(long long)i;
		*/
		ans += (long long)i * yaku[i];
	}

	/*
	for(int i=1;i<=n;i++){
		long long cnt = 1;
		int t = i;
		for(int j=0;j<list_p.size() && t!=1;j++){
			long long cc = 1;
			while(t%list_p[j] == 0 && t!=1){
				t /= list_p[j];
				cc++;
			}
			cnt *= cc;
		}	
		ans += cnt*(long long)i;
	}
	*/

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

Measured memory_bytes: 95276
n = int(input())
a = int(input())
b = int(input())
c = int(input())
d = int(input())
e = int(input())
lis = [a, b, c, d, e]
minn = min(lis)
m = 0
if n % minn == 0:
    m = n // minn
else:
    m = n // minn + 1
print(m + 4)

Measured memory_bytes: 3064
import java.util.*;

class Main{
	public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
	
      String s = sc.next();
      char[] ss = s.toCharArray();
      
      int n = 0;
      for(int i=0;i<s.length();i++){
        	if(i%2 == 0){
              if(ss[i] == 'L'){
                n++;
              }
            } else {
				if(ss[i] == 'R'){
                	n++;
                }            
            }
      }
      
      if(n == 0){
         System.out.println("Yes");
      } else {
        System.out.println("No");
      }
	}
}

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

struct absolute_minima{
	using ll = long long;
	
	priority_queue<pair<ll,ll>> pQ;
	priority_queue<pair<ll,ll>,vector<pair<ll,ll>>,greater<pair<ll,ll>>> sQ;
	ll pSum=0,sSum=0,pCnt=0,sCnt=0;
	
	void add(long long x,long long w){
		sQ.emplace(x,w);
		sSum += x*w;
		sCnt += w;
		while(true){
			if(pQ.size()>0&&sQ.size()>0){
				if(pQ.top().first>sQ.top().first){
					move(pCnt > sCnt);
					continue;
				}
				else if(pCnt > sCnt){
					move(true);
					continue;
				}
				else if(pCnt + sQ.top().second <= sCnt - sQ.top().second){
					move(false);
					continue;
				}
			}
			else{
				if(sQ.size()==0){
					move(true);
					continue;
				}
				else{
					if(sQ.top().second <= sCnt - sQ.top().second){
						move(false);
						continue;
					}
				}
			}
			break;
		}
	}
	
	void add(ll x){
		add(x,1LL);
	}
	
	ll median(){
		return sQ.top().first;
	}
	
	vector<ll> medians(){
		vector<ll> ret;
		if((pCnt+sCnt)%2==0){
			if(pCnt==sCnt)ret.push_back(pQ.top().first);
			else ret.push_back(sQ.top().first);
		}
		ret.push_back(median());
		return ret;
	}
	
	ll distance_sum(){
		ll ret = 0LL;
		ret -= pSum;
		ret += sSum;
		ret += pCnt * median();
		ret -= sCnt * median();
		return ret;
	}
	
	void move(bool f){
		if(f){
			if(pQ.size()>0){
				pair<ll,ll> p = pQ.top();
				pQ.pop();
				pSum -= p.first*p.second;
				pCnt -= p.second;
				sQ.push(p);
				sSum += p.first*p.second;
				sCnt += p.second;
			}
		}
		else{
			if(sQ.size()>0){
				pair<ll,ll> p = sQ.top();
				sQ.pop();
				sSum -= p.first*p.second;
				sCnt -= p.second;
				pQ.push(p);
				pSum += p.first*p.second;
				pCnt += p.second;
			}
		}
	}
	
};

int main(){
	
	long long S = 0LL;
	absolute_minima A;
	
	int Q;
	scanf("%d",&Q);
	
	rep(i,Q){
		int t;
		scanf("%d",&t);
		
		if(t==1){
			long long a,b;
			scanf("%lld %lld",&a,&b);
			A.add(a);
			S += b;
		}
		else{
			printf("%lld %lld\n",A.medians()[0],S + A.distance_sum());
		}
	}
	
    return 0;
}



Measured memory_bytes: 6740
#include <iostream>
#include <fstream>
#include <algorithm>
#include <chrono>
#include <random>
#include <vector>
#include <string>
#include <cmath>
#include <bitset>
#include <list>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <cassert>

#define sq(x) ((x) * (x))
#define err(x) cerr << "["#x"]  " << (x) << "\n";
#define errp(x) cerr << "["#x"]  {" << ((x).first) << ", " << ((x).second) << "}\n";
#define errv(x) {cerr << "["#x"]  [";for(const auto& ___ : (x)) cerr << ___ << ", "; cerr << "]\n";}
#define errvn(x, n) {cerr << "["#x"]  [";for(auto ___ = 0; ___ < (n); ++___) cerr << (x)[___] << ", "; cerr << "]\n";}
#define errf() cerr << "\n\n";

//#pragma GCC optimize("O3")
//#pragma GCC optimize("Ofast, unroll-loops", "omit-frame-pointer","inline")
//#pragma GCC option("arch=native","tune=native","no-zero-upper")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native,avx2")

using namespace std;

using ll = long long;
using ull = unsigned long long;
using ld = long double;

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

int getrnd(int l, int r) {
    return uniform_int_distribution<int>(l, r)(rng);
}

ll getrndll(ll l, ll r) {
    return uniform_int_distribution<ll>(l, r)(rng);
}

template <typename T1, typename T2>
inline bool relax(T1& a, const T2& b) {
    if (a > b) {
        a = b;
        return 1;
    }
    return 0;
}

template <typename T1, typename T2>
inline bool strain(T1& a, const T2& b) {
    if (a < b) {
        a = b;
        return 1;
    }
    return 0;
}

// :::::::::::::::::::::::::::::::

const int N = 2e5 + 5, INF = 2e9;

int a[N], res[N], dp[N];
vector<int> graph[N];

int pos = 0;
void dfs(int v, int p) {
    int i = upper_bound(dp, dp + N, a[v]) - dp;
    int save = dp[i];
    
    if (dp[i - 1] < a[v] && a[v] < dp[i])
        dp[i] = a[v];
    while (dp[pos + 1] < INF) ++pos;
    
    res[v] = pos;
    for (int to : graph[v]) {
        if (to == p) continue;
        
        dfs(to, v);
    }
    
    dp[i] = save;
    while (dp[pos] >= INF) --pos;
}

void solve() {
    int n;
    cin >> n;
    
    for (int i = 0; i < n; ++i)
        cin >> a[i];
    for (int i = 0; i < n - 1; ++i) {
        int x, y;
        cin >> x >> y;
        
        --x; --y;
        graph[x].emplace_back(y);
        graph[y].emplace_back(x);
    }
    
    fill(dp, dp + N, INF);
    dp[0] = -INF;
    dfs(0, 0);
    
    for (int i = 0; i < n; ++i)
        cout << res[i] << '\n';
}

int main() {
    ios::sync_with_stdio(0);
    cin.tie(nullptr); cout.tie(nullptr);
    srand(time(0));
    
    int t = 1;
//    cin >> t;
    while (t--)
        solve();
    
    return 0;
}



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

#define REP(i,a,n) for(int i=a;i<n;++i)
#define REPL(i,a,n) for(ll i=a;i<n;++i)
#define RUP(a,b) ((a+b-1)/(b))
#define ENT "\n"
#define SORTVG(v) sort(v.begin(),v.end(),greater<>())
#define SORTV(v) sort(v.begin(),v.end())
#define ALL(v) (v).begin(),(v).end()
#define MOD 1000000007
#define INF LLONG_MAX/2
#define int long long

typedef long long ll;
typedef pair<int,int> Pii;
typedef vector<int> Vi;

template<class T> void chmax(T& a, T b) {if(a < b){a=b;}}
template<class T> void chmin(T& a, T b) {if(a > b){a=b;}}
template<class T> void YesNo(T& a) {if(a){cout << "Yes" << ENT;}else{cout << "No" << ENT;}}
template<class T> void YESNO(T& a) {if(a){cout << "YES" << ENT;}else{cout << "NO" << ENT;}}
template<class T> void AorB(T& a, string A,string B) {if(a){cout << A << ENT;}else{cout << B << ENT;}}

int atcoder(){
    int h,w;
    cin>>h>>w;
    vector<string> s(h);
    vector<Vi> v(h,Vi(w,-1));
    queue< tuple<int,int,int> > q;
    REP(i,0,h){
        cin>>s[i];
        REP(j,0,w) if(s[i][j]=='#'){q.push(make_tuple(i,j,0));v[i][j]=0;}
    }
    
    while(q.size()>0){
        int nowy=get<0>(q.front());
        int nowx=get<1>(q.front());
        int depth=get<2>(q.front());
        q.pop();
        if(nowx>0) if(v[nowy][nowx-1]==-1) {q.push(make_tuple(nowy,nowx-1,depth+1)); v[nowy][nowx-1]=depth+1;}
        if(nowx<w-1) if(v[nowy][nowx+1]==-1) {q.push(make_tuple(nowy,nowx+1,depth+1)); v[nowy][nowx+1]=depth+1;}
        if(nowy>0) if(v[nowy-1][nowx]==-1) {q.push(make_tuple(nowy-1,nowx,depth+1)); v[nowy-1][nowx]=depth+1;}
        if(nowy<h-1) if(v[nowy+1][nowx]==-1) {q.push(make_tuple(nowy+1,nowx,depth+1)); v[nowy+1][nowx]=depth+1;}
    }
    int ans=0;
    REP(i,0,h) REP(j,0,w) chmax(ans,v[i][j]);
    cout<<ans<<ENT;
    return 0;
}

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


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

#define For(i,a,b) for(long long (i) = (a);(i) < (b);i++)
#define Rep(i,n) For((i),0,(n))
#define All(a) (a).begin(),(a).end()
#define sp " "
#define INF 1e18
#define INT_INF 1e9

typedef long long int ll;
const ll MOD = 1000000007ll;
const ll MOD_9 = 998244353;

int main(){
    ll N;
    cin >> N;
    vector<ll> A(N);
    vector<ll> cnt(64,0ll);
    Rep(i,N){
        cin >> A[i];
        Rep(j,64){
            if(A[i] & (1ll << j)){
                cnt[j] += 1ll;
                //cout << i << sp << j;
            }
        }
    }
    ll ans = 0ll;
    ll pow_2 = 1ll;
    Rep(j,64){
        //cout << cnt[j] << endl;
        ll t = (N-cnt[j])*(cnt[j]);
        t %= MOD;
        ans += pow_2*t;
        ans %= MOD;
        pow_2 *= 2ll;
        pow_2 %= MOD;
    }
    cout << ans;
    //cin >> N;
}

Measured memory_bytes: 2560
import java.util.Scanner;

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

		int A = sc.nextInt();
		int B = sc.nextInt();
		int C = sc.nextInt();

		int result = C-(A-B) >=0 ? C-(A-B) : 0;

		System.out.println(result);
		sc.close();
	}
}


Measured memory_bytes: 22996
mod = 10**9+7

import sys
input = sys.stdin.buffer.readline

def main():

    n, k = map(int, input().split())
    A = list(map(int, input().split()))

    PZ = []
    N = []
    for a in A:
        if a >= 0:
            PZ.append(a)
        else:
            N.append(a)

    flag = True
    if len(PZ) > 0:
        if n == k:
            if len(N)%2 != 0:
                flag = False
    else:
        if k%2 != 0:
            flag = False

    if flag:
        if k%2 == 0:
            PZ.sort(reverse=True)
            N.sort()
            s = len(PZ)
            t = len(N)
            C = []
            for i in range(s//2):
                C.append(PZ[2*i]*PZ[2*i+1])
            for i in range(t//2):
                C.append(N[2*i]*N[2*i+1])
            C.sort(reverse=True)
            ans = 1
            for i in range(k//2):
                ans *= C[i]
                ans %= mod
        else:
            PZ.sort()
            ans = PZ.pop()
            PZ.reverse()
            N.sort()
            s = len(PZ)
            t = len(N)
            C = []
            for i in range(s//2):
                C.append(PZ[2*i]*PZ[2*i+1])
            for i in range(t//2):
                C.append(N[2*i]*N[2*i+1])
            C.sort(reverse=True)
            for i in range(k//2):
                ans *= C[i]
                ans %= mod
    else:
        ABS = [abs(a) for a in A]
        ABS.sort()
        ans = 1
        for i in range(k):
            ans *= ABS[i]
            ans %= mod
        ans *= -1
        ans %= mod
    print(ans)

if __name__ == '__main__':
    main()


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

int main(){
    int n;
    string s;
    cin >> n >> s;
    s=s[n-1]+s+s[0];
    vector<bool> ss(n+2),sw(n+2),ws(n+2),ww(n+2);
    ss[0]=true; ss[1]=true;
    sw[0]=true; sw[1]=false;
    ws[0]=false; ws[1]=true;
    ww[0]=false; ww[1]=false;

    vector<bool> animal(n+2);

    for(int i=0;i<4;i++){
        if(i==0) animal=ss;
        if(i==1) animal=sw;
        if(i==2) animal=ws;
        if(i==3) animal=ww;

        for(int i=1;i<=n;i++){
            if(s[i]=='o'){
                //sheep says o
                if(animal[i]){
                    animal[i+1]=animal[i-1];
                }//wolf says o
                else if(!animal[i]){
                    animal[i+1]=!animal[i-1];
                }
            }else{
                //sheep says x
                if(animal[i]){
                    animal[i+1]=!animal[i-1];   
                }//wolf says x
                else if(!animal[i]){
                    animal[i+1]=animal[i-1];
                }
            }
        }
        if(animal[0]==animal[n]&&animal[1]==animal[n+1]){
            for(int i=1;i<=n;i++){
                if(animal[i]) cout << "S";
                else cout << "W";
            }
            cout << endl;
            return 0;
        }
    }
    cout << "-1" << endl;
}

Measured memory_bytes: 3780
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        long l = sc.nextLong(), r = sc.nextLong();
        long res = Long.MAX_VALUE;
        r = Math.min(l + 2019L, r);
        for(long i = l; i <= r; i++) {
            for(long j = i + 1; j <= r; j++) {
                res = Math.min(res, (i * j) % 2019L);
            }
        }
        System.out.println(res);
    }
}


Measured memory_bytes: 21844
n,m,X = map(int, input().split())
a = [list(map(int, input().split())) for _ in range(n)]

ans = []
for i in range(2**n):
    pat = [0] * n #買うかどうか
    r = [0] * m #理解度
    money = 0
    for j in range(n):
        if ((i>>j)&1):
            pat[n-1-j] = 1
    for x in range(len(pat)):
        if pat[x] == 1:
            money += a[x][0]
            for y in range(m):
                r[y] += a[x][y+1]
    flag = True
    for z in range(m):
        if r[z] < X:
            flag = False
            break
    if flag:
        ans.append(money)
print(min(ans) if ans else -1)

Measured memory_bytes: 9184
#include <iostream>
#include <vector>
#include <queue>
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <map>
#include <deque>
using namespace std;

#define REP(i,n) for(int i = 0; i < n; i++)
#define ALL(v) v.begin(),v.end()
typedef long long ll;
typedef pair<int,int> pi;
typedef pair<ll,ll> pll;
typedef pair<string,string> pss;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<pi> vpi;
typedef vector<ll> vll;
typedef vector<vll> vvll;

double EPS = 1e-9;
int INFi = 1000000005;
long long INFll = 1000000000000000005ll;
double PI = acos(-1);
int dirx[8] = {-1,0,0,1,-1,-1,1,1};
int diry[8] = {0,1,-1,0,-1,1,-1,1};
ll MOD = 1000000007;

const int MAX_N = 2000;

int N, K, Q;
int A[MAX_N];

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

  REP(i, N) cin >> A[i];

  int ans = INFi;

  REP(i, N) {
    int X = A[i];
    vvi vec;
    vi tmp;
    REP(j, N) {
      if(A[j] >= X) {
        tmp.push_back(A[j]);
      } else {
        vec.push_back(tmp);
        tmp.clear();
      }
      if(j == N - 1 && !tmp.empty()) vec.push_back(tmp);
    }

    vi rms;
    REP(j, vec.size()) {
      sort(ALL(vec[j]));
      if(vec[j].size() >= K) {
        REP(k, vec[j].size() - K + 1) {
          rms.push_back(vec[j][k]);
        }
      }
    }
    sort(ALL(rms));
    if(rms.size() >= Q) ans = min(ans, rms[Q - 1] - X);
  }

  cout << ans << endl;


  return 0;
}


Measured memory_bytes: 384
import java.util.Scanner;

public class Main {
	public static void sort(int[] a,int i){
		int key=a[i];
		int j=i-1;
		while(j>=0 && a[j]>key){
			a[j+1]=a[j];
			j--;
		}
		a[j+1]=key;
	}
	public static void main(String[] args) {
		Scanner sc=new Scanner(System.in);
		int n=sc.nextInt();
		int[] a=new int[n];
		for (int i=0;i<n;i++){
			a[i]=sc.nextInt();
		}
		for(int i=0;i<n;i++){
			sort(a,i);
			for(int k=0;k<n-1;k++){
				System.out.print(a[k]+" ");
			}
			System.out.println(a[n-1]);;
		}
	}
}

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

int main() {
  int n;
  cin >> n;
  vector<vector<int>> dp(n + 1, vector<int>(3, 0));
  for (int i = 0; i < n; i++) {
    vector<int> a(3);
    for (int j = 0; j < 3; j++) cin >> a[j];
    for (int j = 0; j < 3; j++) {
      for (int k = 0; k < 3; k++) {
        if (j == k) continue;
        dp[i + 1][j] = max(dp[i + 1][j], dp[i][k] + a[j]);
      }
    }
  }
  int ans = 0;
  for (int i = 0; i < 3; i++) {
    ans = max(ans, dp[n][i]);
  }
  cout << ans << endl;
}


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

using ll = long long;

ll N, K;

void solve(){
    if(N >= K) N %= K;
    ll ans = N;
    ans = min(ans, K - N);
    cout << ans;
}
int main(){
    cin >> N >> K;
    solve();
    return 0;
}

Measured memory_bytes: 256
import java.util.Scanner;
 
public class Main {
 
	public static final MyScanner in = new MyScanner();

	public static int answer=Integer.MAX_VALUE;

	public static int N;
	public static int M;
	public static int X;

	public static int[] C;
	public static int[][] A;

	public static void main(String[] args) {


		N = in.nextInt();
		M = in.nextInt();
		X = in.nextInt();

		C = new int[N];
		A = new int[N][M];

		for(int i=0;i<N;i++) {
			C[i] = in.nextInt();
			for(int j=0;j<M;j++) {
				A[i][j] = in.nextInt();
			}
		}

		s(0, 0, new int[M]);

		System.out.println(answer == Integer.MAX_VALUE ? -1 : answer);
	}

	public static void s(int c, int idx, int[] m) {
		if(idx >= N) return;

		// 選ばない場合と選ぶ場合
		{
			s(c, idx+1,m);
		}
		{
			int tmp = c + C[idx];
			int[] tmpm = new int[M];
			for(int j=0;j<M;j++) tmpm[j] = m[j] + A[idx][j];
			boolean end=true;
			for(int j=0;j<M;j++) if(tmpm[j] < X) end=false;
			if(end) {
				//System.out.print(tmp + " ");
				//for(int j=0;j<M;j++) System.out.print(tmpm[j] + " ");
				//System.out.println();
				answer = Math.min(answer, tmp);
			}else {
				s(tmp, idx+1, tmpm);
			}
		}

	}

	public static class MyScanner {
		Scanner sc = new Scanner(System.in);
		String next() { return sc.next();}
		int nextInt() { return Integer.valueOf(sc.next());}
		long nextLong() { return Long.valueOf(sc.next()); }
	}
}

Measured memory_bytes: 38188
#include <bits/stdc++.h>
#include <math.h>
#define rep(i, n) for(int i = 0; i < (n); i++)
#define rrep(i, n) for(int i = 0; i <= (n); i++)
using namespace std;
typedef long long ll;
const int MOD = 1000000007;
const ll INF = 1LL<<60;

int main(){
    ll N,A,B,C,D,E;
    cin >> N >> A >> B >> C >> D >> E;
    
    ll min_move = min({A,B,C,D,E});

    cout << ((N + min_move - 1) / min_move) + 4 << endl;
}

Measured memory_bytes: 256
<?php

while ($stdin = fgets(STDIN)) {
    $line[] = trim($stdin);
}
foreach ($line as $key => $value) {
    $data[] = explode(' ', $value);
}

foreach ($data as &$line) {
    $line = array_map(function($val) { return (int)$val; }, $line);
}


$N = $data[0][0];
$A = [];
$B = [];

for($i = 1; $i <= $N; $i++) {
    $tasks[$i] = [
        'A' => $data[$i][0],
        'B' => $data[$i][1],
    ];
}

$BArr = [];
foreach ($tasks as $key => $task) {
    $BArr[$key] = $task['B'];
}

array_multisort($BArr, SORT_ASC, $tasks);

$time = 0;
$success = true;
foreach ($tasks as $task) {
    $time += $task['A'];
    //var_dump('締め切り' . $task['B'] . '今' . $time);
    if ($time > $task['B']) {
        $success = false;
    }
}

$ans = $success ? 'Yes' : 'No';

printf('%s', $ans);


Measured memory_bytes: 224092
a=int(input())
b=int(input())
c=int(input())
x=int(input())
cnt=0
for i in range(a+1):
    for j in range(b+1):
        for k in range(c+1):
            if 500*i+100*j+50*k==x:
                cnt+=1
print(cnt)

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

struct Point { double x,y;} p[4];

bool isIntersect(int i, int j, int k, int l){
	double kd = ((p[i].x - p[j].x) * (p[k].y - p[i].y) + (p[i].y - p[j].y) * (p[i].x - p[k].x));
	double ld = ((p[i].x - p[j].x) * (p[l].y - p[i].y) + (p[i].y - p[j].y) * (p[i].x - p[l].x));
	return (kd*ld<0);
}

int main(){
	while(1){
		if(cin.eof()) break;
		for(int i=0; i<4; i++)
			cin >> p[i].x >> p[i].y;
		if(!cin.eof()){
			if (isIntersect(0, 1, 2, 3) || isIntersect(0, 2, 1, 3) || isIntersect(1, 2, 0, 3))
				cout << "NO";
			else cout << "YES";
			cout << endl;
		}
	}
	return 0;
}

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

int main()
{
    int a;
    bool s[31];

    for( int i = 0; i < 31; i++ )
        s[i] = false;

    for( int i = 0; i < 28; i++ )
    {    
        cin >> a;
        s[a] = true;
    }

    for( int i = 1; i < 31; i++ )    
        if( s[i] == false )
            cout << i << endl;

    return 0;
}


Measured memory_bytes: 3084
n = int(input())

a = []
for i in range(2):
    a.append(list(map(int, input().split())))

b = []


def solve(i, j, k):
    if i >= 2 or j >= n:
        b.append(k)

    else:
        k += a[i][j]
        solve(i + 1, j, k)
        solve(i, j + 1, k)


solve(0, 0, 0)
print(max(b))


Measured memory_bytes: 3188
if __name__ == "__main__":
    N, M = list(map(int, input().split()))
    A = list(map(int, input().split()))
    sorted_A = sorted(A, reverse=True)
    num_of_vote = sum(A)
    flag = True
    for i in range(M):
        if sorted_A[i]*4*M < num_of_vote:
            flag = False
            break

    if flag:
        print('Yes')
    else:
        print('No')

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

#define int long long
#define fast_io(); ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define loop(i,a,b) for(int i=a;i<b;i++)
#define all(x) x.begin(),x.end()
#define us unordered_set
#define um unordered_map
#define ms multiset
#define mm multimap
#define pb push_back
#define pf push_front
#define ins insert
#define mp make_pair
#define mt make_tuple
#define fi first
#define se second
#define _max max_element
#define _min min_element
#define tc(T) int T; cin>>T; while(T--)
#define q(q) int q; cin>>q; while(q--)
#define pi 3.1415926535897932384626433832
const int pinf=((int)2e18);
const int ninf=((int)-2e18);
const int mod=1000000007;

using namespace std;

int power(int x,int n,int M)
{
    if(n==0)
        return 1;
    else if(n%2 == 0)
        return power((x*x)%M,n/2,M);
    else
        return (x*power((x*x)%M,(n-1)/2,M))%M;
}

bool vowel(char c)
{
    return(c=='a' or c=='e' or c=='i' or c=='o' or c=='u' or c=='A' or c=='E' or c=='I' or c=='U' or c=='O');
}

int32_t main()
{
    fast_io();

    //FILE *fptr1=freopen("in.txt","r",stdin);
    //FILE *fptr2=freopen("out.txt","w",stdout);

    int n;
    cin>>n;
    vector <int> a(n);
    map <int,int> m;
    int s=0;
    loop(i,0,n)
    {
        cin>>a[i];
        s+=a[i];
        m[a[i]]++;
    }
    q(q)
    {
        int b,c;
        cin>>b>>c;
        s-=(b*m[b]);
        m[c]+=m[b];
        s+=(c*m[b]);
        m[b]=0;


        cout<<s<< "\n";

    }


    return 0;
}


Measured memory_bytes: 9948
#include<cmath>
#include<math.h>
#include<ctype.h>
#include<algorithm>
#include<bitset>
#include<cassert>
#include<cctype>
#include<cerrno>
#include<cfloat>
#include<ciso646>
#include<climits>
#include<clocale>
#include<complex>
#include<csetjmp>
#include<csignal>
#include<cstdarg>
#include<cstddef>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<ctime>
#include<cwchar>
#include<cwctype>
#include<deque>
#include<exception>
#include<fstream>
#include<functional>
#include<iomanip>
#include<ios>
#include<iosfwd>
#include<iostream>
#include<istream>
#include<iterator>
#include<limits>
#include<list>
#include<locale>
#include<map>
#include<memory>
#include<new>
#include<numeric>
#include<ostream>
#include<queue>
#include<set>
#include<sstream>
#include<stack>
#include<stdexcept>
#include<streambuf>
#include<string>
#include<typeinfo>
#include<utility>
#include<valarray>
#include<vector>
#include<string.h>
#include<stdlib.h>
#include<stdio.h>
#include<bits/stdc++.h>
using namespace std;
int main()
{
	int a,b;
	cin>>a>>b;
	if(a<b)
	cout<<"0";
	else
	cout<<(a-b);
	return 0;
}

Measured memory_bytes: 256
#include<stdio.h>
#include<stdlib.h>
int main(void)
{
  int N,M,i,k,tmp;
  scanf("%d %d",&N,&M);
  int A[N];
  for(i=0;i<N;i++) scanf("%d",&A[i]);
  for(k=1;k<=M;k++)
  {
    for(i=0;i<N-1;i++)
    {
      if(A[i]%k>A[i+1]%k)
      {
        tmp=A[i];
        A[i]=A[i+1];
        A[i+1]=tmp;
      }
    }
  }
  for(i=0;i<N;i++) printf("%d\n",A[i]);
  return 0;
}



Measured memory_bytes: 2100
#include <stdio.h>
int main() {
	int h,w,n,a,t = 0,y = 0;
	scanf("%d %d",&h,&w);
	scanf("%d",&n);
	int c[h][w];
	for(int i = 1; i <= n; i++) {
		scanf("%d",&a);
		for(int j = 0; j < a; j++) {
			c[t][y] = i;
			if(t%2 == 0) {
				if(y == w - 1) t++;
				else y++;
			} else {
				if(y == 0) t++;
				else y--;
			}
		}
	}
	for(int i = 0; i < h; i++) {
		for(int j = 0; j < w; j++) printf("%d ",c[i][j]);
		printf("\n");
	}
	return 0;
}
			

Measured memory_bytes: 256
#include <iostream> // cout, endl, cin
#include <string> // string, to_string, stoi
#include <vector> // vector
#include <algorithm> // min, max, swap, sort, reverse, lower_bound, upper_bound
#include <utility> // pair, make_pair
#include <tuple> // tuple, make_tuple
#include <cstdint> // int64_t, int*_t
#include <cstdio> // printf
#include <map> // map
#include <queue> // queue, priority_queue
#include <set> // set
#include <stack> // stack
#include <deque> // deque
#include <unordered_map> // unordered_map
#include <unordered_set> // unordered_set
#include <bitset> // bitset
#include <cctype> // isupper, islower, isdigit, toupper, tolower, islower
#include <math.h>

#define rep(i,n) for(int i = 0; i < n; i++)
using namespace std;
//using ll = long long; using ld = long double;
//using P = pair<int, int>;

long long n, m, k, x, y, z;
const long long template_arry_size = 1000625;
long long a[template_arry_size];
long long b[template_arry_size];
long long c[template_arry_size];
string s;
long long ans = 0;

vector<long long> in_row[300005];
multiset<long long> best;

void upd(long long pos, long long delta) {
    best.erase(best.find(c[pos]));
    c[pos] += delta;
    best.insert(c[pos]);
}

void solve(){
    cin >> n >> m >> k;
    rep(i,m) {
        c[i] = 0;
        best.insert(0);
    }
    rep(i,k) {
        cin >> x >> y;
        --x; --y;
        in_row[x].push_back(y);
        upd(y,1);
        //rep(i,in_row[x].size()) cout << x << ' ' <<in_row[x][i] << endl;
    }

    ans = 0;
    for (long long i = 0; i < n; i++) {
        long long cur = in_row[i].size();

        for (long long x: in_row[i]) {
            upd(x, -1);
        }

        if (best.size()) {
            cur += *best.rbegin();
        }

        ans = max(ans, cur);
        
        for (long long x: in_row[i]) {
            upd(x,1);
        }
        
    }
    cout << ans << endl;
}

int main(){

    solve();
    
}


Measured memory_bytes: 36376
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MM = 1000000000;
const int MOD = MM + 7;
const int MAX = 510000;
#define rep(i, n) for(ll i=0; i<n; i++)
#define Rep(i, j, n) for(ll i=j; i<n; i++)
#define all(vec) vec.begin(), vec.end()
template<class T> inline bool chmin(T& a, T b) {if(a > b) {a = b; return true;} return false;}
template<class T> inline bool chmax(T& a, T b) {if(a < b) {a = b; return true;} return false;}
const ll INF = 1LL << 60;
const double pi = acos(-1.0);

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

Measured memory_bytes: 3600
#include <bits/stdc++.h>
#define rep(i,n) for(int i=0; i<(n); i++)
#define FOR(i,a,n) for(int i=(a); i<(n); i++)
#define all(v) v.begin(), v.end()
#define fi first
#define se second
#define sz(x) int(x.size())
using namespace std;
using ll = long long;
using P = pair<int ,int>;
const int INF = 1001001001;
const int MOD = 1000000007;
template<class T> inline bool chmax(T &a, T b) { if (a<b) {a=b; return 1;} return 0;}
template<class T> inline bool chmin(T &a, T b) { if (b<a) {a=b; return 1;} return 0;}

#ifdef _DEBUG
#include "debug.h"
#else
#define debug(...)
#define print(...)
#endif

vector<vector<P>> G;
vector<int> loc;
vector<bool> seen;
bool dfs(int v) {
    if (loc[v]==INF) loc[v] = 0;
    seen[v] = true;
    bool ok = true;
    for(const auto& p: G[v]) {
        int nv = p.fi, tmp = loc[v]+p.se;
        debug(seen[nv], v, nv, tmp);
        if (seen[nv] && loc[nv]!=tmp) return false;
        if (!seen[nv]) {
            loc[nv] = tmp;
            if (!dfs(nv)) ok = false;
        }
    }
    return ok;
}

int main() {
    int n, m; cin >> n >> m;
    G.resize(n);
    rep(i, m) {
        int u, v, cost; cin >> u >> v >> cost;
        u--; v--;
        G[u].emplace_back(v, cost);
        G[v].emplace_back(u, -cost);
    }
    loc.assign(n, INF);
    seen.assign(n, false);
    rep(i, n) {
        if (seen[i]) continue;
        if (!dfs(i)) {
            cout << "No" << endl;
            return 0;
        }
        debug(seen);
        debug(loc);
    }
    cout << "Yes" << endl;
    return 0;
}

Measured memory_bytes: 12032
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <iostream>
#include <queue>
#include <vector>
#include <string>
#include <sstream>
#include <utility>
#include <deque>
#include <numeric>
#include <map>
#define rep(i,n) for (int i = 0; i < (n); ++i)
#define INF 1000000000000

using namespace std;
 
typedef pair<int,int> pii;
typedef vector<int> vi;
typedef long long ll;

int main(void){
	int n,k;
	cin >> n >> k;

	vector<int> x(n),y(n);
	vector<int> xary,yary;

	for(int i = 0; i < n; ++i){
		cin >> x[i] >> y[i];
		xary.push_back(x[i]);
		yary.push_back(y[i]);
	}

	sort(begin(xary),end(xary));
	sort(begin(yary),end(yary));

	ll ans = 1LL * (xary[n-1] - xary[0]) * (yary[n-1] - yary[0]);

	for(int xi = 0; xi < n; ++xi){
		for(int xj = xi + 1; xj < n;++xj){
			for(int yi = 0; yi < n; ++yi){
				for(int yj = yi + 1; yj < n;++yj){
					int num = 0;
					ll lx = xary[xi],rx = xary[xj];
					ll by = yary[yi],uy = yary[yj];

					for(int i = 0; i < n;++i){
						if(lx <= x[i] and x[i] <= rx and by <= y[i] and y[i] <= uy){
							num++;
						}
					}
					if(num >= k) ans = min(ans,1LL * (rx - lx) * (uy - by));
				}
			}
		}
	}
	cout << ans << endl;
	return 0;
}

Measured memory_bytes: 512
//Himanshu Shukla
#pragma GCC optimize("O2")
#include <bits/stdc++.h>
using namespace std;
#define FASTIO ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0)
//#define multitest
#define ll long long
#define v32 vector<int>
#define v64 vector<ll>
#define s32 set<int>
#define s64 set<ll>
#define p32 pair<int, int>
#define p64 pair<ll, ll>
#define sz(v) (ll)(v).size()
#define fi first
#define se second
#define ln '\n'
#define debug(x) cerr << (#x) << ": " << (x) << ln
inline ll add(ll a, ll b, ll m)
{
	return (((a % m + b % m) % m + m) % m);
}
inline ll mul(ll a, ll b, ll m) { return (((a % m * b % m) % m + m) % m); }
const ll MOD = 1e9 + 7;
template <typename T, typename U>
ostream &operator<<(ostream &out, const pair<T, U> &p)
{
	out << '[' << p.fi << ", " << p.se << ']';
	return out;
}
template <template <typename, typename...> class ContainerType, typename ValueType, typename... Args>
void print_container(const ContainerType<ValueType, Args...> &c)
{
	cout << "{ ";
	for (const auto &v : c)
		cout << v << ' ';
	cout << '}' << ln;
}

void solve()
{
	ll x;
	cin >> x;
	ll ans = 1000 * (x / 500);
	x %= 500;
	ans += (x / 5) * 5;
	cout << ans << ln;
	return;
}

int main()
{
	FASTIO;
	int t = 1;
#ifdef multitest
	cin >> t;
#endif
	for (int tc = 1; tc < t + 1; tc++)
		solve();
	return 0;
}


Measured memory_bytes: 256