akhauriyash/Code-Regression huggingface dataset:
from itertools import product
D, G = map(int, input().split())
l = [tuple(map(int, input().split())) for i in range(D)]
s = [0] * D
ans = float('inf')
for b in product([0,1], repeat=D):
    s = sum([(i+1)*100 * l[i][0] + l[i][1] for i,f in enumerate(b) if f])
    tmp = sum([l[i][0] for i,f in enumerate(b) if f])
    if s < G:
        for i, j in enumerate(reversed(b)):
            if j == 0:
                d = D - i
                k = 0
                if s + 100 * d * (l[d-1][0] - 1 - k) < G:
                    tmp = float('inf')
                    break
                while True:
                    k += 1
                    if s + 100 * d * (l[d-1][0] - 1 - k) < G:
                        break
                tmp += l[d-1][0] - k 
                break
    ans = min(ans, tmp)
print(ans)


Measured memory_bytes: 3064
#include <bits/stdc++.h>
#define loop(i,n) for(int i = 0;i < (n);i++)
#define range(i,a,b) for(int i = (a);i <= (b);i++)
#define all(A) A.begin(),A.end()
#define pb push_back
#define mp make_pair
#define sz(A) ((int)A.size())
#define vi vector<int>
#define vl vector<long long>
#define vd vector<double>
#define vp vector<pair<int,int> >
#define ll long long
#define pi pair<int,int>
#define popcnt(x) __builtin_popcount(x)
#define LSOne(x) ((x) & (-(x)))
#define xx first
#define yy second
#define PQ priority_queue
#define print(A,t) cerr << #A << ": "; copy(all(A),ostream_iterator<t>(cerr," " )); cerr << endl
#define prp(p) cerr << "(" << (p).first << " ," << (p).second << ")";
#define prArr(A,n,t)  cerr << #A << ": "; copy(A,A + n,ostream_iterator<t>(cerr," " )); cerr << endl
#define PRESTDIO() cin.tie(0),cerr.tie(0),ios_base::sync_with_stdio(0)
#define what_is(x) cerr << #x << " is " << x << endl
#define bit_lg(x) (assert(x > 0),__builtin_ffsll(x) - 1)
const double PI = acos(-1);
using namespace std;

int A[1 << 20],n;
int H,W;
int color[100][100];

int main(){
	/*#ifndef ONLINE_JUDGE
		freopen("in.in", "r", stdin);
	#endif
	*/scanf("%d %d",&H,&W);
	scanf("%d",&n);
	range(i,1,n) scanf("%d",A + i);
	int cur = 1;
	int x = 0,y = 0,dy = 1;
	while(cur <= n) {
		while(A[cur]) {
			color[x][y] = cur;
			y += dy;
			if(y == -1 || y == W) {
				x++;
				dy *= -1;
				y = (y == -1) ? 0 : (W-1);
			}
			A[cur]--;
		}
		cur++;
	}

	loop(i,H) loop(j,W) printf("%d%c",color[i][j]," \n"[j==W-1]);
	return 0;
}


Measured memory_bytes: 384
N=int(input())
S=[];P=[];Q=[];X=[]
for i in range(N):
  s,p=map(str,input().split())
  S.append(s)#都市名
  P.append(int(p))#評価
  Q.append(i+1)#都市番号
T=list(map(list,zip(Q,S,P)))#都市番号/都市名/評価
T.sort(reverse=True,key=lambda x: x[2])
O=sorted(set(S))
#print("len(T)",len(T),"len(O)",len(O),N)
for i in range(len(O)):
  for j in range(len(T)):
    #print(T[j][1],O[i],i,j)
    if T[j][1]==O[i]:
      X.append(T[j][0])
for i in X:
  print(i)

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

ll calc(string S, ll rep)
{
  string temp_s = S;
  for (ll i = 0; i < rep - 1; ++i) {
    S = S + temp_s;
  }
  ll res = 0;
  ll cnt = 1;
  for (ll i = 1; i < S.size(); ++i) {
    if (S[i] == S[i - 1]) {
      cnt += 1;
    }
    else {
      if (cnt > 1) {
        res += cnt / 2;
      }
      cnt = 1;
    }
  }
  if (cnt > 1) {
    res += cnt / 2;
  }
  return res;
}

int main()
{
  string S;
  cin >> S;
  ll K;
  cin >> K;
  char temp = S[0];
  bool all_same = true;
  for (ll i = 0; i < S.size(); ++i) {
    if (S[i] == temp) {
      continue;
    }
    all_same = false;
    break;
  }
  if (all_same) {
    cout << S.size() * K / 2 << endl;
    return 0;
  }
  ll one = calc(S, 1);
  ll two = calc(S, 2);
  ll three = calc(S, 3);
  ll dif = three - two;
  ll ans = one + (K - 1) * dif;
  cout << ans << endl;
}


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

using namespace std;

int main(void){
    char a, b;
    cin >> a >> b;
    if (a == 'H') cout << b << endl;
    if (a == 'D'){
        if (b == 'H') cout << 'D' << endl;
        else cout << 'H' << endl;
    }
}

Measured memory_bytes: 256
from enum import Enum
import sys
import math

#0:中央,1:南,2:東,3:西,4:北,5:裏側
class Dice():

    def __init__(self):
        self.number = [i for i in range(6)]
        self.work = [i for i in range(6)]
        self.order = 'NNNNWNNNWNNNENNNENNNWNNN'

    def setNumber(self,n0,n1,n2,n3,n4,n5):
        self.number[0] = n0
        self.number[1] = n1
        self.number[2] = n2
        self.number[3] = n3
        self.number[4] = n4
        self.number[5] = n5

    def roll(self,loc):
        for i in range(6):
            self.work[i] = self.number[i]

        if loc == 'E':
            self.setNumber(self.work[3],self.work[1],self.work[0],self.work[5],self.work[4],self.work[2])
        elif loc == 'N':
            self.setNumber(self.work[1],self.work[5],self.work[2],self.work[3],self.work[0],self.work[4])
        elif loc == 'S':
            self.setNumber(self.work[4],self.work[0],self.work[2],self.work[3],self.work[5],self.work[1])
        elif loc == 'W':
            self.setNumber(self.work[2],self.work[1],self.work[5],self.work[0],self.work[4],self.work[3])

    def query(self,top_num,front_num):
        self.save_data = [i for i in range(6)]
        for i in range(len(self.number)):
            self.save_data[i] = self.number[i]
        ret =-1

        for i in range(len(self.order)):
            self.roll(self.order[i])
            if self.number[0] == top_num and self.number[1] == front_num:
                ret = self.number[2]
                break
        for i in range(len(self.number)):
            self.number[i] = self.save_data[i]

    def equal(self,another):
        ret = False;

        for i in range(len(self.order)):
            self.roll(self.order[i])
            FLG=True
            for k in range(6):
                if self.number[k] != another.number[k]:
                    FLG = False
                    break
            if FLG == True:
                return True

        return ret;

diceA = Dice()
diceB = Dice()
table=list(map(int,input().split()))
diceA.setNumber(table[0],table[1],table[2],table[3],table[4],table[5])

table=list(map(int,input().split()))
diceB.setNumber(table[0],table[1],table[2],table[3],table[4],table[5])

if diceA.equal(diceB):
    print("Yes")
else:
    print("No")



Measured memory_bytes: 6388
n, k = map(int, input().split())
d = []
a = []
for i in range(k):
    d.append(int(input()))
    a.append(list(map(int, input().split())))

hito = [i+1 for i in range(n)]
for i in a:
    for j in i:
        if j in hito:
            hito.remove(j)

print(len(hito))

Measured memory_bytes: 9188
def gcd(x,y):
  if y==0:
    return x
  a=x if x>=y else y
  b=y if a==x else x
  return gcd(b,a%b)

a,b,c,d=list(map(int,input().split()))
x=b//c-(a-1)//c
y=b//d-(a-1)//d
e=c//gcd(c,d)*d
z=b//e-(a-1)//e
print(b-a+1-(x+y-z))

Measured memory_bytes: 3064
#include <iostream>
#include <string>
#include <string.h>
#include <math.h>
#include <cmath>
#include <vector>
#include <algorithm>

#define ll long long

using namespace std;

const long long mod = 1e9 + 7;

struct P{
    int num;
    int count;
};
int main() {
    ll n, k; cin >> n >> k;
    vector<P> v(n);
    for(int i = 0; i < n; i++){
        cin >> v[i].num >> v[i].count;
    }
    sort(v.begin(),v.end(),[](P a, P b){return a.num<b.num;});
    /*int ans = 0;
    for(auto all: v){
        if(k<=all.count){
            cout << all.num << endl;
            return 0;
        }else{
            k-=all.count;
        }
    }*/

    ll count = 0;
    for(int i = 0; i < n; i++){
        count += v[i].count;
        if(k <= count){
            cout << v[i].num << endl;
            return 0;
        }
    }
    return 0;
}

Measured memory_bytes: 1024
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Scanner;

public class Main {

    void run() {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt(), m = sc.nextInt();

        UnionFind uf = new UnionFind(n);

        for (int i = 0; i < m; i++) {
            int x = sc.nextInt() - 1, y = sc.nextInt() - 1, z = sc.nextInt();
            uf.union(x, y);
        }

        HashSet<Integer> set = new HashSet<>();
        for (int i = 0; i < n; i++) {
            set.add(uf.find(i));
        }
        System.out.println(set.size());
    }

    class UnionFind {
        private int n, t;
        private int[] parent, rank;

        public UnionFind(int n) {
            this.n = n;
            this.parent = new int[n];
            this.rank = new int[n];
            Arrays.fill(rank, 1);
            for (int i = 0; i < n; i++) parent[i] = i;
        }

        public void union(int x, int y) {
            t++;
            int xr = find(x);
            int yr = find(y);

            if (xr > yr) {
                parent[yr] = xr;
            } else if (xr < yr) {
                parent[xr] = yr;
            } else {
                parent[yr] = xr;
                rank[yr]++;
            }

        }

        public int find(int x) {
            if (x != parent[x]) {
                parent[x] = find(parent[x]);
            }
            return parent[x];
        }

        public boolean same(int x, int y) {
            return find(x) == find(y);
        }

    }

    void debug(Object...os) {
        System.err.println(Arrays.deepToString(os));
    }

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


Measured memory_bytes: 92184
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <set>
#include <algorithm>
using namespace std;
int n,m,i,x,y,fa,fb,ans,o,a[100010],b[100010],c[100010],f[100010],j;
int find1(int x)
{
    if (f[x]==x) return(f[x]); else f[x]=find1(f[x]);
    return(f[x]);
}

set<int> s[100010];

int main()
{
scanf("%d%d",&n,&m);
for (i=1;i<=n;i++)
    scanf("%d",&a[i]);
for (i=1;i<=n;i++)
    f[i]=i;
for (i=1;i<=m;i++)
{
    scanf("%d%d",&x,&y);
    fa=find1(x);
    fb=find1(y);
    if (fa!=fb)
        f[fa]=fb;
}
for (i=1;i<=n;i++)
  s[find1(i)].insert(i);
ans=0;
for (i=1;i<=n;i++)
if (!s[i].empty())
{
    o=0;
 set<int>::iterator it;
 for (it=s[i].begin();it!=s[i].end();it++)
     {
         o+=1;
         b[o]=a[*it];
         c[o]=*it;
     }
  sort(b+1,b+o+1);
  for (j=1;j<=o;j++)
   {
       int p=lower_bound(b+1,b+o+1,c[j])-b;
       if (p<=o&&p>=1&&b[p]==c[j]) ans++;
   }
}

printf("%d\n",ans);
    return(0);
}


Measured memory_bytes: 11136
import java.util.Scanner;

public class Main {
	
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		long a = sc.nextLong();
		long b = sc.nextLong();
		System.out.println(fanc(a, b));
	}
	static long fanc(long a, long b) {
		long an = a / 4;
		long bn = b / 4;
		if (an == bn) {
			return XOR(a, b);
		} else {
			return XOR(a, an * 4 + 3) ^ XOR(bn * 4, b);
		}
	}
	static long XOR(long a, long b) {
		long ans = 0;
		for (long i = a; i <= b; i++) {
			ans = ans ^ i;
		}
		return ans;
	}
}


Measured memory_bytes: 23892
#include<iostream>
#include<algorithm>
using namespace std;
#define rep(i,n) for(int i = 0 ; i < n ; ++i)
#define INF ( 1 << 30 )

int main(){
  int dp[5001];
  int item[] = { 200 , 300 , 500 , 1000 , 1200 , 1500 };
  int price[]= { 380 , 550 , 850 , 1520 , 1870 , 2244 };
  fill_n(dp,5001,INF);
  dp[0] = 0;
  rep(i,6) rep(j,5001 - item[i]){
    dp[j + item[i]] = min ( dp[j + item[i]] , dp[j] + price[i] );
  }
  int n;
  while(cin >> n , n) cout << dp[n] << endl;
}

Measured memory_bytes: 1176
#include<bits/stdc++.h>
using namespace std;
#define watch(x) cout<<(#x)<<" is "<<(x)<<endl;
#define mod 1000000007
#define pb push_back
#define endl '\n'
#define mp make_pair


#define all(x) x.begin(),x.end()

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

	int n,m,x;
	cin>>n>>m>>x;

	int a[n][m+1]={0};

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


	int found = 0;
	int ans = 100000000;
	int cost=0;
	int arr[m+1]={0};



	for(int i=0;i<(1<<n);i++)
	{
		cost=0;
		for(auto &it:arr) it=0;
		for(int j=0;j<n;j++)
		{
			if(i & (1<<j))
			{
				cost+=a[j][0];
				for(int k=1;k<=m;k++)
				{
					arr[k]+=a[j][k];
				}
			}
		}

		sort(arr,arr+m+1);

		if(arr[1]>= x)
		{
			found=1;
			
			ans= min(ans,cost);
		}
		

	}

	if(found == 1)
	{
		cout<<ans;
	}
	else cout<<"-1";

	cout<<endl;
}

Measured memory_bytes: 3588
#include <bits/stdc++.h>
#define PI acos(-1)
#define eq(x,y) (fabs((x)-(y)) < eps)
#define debug(x) cerr<<x<<endl;
#define endl "\n"
#define pb push_back
#define min3(a,b,c) min(a,min(b,c))
#define max3(a,b,c) max(a,max(b,c))
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
const int inf=1e9+7;
const int nmax=1e5+10;
const int eps=1e-8;

int main()
{
    //freopen("input.txt", "r", stdin);
    //freopen("output.txt", "w", stdout);
    ios::sync_with_stdio(false);
    cin.tie(0);
    //cout.precision(10);
    //cout<<fixed;
    //cout.tie(NULL);
    int n;
    cin>>n;

    vector<int>a(n+5),b(n+5),c(n);

    for(int i=1;i<=n;i++)
        cin>>a[i];
    for(int i=1;i<=n;i++)
        cin>>b[i];
    for(int i=1;i<=n-1;i++)
        cin>>c[i];

    int ans=0;

    for(int i=1;i<=n;i++)
    {
        ans+=b[a[i]];

        //cout<<"ans1 "<<ans<<endl;

        if(i!=1 && a[i]-a[i-1]==1)
        {
            ans+=c[a[i-1]];
        }
        //cout<<"ans2 "<<ans<<endl;
    }


    cout<<ans<<endl;


    return 0;
}


Measured memory_bytes: 256
#include<iostream>
#include<string>
#include<queue>
#include<algorithm>
#include<map>
#include<set>
#include<vector>
#include<math.h>

using namespace std;

#define INF 1000000007
#define LINF (1LL << 62)

typedef long long i64;
typedef pair<i64,i64> P;


int n, a[1010];

int main(){
	cin >> n;
	for(int i = 0; i < n; i++){
		cin >> a[i];
	}
	
	i64 ans = 0;
	for(int i = 0; i < n; i++){
		for(int j = i+1; j < n; j++){
			ans += a[i] * a[j];
		}
	}
	cout << ans << endl;
	
	return 0;
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
#define F first
#define S second
#define pb push_back
#define all(x) (x).begin(),(x).end()
#define sz(x) (int)(x).size()
#define fill(x,v) memset(x, v, sizeof(x))
#define PI (ld)acos(-1)
#define endl '\n'
#define dbg(x) cout<<#x<<"="<<(x)<<endl;
#define dbg2(x,y) cout<<#x<<"="<<(x)<<" "<<#y<<"="<<(y)<<endl; 
#define fio ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0); 
#define IO freopen("input.txt","r",stdin);freopen("output.txt","w",stdout);
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<int,int> pi;
typedef pair<ll,ll> pl;
const ld eps=(ld)1e-9;
const ll inf=(ll)1e18;
const ll mod=(ll)1e9+7;
int n,k, h[100007],dp[100007];
int f(int i){
	if(i==n-1){
		return 0;
	}
	if(dp[i]!=-1){
		return dp[i];
	}
	int ans=INT_MAX;
	for (int j = i+1;j<n && j <=i+k; ++j)
	{
		ans=min(ans,abs(h[j]-h[i])+f(j));
	}
	return dp[i]=ans;
}


void solve(){
	cin>>n>>k;
	fill(dp,-1);
	for (int i = 0; i < n; ++i)
	{
		cin>>h[i];
	}
	cout<<f(0);
}

int main(){fio;
int test=1;
// cin>>test;
while(test--){
	solve();
	cout<<endl;
}
return 0;
}

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

int main()
{
    int n, l;
    cin >> n >> l;
    int sum = 0;
    for(int i = 0; i < n; i++){
        sum += i + l;
    }
    if(l < 0){
        if(l <= -n + 1) sum -= (n - 1 + l);
    }
    if(l > 0){
        sum -= l;
    }
    cout << sum << endl;
}

Measured memory_bytes: 256
P = Struct.new(:name, :time)
n, q = gets.split.map(&:to_i)
list = []
n.times{
	name, time = gets.split
	list.push P.new(name, time.to_i)
}
total = 0
while list.size > 0
	p = list[0]
	if q >= p.time
		total += p.time
		list.shift
		puts [p.name, total] * ' '
	else
		total += q
		p.time -= q
		list.rotate!
	end
end

Measured memory_bytes: 16364
#from statistics import median
#import collections
#aa = collections.Counter(a) # list to list || .most_common(2)で最大の2個とりだせるお a[0][0]
from fractions import gcd
from itertools import combinations,permutations,accumulate # (string,3) 3回
#from collections import deque
from collections import deque,defaultdict,Counter
import decimal
import re
#import bisect
#
#    d = m - k[i] - k[j]
#    if kk[bisect.bisect_right(kk,d) - 1] == d:
#
#
#
# pythonで無理なときは、pypyでやると正解するかも！！
#
#
# my_round_int = lambda x:np.round((x*2 + 1)//2)
# 四捨五入
import sys
sys.setrecursionlimit(10000000)
mod = 10**9 + 7
#mod = 9982443453
def readInts():
  return list(map(int,input().split()))
def I():
  return int(input())
n = I()
ans = []
for i in range(n):
    name,score = input().split()
    score = int(score)
    ans.append([name,score,i+1])
ans = sorted(ans,key = lambda x:x[1],reverse = True)
ans = sorted(ans,key = lambda x:x[0])
for i in range(n):
    print(ans[i][2])


Measured memory_bytes: 10196
import sys
from math import gcd
sys.setrecursionlimit(10**7)
def I(): return int(sys.stdin.readline().rstrip())
def MI(): return map(int,sys.stdin.readline().rstrip().split())
def LI(): return list(map(int,sys.stdin.readline().rstrip().split()))  #空白あり
def LI2(): return list(map(int,sys.stdin.readline().rstrip()))  #空白なし
def S(): return sys.stdin.readline().rstrip()
def LS(): return list(sys.stdin.readline().rstrip().split())  #空白あり
def LS2(): return list(sys.stdin.readline().rstrip())  #空白なし


N,M = MI()
A = LI()

x = sum(a % 2 == 0 for a in A)
if x != N:
    print(0)
    exit()

r = 0
while A[0] % 2 == 0:
    r += 1
    A[0] //= 2
for i in range(1,N):
    if A[i] % (2**r) != 0 or (A[i]//(2**r)) % 2 == 0:
        print(0)
        exit()
    else:
        A[i] //= 2**r

lcm = A[0]
for i in range(1,N):
    lcm = (lcm*A[i])//gcd(lcm,A[i])

a = (lcm*2**r)//2
print(((M//a)+1)//2)


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

int a[4];
string ans;

void dfs(int i, string s, int x){
    if(i == 3){
        if(x == 7){
            ans = s;
        }
    }
    else{
        dfs(i + 1, s + '+', x + a[i + 1]);
        dfs(i + 1, s + '-', x - a[i + 1]);
    }
}

int main()
{
    int n;
    cin >> n;
    for(int i = 3; i >= 0; i--){
        a[i] = n % 10;
        n /= 10;
    }
    dfs(0, "", a[0]);
    cout << a[0] << ans[0] << a[1] << ans[1] << a[2] << ans[2] << a[3] << "=7" << endl;
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

#define int long long
#define ull unsigned long long
#define ld long double
#define rep(a) rep1(i,a)
#define rep1(i,a) rep2(i,0,a)
#define rep2(i,b,a) for(int i=(b); i<((int)(a)); i++)
#define rep3(i,b,a) for(int i=(b); i>=((int)(a)); i--)
#define all(a) a.begin(),a.end()
#define pii pair<int,int>
#define pb push_back
#define mp make_pair
//#define inf 2000000000
#define inf 8000000000000000000
#define eps 1e-9
#define sz(a) ((int)a.size())
#define pow2(x) (1ll<<(x))
#define ceiling(a,b) (((a)+(b)-1)/(b))
#define print0(a) cout << (a) << ' '
#define print1(a) cout << (a) << '\n'
#define print2(a,b) cout << (a) << ' ',print1(b)
#define print3(a,b,c) cout << (a) << ' ',print2(b,c)
#define print4(a,b,c,d) cout << (a) << ' ',print3(b,c,d)
#define ykh mt19937 rng(chrono::steady_clock::now().time_since_epoch().count())
#ifdef i_am_noob
#define debug(a) print2(#a,a)
#else
#define debug(a)
#endif
#define ordered_set tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update>

using namespace std;
using namespace __gnu_pbds;

const int Mod=1000000007,Mod2=998244353;
const int MOD=Mod;

inline int add(int a, int b){return (a+b>=MOD?a+b-MOD:a+b);}
inline int sub(int a, int b){return (a+MOD-b>=MOD?a-b:a+MOD-b);}
int Pow(int a, int b){
	if(!b) return 1;
	int tmp=Pow(a*a%MOD,b>>1);
	return (b&1?tmp*a%MOD:tmp);
}
inline int inverse(int n){return Pow(n,MOD-2);}

const int maxn=200005;
//i_am_noob
int n,cnt[4]={0};
string str[4]={"AC","WA","TLE","RE"},str1;

signed main(){
    ios_base::sync_with_stdio(0),cin.tie(0);
    cin >> n;
    while(n--){
        cin >> str1;
        rep(4) if(str1==str[i]) cnt[i]++;
    }
    rep(4) print3(str[i],"x",cnt[i]);
    return 0;
}


Measured memory_bytes: 3624
#include "bits/stdc++.h"
using namespace std;
using ll = long long;
using P = pair<int, int>;
#define PI acos(-1)
#define Mod (int)1000000007
#define INFTY (int)INT_MAX
#define Rep(i, n) for (int i = 0; i < (int)(n); i++)
#define BitRep(i, n) for (int i = 0; i < (int)(1 << n); i++)
#define All(vec) vec.begin(), vec.end()
#define Part(vec, b, e) vec.begin() + b, vec.begin() + e
#define Sum(vec) accumulate(vec.begin(), vec.end(), 0)
#define Sort(vec) sort(vec.begin(), vec.end())
#define Reverse(vec) reverse(vec.begin(), vec.end())
#define Count(vec, x) count(vec.begin(), vec.end(), x)
#define Next_permutation(vec) next_permutation(vec.begin(), vec.end())
#define Find(vec, s) vec.find(s) != vec.end()
#define Set_union(A, B, res) set_union(A.begin(), A.end(), B.begin(), B.end(), inserter(res, res.end()));
#define Set_intersection(A, B, res) set_intersection(A.begin(), A.end(), B.begin(), B.end(), inserter(res, res.end()));
#define Set_difference(A, B, res) set_difference(A.begin(), A.end(), B.begin(), B.end(), inserter(res, res.end()));
#define Set_symmetric_difference(A, B, res) set_symmetric_difference(A.begin(), A.end(), B.begin(), B.end(), inserter(res, res.end()));

struct UnionFind {
	vector<int> par;
	int group_count;

	UnionFind(int N) {
		par = vector<int>(N, -1);
		group_count = N;
	}

	int root(int x) {
		if (par[x] < 0) return x;
		return par[x] = root(par[x]);
	}

	bool same(int x, int y) {
		return root(x) == root(y);
	}

	void unite(int x, int y) {
		x = root(x), y = root(y);
		if (x != y) {
			if (par[x] > par[y]) swap(x, y);
			par[x] += par[y];
			par[y] = x;
			group_count--;
		}
	}

	int size(int x) {
		return -par[root(x)];
	}
};

int main() {
	int N, M;
	cin >> N >> M;
	UnionFind UF(N);
	int A, B;
	Rep(i, M) {
		cin >> A >> B;
		A--, B--;
		UF.unite(A, B);
	}
	cout << UF.group_count - 1 << endl;
	return 0;
}

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

#define F first
#define S second
#define mp make_pair
#define pb push_back
#define int ll
#define pw(x) (1LL<<(x))
#define all(a) (a).begin(), (a).end()
#define rall(a) (a).rbegin(), (a).rend()
#define SZ(a) ((int)(a).size())
#define dbg(a) cerr << #a << " = " << a << '\n'

using ll = long long;
using ld = long double;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using vi = vector<int>;
using vvi = vector<vi>;

void open(string file) {
    freopen((file + ".in").c_str(), "r", stdin);
    freopen((file + ".out").c_str(), "w", stdout);
}

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

const int N = 2e3 + 17;

int n, k;
vector<int> g[N];
int good[N], h[N];

void dfs(int v, int p = -1) {
    if (p == -1) h[v] = 0;
    good[v] = 0;
    if (h[v] == k / 2 + 1) good[v]++;
    for (auto to : g[v]) {
        if (to != p) {
            h[to] = h[v] + 1;
            dfs(to, v);
            good[v] += good[to];
        }
    }
}

signed main() {
    //open("cbarn");
    fast_io();
    cin >> n >> k;
    for (int i = 1; i < n; i++) {
        int a, b;
        cin >> a >> b;
        g[a].pb(b);
        g[b].pb(a);
    }
    if (k % 2 == 0) {
        int ans = n;
        for (int i = 1; i <= n; i++) {
            dfs(i);
            int cnt = 0;
            for (int j = 1; j <= n; j++) {
                if (h[j] <= k / 2) cnt++;
            }
            ans = min(ans, n - cnt);
        }
        cout << ans;
    } else {
        int ans = n;
        for (int i = 1; i <= n; i++) {
            dfs(i);
            int cnt = 0;
            for (int j = 1; j <= n; j++) {
                if (h[j] <= k / 2) cnt++;
            }
            int mx = 0;
            for (auto to : g[i]) mx = max(mx, good[to]);
            cnt += mx;
            ans = min(ans, n - cnt);
        }
        cout << ans;
    }
    return 0;
}


Measured memory_bytes: 3892
#include<stdio.h>
#include<stdlib.h>
#include<string.h>

int main(void) {
	int i;
	int a, b;
	char buf[128], *ch;

	fgets(buf, sizeof(buf), stdin);
	ch = strtok(buf, " ");
	a = atoi(ch);
	ch = strtok(NULL, " ");
	b = atoi(ch);
	
	if(a == 1) {
		a = 14;
	}
	if(b == 1) {
		b = 14;
	}

	if(a > b) {
		printf("Alice\n");
	} else if(a < b) {
		printf("Bob\n");
	} else {
		printf("Draw\n");
	}

	return 0;
}


Measured memory_bytes: 1992
n, m = map(int,input().split())
cnt = 0
for i in range(1,n+1):
  for j in range(1,m+1):
    if j>=22:
      str_j = str(j)
      d1 = str_j[0]
      d10 = str_j[1]
      if int(d1)*int(d10)==i and d1!='1' and d10 != '1':
        cnt+=1
print(cnt)

Measured memory_bytes: 3060
#include <bits/stdc++.h>
#define PI 3.14159265359
#define NIL (-1)
#define LL long long
using namespace std;
const int64_t MOD = 1e9 + 7;
const int INF = INT_MAX;

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

	vector<int> A(N), v(N);
	for (int i = 0; i < N; i++) cin >> A[i];

	vector<int> C;
	for (int i = N - 1; i >= 0; i--) {
		int n = i + 1;
		int c = 0;
		for (int j = i; j < N; j += n) {
			if (v[j]) c++;
		}
		c %= 2;
		//cout << c << endl;
		v[i] = A[i] - c;
		if (v[i]) C.push_back(i + 1);
	}
	sort(C.begin(), C.end());

	cout << C.size() << endl;
	if (C.size()) {
		for (int i = 0; i < C.size(); i++) {
			if (i) cout << ' ';
			cout << C[i];
		}
		cout << endl;
	}
}

Measured memory_bytes: 5200
#include <bits/stdc++.h>
using namespace std;
template <class T>
inline bool chmax(T &a, T b)
{
    if (a < b)
    {
        a = b;
        return 1;
    }
    return 0;
}
template <class T>
inline bool chmin(T &a, T b)
{
    if (a > b)
    {
        a = b;
        return 1;
    }
    return 0;
}
typedef long long int ll;
    
#define EPS (1e-7)
#define INF (1<<30)
#define LLINF (1LL<<60)
#define PI (acos(-1))
#define MOD (1000000007)
#define ALL(v) (v).begin(), (v).end()
#define RALL(v) (v).rbegin(), (v).rend()
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};

//-------------------------------------

ll dp[100010];

int main()
{
    cin.tie(0);
    ios::sync_with_stdio(false);
    ll n,m;
    cin>>n>>m;
    set<ll> se;
    for(ll i=0;i<m;i++){
        ll a;
        cin>>a;
        se.insert(a);
    }
    dp[0]=1;
    dp[1]=1;
    for(ll i=2;i<=n;i++){
        if(!se.count(i-1)){
            (dp[i]+=dp[i-1])%=MOD;
        }
        if(!se.count(i-2)){
            (dp[i]+=dp[i-2])%=MOD;
        }
    }
    cout<<dp[n]<<endl;
}

Measured memory_bytes: 4992
/*
AuThOr GaRyMr
*/
#include<bits/stdc++.h>
#define rb(a,b,c) for(int a=b;a<=c;++a)
#define rl(a,b,c) for(int a=b;a>=c;--a)
#define LL long long
#define IT iterator
#define PB push_back
#define II(a,b) make_pair(a,b)
#define FIR first
#define SEC second
#define FREO freopen("check.out","w",stdout)
#define rep(a,b) for(int a=0;a<b;++a)
#define KEEP while(1)
#define SRAND mt19937 rng(chrono::steady_clock::now().time_since_epoch().count())
#define random(a) rng()%a
#define ALL(a) a.begin(),a.end()
#define POB pop_back
#define ff fflush(stdout)
#define fastio ios::sync_with_stdio(false)
using namespace std;
const int INF=0x3f3f3f3f;
typedef pair<int,int> mp;
typedef pair<mp,mp> superpair;
LL n,a,d;
pair<LL,LL> each[200000+1]; 
LL res=0;
LL A[200000+1];
int main(){
	fastio;
	cin>>n>>d>>a;
	d<<=1;
	rb(i,1,n)
		cin>>each[i].FIR>>each[i].SEC;
	sort(each+1,each+1+n);
	LL tmp=0;
	rb(i,1,n){
//		cout<<tmp<<endl;
		each[i].SEC-=tmp;
		if(each[i].SEC>0){
			LL need=(each[i].SEC+a-1)/a;
			tmp+=need*a;
			res+=need;
			int l=i,r=n+1;
			while(l<r-1){
				int mid=(l+r)>>1;
				if(each[i].FIR+d>=each[mid].FIR){
					l=mid;
				}
				else{
					r=mid;
				}
			}
//			cout<<each[i].FIR+d<<endl;
//			cout<<l<<endl;
			A[l]+=need*a;
		}
		tmp-=A[i];
	}
	cout<<res;
	return 0;
}


Measured memory_bytes: 4992
/*** keep hungry and calm CoolGuang!***/
#pragma GCC optimize(2)
#include <bits/stdc++.h>
#include<stdio.h>
#include<queue>
#include<algorithm>
#include<string.h>
#include<iostream>
#define debug(x) cout<<#x<<":"<<x<<endl;
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pp;
const ll INF=1e16;
const int maxn=2e6+6;
const int mod=1000000007;
const double eps=1e-3;
inline bool read(ll &num)
{char in;bool IsN=false;
in=getchar();if(in==EOF) return false;while(in!='-'&&(in<'0'||in>'9')) in=getchar();if(in=='-'){ IsN=true;num=0;}else num=in-'0';while(in=getchar(),in>='0'&&in<='9'){num*=10,num+=in-'0';}if(IsN) num=-num;return true;}
ll n,m,p;
struct dsu{int pre[maxn];void inint(){for(int i=1;i<=n;i++) pre[i]=i;}int Find(int x){return x==pre[x]?x:pre[x]=Find(pre[x]);}void Merge(int x,int y){int dx=Find(x),dy=Find(y);if(dx!=dy) pre[dx]=dy;}};
ll num[maxn];
int a[maxn];
int main()
{
    read(n);read(m);
    ll maxl=-INF,sum=0;
    for(int i=1;i<=n;i++){
        read(num[i]);
        sum+=num[i];
    }
    int cnt=0;
    for(int i=1;i<=n;i++)
        if(num[i]*4*m>=sum) cnt++;
    if(cnt>=m) printf("Yes\n");
    else printf("No\n");
    return 0;
}
/***
6
0 0 0 0 0 0
0 2 3 1
0 3 4 1
0 1 5 1
1 0 1 0
1 0 2 0
1 0 4 0
***/


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


using namespace std;
#define rep(i,N) for(int (i)=0;(i)<(N);(i)++)
#define a 0.006

int main() {
	int N, T, A; cin >> N >> T >> A;
	vector<int> H(N);
	rep(i, N) {
		cin >> H[i];
	}
	double b,c=1000000000000;
	int ans;
	rep(i, N) {
		b=abs(double(A) - double(T - H[i] * a));
		if (b < c) {
			c = b;
			ans = i;
		}
	}  
	cout << ans+1 << endl;

}

Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
int main(){
    string S;
    cin >> S;
    int flag = 0;
    if(S.size() % 2 == 1) flag = 1;
    for(int i = 0; i < S.size(); i++){
        if(i % 2 == 0 && S[i] != 'h') flag = 1;
        else if(i % 2 == 1 && S[i] != 'i') flag = 1;
    }
    if(flag == 1) cout << "No" << endl;
    else cout << "Yes" << endl;
}

Measured memory_bytes: 256
X = list(map(int,input().split()))
N = X[0]
M = X[1]
C = int(X[2])
B = list(map(int,input().split()))
A = [input().split() for l in range(N)]


correct = 0
for N_iter in range(N):
    line_sum = 0
    for M_iter in range(M):
        line_sum += int(A[N_iter][M_iter])*B[M_iter]
    line_sum += C
    if line_sum > 0:
        correct += 1

print(correct)

Measured memory_bytes: 9176
#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 whole(x) (x).begin(),(x).end()
#define UNIQUE(v) v.erase(unique(v.begin(), v.end()), v.end())
using ll = long long;
using P = pair<int, int>;
const int mod = 1000000007;

vector<ll> to[200005];
ll c[200005];
ll buka[200005];
vector<ll> dat[200005];
ll numcol[200005];
ll n;

void dfs(ll x, ll p) {
    if (to[x].size()==1 && to[x][0]==p) return;
    for (ll e: to[x]) {
        if (e==p) continue;
        dfs(e, x);
        buka[x] += (buka[e])+1;
    }
}

void dfs2(ll x, ll p) {
    ll color = c[x];
    int flg = dat[color].size();
    ll v;
    if (!flg) {
        dat[color].push_back(n);
    }
    
    v = dat[color][dat[color].size()-1];
    v -= (buka[x]+1);
    dat[color].erase(dat[color].end()-1);   
    
    for (ll e: to[x]) {
        if (e==p) continue;
        dat[color].push_back(buka[e]+1);
        dfs2(e, x);
    }
    
    dat[color].push_back(v);
    
}

int main(){
    cin >> n;
    
    rep(i, n) {
        cin >> c[i];
        c[i]--;
        numcol[c[i]]++;
    }
    rep(i, n-1) {
        ll a, b;
        cin >> a >> b;
        a--; b--;
        to[a].emplace_back(b);
        to[b].emplace_back(a);
    }

    dfs(0, -1);
    dfs2(0, -1);

    rep(i, n) {
        if (dat[i].size()==0) {
            cout << "0" << endl;
            continue;
        }
        ll ans = 0;
        for (ll e: dat[i]) {
            ll x = e;
            ans += x*(x+1)/2;
        }
        ll res = n*(n+1)/2 - ans;
        cout << res << endl;
    }
    
    return 0;
}


Measured memory_bytes: 54696
#include <bits/stdc++.h>
#define IO_OP std::ios::sync_with_stdio(0); std::cin.tie(0);
#define F first
#define S second
#define PB push_back
#define MP make_pair
#define EB emplace_back
#define ALL(v) (v).begin(), (v).end()
#define endl '\n'

using namespace std;

typedef long long ll;
typedef pair<int, int> pi;
typedef vector<int> vi;

const int INF = 1e9 + 7;

long double p[55];

int main()
{
	IO_OP;
	
	int n;
	cin >> n;
	for(int i=0;i<n;i++)
		cin >> p[i];
	sort(p, p+n);
	long double c = (p[0] + p[1]) / 2;
	for(int i=2;i<n;i++)
		c = (c + p[i]) / 2;

	cout << c << endl;
	
}



Measured memory_bytes: 256
#include <bits/stdc++.h>
#define fore(x,a,b) for(int x=a, qwerty=b; x<qwerty; x++)
#define ALL(a) (a).begin(),(a).end()
#define SZ(a) ((int)(a).size())
#define FIN ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0)
#define pb push_back
#define fst first
#define snd second
using namespace std;
typedef long long ll;

const int N=3e5+5;
int n, a, b, ans[N];

bool can(){
    int i=max(a,b), j=min(a,b), nused=n, pos=0;
    
    fore(x,0,i){
        ans[i-1-x]=nused--;
    }
    pos=i;
    j--;
    while(nused){
        if(j==0||nused<j)return false;
        int tmp=(nused-(j-1));
        if(tmp>=i)tmp=i;
        stack<int> s;
        fore(x,0,tmp)s.push(nused--);
        fore(x,0,tmp)ans[pos++]=s.top(),s.pop();
        j--;
    }
    return j==0;
}

int main(){FIN;
	cin >> n >> a >> b;
    if(!can()){cout<<"-1\n";return 0;}
    if(a<b)fore(x,0,n)ans[x]=n-ans[x]+1;
    fore(x,0,n)cout<<ans[x]<<" \n"[x+1==n];
}

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

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

    if(s == "ARC") cout << "ABC" << endl;
    else cout << "ARC" << endl;

    return 0;
}

Measured memory_bytes: 3608
#include <iostream>
#include <cmath>
#include <string>
#include <map>
#include <queue>
using namespace std;
#define N 3
#define N2 9

struct Puzzle{
    int f[N2]; int space; string path;
    bool operator<(const Puzzle &p) const{
    for(int i=0;i<N2;i++){
        if(f[i]==p.f[i]) continue;
        return f[i]>p.f[i];
    }
    return false;
    }
};

static const int dx[4] = {-1,0,1,0};
static const int dy[4] = {0,-1,0,1};
static const char dir[4] = {'u','l','d','r'};

bool isTarget(Puzzle p){
    for(int i=0;i<N2;i++){
        if(p.f[i]!=(i+1)) return false;
    }
     return true;    
}

string bfs(Puzzle s){
    queue<Puzzle> Q;
    map<Puzzle,bool> V;
    Puzzle u,v;
    s.path="";
    Q.push(s);
    V[s]=true;

    while (!Q.empty()){
        u=Q.front();Q.pop();
        if(isTarget(u)) return u.path;
        int sx=u.space/N;
        int sy=u.space%N;
        for(int r=0;r<4;r++){
            int tx=sx+dx[r];
            int ty=sy+dy[r];
            if(tx<0 || ty<0 || tx>=N || ty>=N) continue;
            v=u;
            swap(v.f[u.space],v.f[tx*N+ty]);
            v.space=tx*N+ty;
            if(!V[v]){
                V[v]=true;
                v.path+=dir[r];
                Q.push(v);
            }
        }
    }
    return "unsolvable";
}

int main(){
    Puzzle in;

    for(int i=0;i<N2;i++){
        cin>>in.f[i];
        if(in.f[i]==0){
            in.f[i]=N2;
            in.space=i;
        }
    }
    string ans=bfs(in);
    cout<<ans.size()<<endl;

    return 0;
}


Measured memory_bytes: 30568
#include "bits/stdc++.h"
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
const LL MOD = 1000000007LL;
bool dpX[2][16001];
bool dpY[2][16001];
int main() {
	string s;
	cin >> s;
	int N = s.size();
	int X, Y;
	cin >> X >> Y;
	int pos = 0;
	while (pos < N && s[pos] == 'F') X--, pos++;
	bool dir = 1;
	int d = 0;
	vector<int> dx, dy;
	while (pos < N) {
		if (s[pos] == 'F') {
			d++;
		}
		else {
			if (dir) dx.push_back(d);
			else dy.push_back(d);
			dir ^= 1;
			d = 0;
		}
		pos++;
	}
	if (dir) dx.push_back(d);
	else dy.push_back(d);
	dpX[0][N] = 1;
	for (int i = 0; i < dx.size(); i++) {
		for (int x = -N; x <= N; x++) {
			if (x + dx[i] <= N) dpX[(i + 1) & 1][x + dx[i] + N] |= dpX[i & 1][x + N];
			if (x - dx[i] >= -N) dpX[(i + 1) & 1][x - dx[i] + N] |= dpX[i & 1][x + N];
			dpX[i & 1][x + N] = 0;
		}
	}
	dpY[0][N] = 1;
	for (int i = 0; i < dy.size(); i++) {
		for (int y = -N; y <= N; y++) {
			if (y + dy[i] <= N) dpY[(i + 1) & 1][y + dy[i] + N] |= dpY[i & 1][y + N];
			if (y - dy[i] >= -N) dpY[(i + 1) & 1][y - dy[i] + N] |= dpY[i & 1][y + N];
			dpY[i & 1][y + N] = 0;
		}
	}
	if (-N <= X && X <= N && -N <= Y && Y <= N && dpX[dx.size() & 1][X + N] && dpY[dy.size() & 1][Y + N]) {
		cout << "Yes" << endl;
	}
	else {
		cout << "No" << endl;
	}
}

Measured memory_bytes: 384
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define endl "\n"
#define MOD 1000000007
#define print(A,n) for(ll i=0;i<n;++i)cout<<A[i]<<' ';cout<<endl;
#define take(A,n) for(ll i=0;i<n;++i)cin>>A[i];
//    for(auto& it : m)
void fastio(){ios_base::sync_with_stdio(false);cin.tie(NULL);}
ll int power(ll int x, ll int y, ll int p)  
{  
   ll int res = 1;     // Initialize result  
 
    x = x % p; // Update x if it is more than or  
                // equal to p
   
    if (x == 0) return 0; // In case x is divisible by p;
 
    while (y > 0)  
    {  
        // If y is odd, multiply x with result  
        if (y & 1)  
            res = (res*x) % p;  
 
        // y must be even now  
        y = y>>1; // y = y/2  
        x = (x*x) % p;  
    }  
    return res;  
}  
 


int main()
{
 
  fastio();

ll int T=1;

while(T--)
{
    ll int n,i;
    cin>>n;
    ll int a[n];
    ll int sum=0,ans=0;
    for(i=0;i<n;i++)
        {cin>>a[i];sum+=a[i];sum%=MOD;}
    for(i=0;i<n;i++)
    {
        sum-=a[i];
        sum+=MOD;
        sum%=MOD;
        ans+=a[i]*sum;
        ans%=MOD;
    }
    cout<<ans<<endl;
}



return 0;}

Measured memory_bytes: 5212
import sys
import numpy as np
import numba

sys.setrecursionlimit(10 ** 8)
ini = lambda: int(sys.stdin.readline())
inm = lambda: map(int, sys.stdin.readline().split())
inl = lambda: list(inm())
ins = lambda: sys.stdin.readline().rstrip()
debug = lambda *a, **kw: print("\033[33m", *a, "\033[0m", **dict(file=sys.stderr, **kw))


@numba.njit("(i8, i2[:], i4[:,:], i2[:])", cache=True)
def optimize(D, C, S, sol):
    NUM_ITERS = 30_000_000

    def calc_score(sol):
        last = -np.ones((26, D + 1))
        score = 0
        for d in range(D):
            ct = sol[d]
            score += S[d, ct]
            for i in range(26):
                last[i, d + 1] = last[i, d]
            last[ct, d + 1] = d
            for i in range(26):
                score -= C[i] * (d - last[i, d + 1])
        return score, last

    sol_bak = sol.copy()
    max_score, last = calc_score(sol)
    last_bak = last.copy()

    def update(d, before, after):
        score = max_score
        score -= S[d, before]
        score += S[d, after]
        lb = last[before, d]
        la = last[after, d]
        da = db = 0
        for x in range(d, D):
            if x > d and sol[x] == before:
                break
            last[before, x + 1] = lb
            db += 1
        score -= C[before] * (d - lb) * db
        for x in range(d, D):
            if x > d and sol[x] == after:
                break
            last[after, x + 1] = d
            da += 1
        score += C[after] * (d - la) * da
        sol[d] = after
        return score

    tried = numba.typed.Dict.empty(
        key_type=numba.types.int32, value_type=numba.types.boolean
    )
    for i in range(NUM_ITERS):
        new_d = np.random.randint(0, D)
        c_before = sol[new_d]
        c_after = np.random.randint(0, 26)
        if c_before == c_after:
            continue
        candidate = new_d * 26 + c_after
        if candidate in tried:
            continue
        tried[candidate] = True
        new_score = update(new_d, c_before, c_after)
        if max_score < new_score:
            max_score = new_score
            sol_bak = sol.copy()
            last_bak[c_before, :] = last[c_before, :]
            last_bak[c_after, :] = last[c_after, :]
            tried.clear()
        else:
            sol = sol_bak.copy()
            last[c_before, :] = last_bak[c_before, :]
            last[c_after, :] = last_bak[c_after, :]
    return sol


def main():
    D = ini()
    C = np.array(inl(), dtype="int16")
    assert len(C) == 26
    S = np.zeros((D, 26), dtype="int32")
    for i in range(D):
        S[i, :] = inl()

    sol = np.zeros(D, dtype="int16")
    last = -np.ones(26, dtype="int16")
    for d in range(D):
        score = 0
        mi = -1
        for i in range(26):
            s = S[d, i] - C[i] * (d - last[i])
            if score < s:
                score = s
                mi = i
        sol[d] = mi
        last[mi] = d
    sol = optimize(D, C, S, sol)

    print(*(sol + 1), sep="\n")


main()


Measured memory_bytes: 108536
// clang-format off
#include <bits/stdc++.h>
using namespace std;
#define int long long
#define loop(i, a, n) for (int i = (a); i < (n); i++)
#define rep(i, n) loop(i, 0, n)
#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()
#define prec(n) fixed << setprecision(n)
constexpr int INF = sizeof(int) == sizeof(long long) ? 1000000000000000000LL : 1000000000;
constexpr int MOD = 1000000007;
constexpr double PI = 3.14159265358979;
template<typename A, typename B> bool cmin(A &a, const B &b) { return a > b ? (a = b, true) : false; }
template<typename A, typename B> bool cmax(A &a, const B &b) { return a < b ? (a = b, true) : false; }
bool odd(const int &n) { return n & 1; }
bool even(const int &n) { return ~n & 1; }
template<typename T> int len(const T &v) { return v.size(); }
template<typename T = int> T in() { T x; cin >> x; return x; }
template<typename T = int> T in(T &&x) { T z(forward<T>(x)); cin >> z; return z; }
template<typename T> istream &operator>>(istream &is, vector<T> &v) { for (T &x : v) is >> x; return is; }
template<typename A, typename B> istream &operator>>(istream &is, pair<A, B> &p) { return is >> p.first >> p.second; }
template<typename T> ostream &operator<<(ostream &os, const vector<vector<T>> &v) { int n = v.size(); rep(i, n) os << v[i] << (i == n - 1 ? "" : "\n"); return os; }
template<typename T> ostream &operator<<(ostream &os, const vector<T> &v) { int n = v.size(); rep(i, n) os << v[i] << (i == n - 1 ? "" : " "); return os; }
template<typename A, typename B> ostream &operator<<(ostream &os, const pair<A, B> &p) { return os << p.first << ' ' << p.second; }
template<typename Head, typename Value> auto vectors(const Head &head, const Value &v) { return vector<Value>(head, v); }
template<typename Head, typename... Tail> auto vectors(Head x, Tail... tail) { auto inner = vectors(tail...); return vector<decltype(inner)>(x, inner); }
// clang-format on

map<int, int> compressByOrd(const vector<int> &v) {
  map<int, int> m;
  rep(i, v.size()) m[v[i]] = i;
  return m;
}

main() {
  int N;
  cin >> N;
  vector<int> x1(N), y1(N), x2(N), y2(N);
  vector<int> x(2 * N), y(2 * N);
  rep(i, N) {
    cin >> x1[i] >> y1[i] >> x2[i] >> y2[i];
    x[i * 2] = x1[i], x[i * 2 + 1] = x2[i];
    y[i * 2] = y1[i], y[i * 2 + 1] = y2[i];
  }
  sort(all(x)), sort(all(y));
  map<int, int> cx = compressByOrd(x), cy = compressByOrd(y);
  auto p = vectors(2 * N, 2 * N, 0LL);
  rep(i, N) {
    int a = cx[x1[i]], b = cy[y1[i]], c = cx[x2[i]], d = cy[y2[i]];
    p[a][b]++, p[c][d]++, p[a][d]--, p[c][b]--;
  }
  rep(i, 2 * N) loop(j, 1, 2 * N) p[i][j] += p[i][j - 1];
  rep(j, 2 * N) loop(i, 1, 2 * N) p[i][j] += p[i - 1][j];
  int s = 0;
  rep(i, 2 * N - 1) rep(j, 2 * N - 1) s += (x[i + 1] - x[i]) * (y[j + 1] - y[j]) * (p[i][j] > 0);
  cout << s << endl;
}



Measured memory_bytes: 128768
#include<bits/stdc++.h>
using namespace std ;
int main()
{
    int student = 0 ;
    int baton = 0 ;
    int bibs[101] = {0} ;

    cin >> student >> baton ;
    for ( int i = 0 ; i < student ; i++ )
    {
        cin >> bibs[i] ;
    }

    for ( int i = 1 ; i <= baton ; i++ )
    {
        for ( int j = 0 ; j < student ; j++ )
        {
            if ( (bibs[j] % i) > (bibs[j+1] % i) && j != student-1 )
            {
                swap( bibs[j],bibs[j+1] ) ;
            }
        }
    }

    for ( int i = 0 ; i < student ; i++ )
    {
        cout << bibs[i] << endl ;
    }

    return 0 ;
}

Measured memory_bytes: 3068
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int,int> P;
typedef pair<ll,ll> l_l;
const int INF=1001001000;
const int mINF=-1001001000;
const ll LINF=1010010010010010000;
int main(){
    int n,m;cin >> n >> m;
    vector<int> G[100010];
    for(int i=0;i<m;i++){
        int u,v;cin >> u >> v;
        u--;v--;
        G[u].push_back(v);
    }
    int sv,tv;cin >> sv >> tv;
    sv--;tv--;
    queue<P> q;
    q.push({sv,0});
    vector<vector<int>> dist(n,vector<int>(3,INF));
    dist[sv][0]=0;
    while(!q.empty()){
        int nowp=q.front().first;
        int nowd=q.front().second;
        q.pop();
        for(auto next: G[nowp]){
            if(dist[next][(nowd+1)%3]!=INF) continue;
            dist[next][(nowd+1)%3]=dist[nowp][nowd]+1;
            q.push({next,(nowd+1)%3});
        }
    }
    ll ans=dist[tv][0];
    if(dist[tv][0]==INF) ans=-1;
    else ans/=3;
    cout << ans << endl;
    return 0;
}

Measured memory_bytes: 11136
#include<bits/stdc++.h>
const int N=100005;
using namespace std;
struct edge{
	int to,next,v;
}e[N*2];
int head[N],tot=1;
void add(int x,int y,int v){
	e[++tot]=(edge){y,head[x],v};
	head[x]=tot;
}
int co[N],cnt[3];
int cnte,cntp,ok;
bool vis[N*2];
void dfs(int x,int c){
	if (co[x]!=-1){
		if (co[x]!=c)
			ok=0;
		return;
	}
	co[x]=c; cnt[c]++; cntp++;
	for (int i=head[x];i;i=e[i].next)
		if (!vis[i]&&!vis[i^1]){
			vis[i]=vis[i^1]=1; cnte++;
			dfs(e[i].to,(c+e[i].v)%3);
		}
}
int n,m;
int main(){
	scanf("%d%d",&n,&m);
	for (int i=1;i<=m;i++){
		int x,y;
		scanf("%d%d",&x,&y);
		add(x,y,1); add(y,x,2);
	}
	memset(co,-1,sizeof(co));
	long long ans=0;
	for (int i=1;i<=n;i++)
		if (co[i]==-1){
			memset(cnt,0,sizeof(cnt));
			ok=1; cnte=0; cntp=0; dfs(i,0);
			if (!ok) ans+=1ll*cntp*cntp;
			else if (!cnt[0]||!cnt[1]||!cnt[2]) ans+=cnte;
			else ans+=1ll*cnt[0]*cnt[1]+1ll*cnt[1]*cnt[2]+1ll*cnt[2]*cnt[0];
		}
	printf("%lld",ans);
}

Measured memory_bytes: 6656
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <cmath>
#include <vector>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <queue>
#include <ctime>
#include <cassert>
#include <complex>
#include <string>
#include <cstring>
#include <chrono>
#include <random>
#include <queue>
#include <bitset>
#include <stack>
#include <functional>

#ifdef LOCAL
    #define eprintf(...) fprintf(stderr, __VA_ARGS__)
#else
    #define eprintf(...) 42
#endif

#define rep(i, n) for(int i = 0, i##_len = (n); i < i##_len; ++i)
#define repp(i, m, n) for(int i = m, i##_len = (n); i < i##_len; ++i)
#define reprev(i, n) for(int i = (n-1LL); i >= 0; --i)
#define all(x) (x).begin(), (x).end()
template <class T> bool chmax(T &a, const T &b) { if (a < b) { a = b; return 1; } return 0; }
template <class T> bool chmin(T &a, const T &b) { if (b < a) { a = b; return 1; } return 0; }
template <class T> T gcd(T a, T b) { return b ? gcd(b, a % b) : a; }
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair <int,int> P;
typedef long double ld;

int main(void)
{
    cin.tie(0);
    ios::sync_with_stdio(false);
    int n, k;
    cin >> n >> k;
    cout << (n - 1) / k + 1 << endl;
    return 0;
}

Measured memory_bytes: 256
#include <iostream>
#include <vector>

int main()
{
    long long N, L;
    std::cin >> N >> L;
    std::vector<long long> a(N);
    for(int i=0; i<N; i++) scanf(" %lld", &a[i]);

    int index = -1;
    for(int i=0; i<N-1; i++){
        if(L <= a[i] + a[i+1]) {index = i; break;}
    }

    if(index == -1) printf("Impossible\n");
    else{
        printf("Possible\n");
        for(int i=0; i<index; i++) printf("%d\n", i+1);
        for(int i=N-1; i>index; i--) printf("%d\n", i);
    }
}

Measured memory_bytes: 1536
N, A, B = map(int, input().split())
AB = A ^ B
D = bin(AB)[2:].count('1')
if D % 2 == 0:
  print('NO')
else:
  print('YES')
  C = [0] * (2 ** N)
  C[0] = str(A)
  P = []
  for i in range(N):
    if AB % 2:
      P = P + [i]
    else:
      P = [i] + P
    AB >>= 1
  for i in range(1, 2 ** N):
    k = 0
    b = i
    while b % 2 == 0:
      k += 1
      b >>= 1
    A ^= 1 << P[k]
    C[i] = str(A)
  k = 0
  for i in range(D // 2):
    k += 1 / (4 ** (i + 1))
    K = int(2 ** N * (1 - k))
    C = C[:K-1] + C[K+1:] + C[K-1:K+1][::-1]
  print(' '.join(C))

Measured memory_bytes: 15204