akhauriyash/Code-Regression huggingface dataset:
#include <bits/stdc++.h>

#define MOD 998244353LL

using namespace std;

typedef long long ll;

ll factorials[300001];
ll invFactorials[300001];

// Calculates a^b mod n.
ll powMod(ll a, ll b, ll n) {
  if (b == 0) return 1;
  ll res = 1;
  while (b > 1) {
    if (b & 1) res = (a * res) % n;
    a = (a * a) % n;
    b /= 2;
  }
  return (a * res) % n;
}

// Calculates binomial coefficient n choose k.
ll binom(ll n, ll k) {
  if (k > n) return 0;
  return factorials[n] * invFactorials[k] % MOD * invFactorials[n - k] % MOD;
}

// Calculates the number of paintings with x horizontal persons, y persons from
// the top and z persons from the bottom under the assumption that a horizontal
// person moves first.
ll countSubs(int x, int y, int z) {
  if (x == 0) {
    if (y == 0 && z == 0) return 1;
    return 0;
  }
  return binom(x + y + z - 1, x - 1);
}

// Counts the number of different paintings assuming that the first person moves
// vertically. Call again with the mirrored instance to get total number.
ll solve(int n, int m, string &a, string &b, string &c, string &d) {
  int left = 0, right = 0, bottomRight = 0, topRight = 0;
  for (int i = 0; i < n; i++) {
    if (a[i] == '1') left++;
    if (b[i] == '1') right++;
  }
  for (int i = 0; i < m; i++) {
    if (c[i] == '1') topRight++;
    if (d[i] == '1') bottomRight++;
  }
  ll result = 0, prefix = 0;
  int bottomLeft = 0, topLeft = 0;
  for (int i = 0; i < m; i++) {
    if (c[i] == '0' && d[i] == '0') continue;
    prefix += countSubs(left, bottomLeft, topLeft);
    prefix %= MOD;
    if (c[i] == '1' && d[i] == '1') {
      prefix *= 2;
      prefix %= MOD;
    }
    if (c[i] == '1') {
      topLeft++;
      topRight--;
    }
    if (d[i] == '1') {
      bottomLeft++;
      bottomRight--;
    }
    result += prefix * countSubs(right, bottomRight, topRight);
    result %= MOD;
  }
  return result;
}

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

  // Precalculate factorials and inverse factorials.
  factorials[0] = 1;
  invFactorials[0] = 1;
  for (ll i = 1; i <= 300000; i++) {
    factorials[i] = factorials[i - 1] * i % MOD;
    invFactorials[i] = powMod(factorials[i], MOD - 2, MOD);
  }

  ll n, m;
  cin >> n >> m;
  string a, b, c, d;
  cin >> a >> b >> c >> d;

  // If there are no persons at all, the answer is 1.
  bool anyPerson = false;
  for (int i = 0; i < n; i++) {
    anyPerson |= a[i] == '1';
    anyPerson |= b[i] == '1';
  }
  for (int i = 0; i < m; i++) {
    anyPerson |= c[i] == '1';
    anyPerson |= d[i] == '1';
  }
  if (!anyPerson) {
    cout << "1" << endl;
    return 0;
  }

  // Assume first person moves vertically. Then swap instance to consider case
  // where first person moves horizontally.
  ll result = (solve(n, m, a, b, c, d) + solve(m, n, c, d, a, b)) % MOD;
  cout << result << endl;
  return 0;
}


Measured memory_bytes: 5584
#include<bits/stdc++.h>
using namespace std;
int p[100005];
int blocked[100005];
int direct_friend[100005];
int u_find(int a){
	if(p[a]<0) return a;
	return p[a] = u_find(p[a]);
}
void merge(int a,int b){
	a = u_find(a);
	b = u_find(b);
	if(a==b) return;
	p[a]+=p[b];
	p[b] = a;
}
int main(){
	memset(p,-1,sizeof(p));
	int n,m,k;
	cin>>n>>m>>k;
	while(m--){
		int a,b;
		cin>>a>>b;
		direct_friend[a]++;
		direct_friend[b]++;
		merge(a,b);
	}
	while(k--){
		int a,b;
		cin>>a>>b;
		if(u_find(a)==u_find(b)){
			blocked[a]++;
			blocked[b]++;
		}
	}
	for(int i=1; i<=n; i++){
		cout<<-1*p[u_find(i)]-direct_friend[i]-blocked[i]-1<<" ";
	}
}

Measured memory_bytes: 5804
#include <iostream>
#include <cstdlib>
#include <list>
#include <sstream>
#include <algorithm>
using namespace std;
/*以下の命令を受けつける双方向連結リストを実装してください。
insert x: 連結リストの先頭にキー x を持つ要素を継ぎ足す。
delete x: キー x を持つ最初の要素を連結リストから削除する。そのような要素が存在しない場合は何もしない。
deleteFirst: リストの先頭の要素を削除する。
deleteLast: リストの末尾の要素を削除する。*/
int main(){
    list<int> l;
    string command;
    getline(cin, command);
    while(getline(cin, command)){
	    if (command == "deleteFirst") {
		    l.pop_front();
		    continue;
	    } else if(command == "deleteLast") {
		    l.pop_back();
		    continue;
	    }
	    auto pos = command.find(" ");
	    int value = stoi(command.substr(pos + 1));
	    if (command.find("insert") != string::npos) {
		    l.push_front(value);
	    } else if (command.find("delete") != string::npos) {
		    auto result = find(l.begin(), l.end(), value);
		    if (l.end() != result) {
			    l.erase(result);
		    }
	    }
    }
    for (auto itr = l.begin(), end = l.end(); itr != end; itr++) {
	    cout << *itr << ((next(itr) != end) ? " " : "");
    }
    cout << endl;
}



Measured memory_bytes: 34388
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
#include <queue>
#include <string>
#include <set>
#include <map>
#define REP(i,n) for(ll i = 0; i < (ll)n; i++)
#define INF 1000000000000000
using namespace std;
typedef long long ll;
typedef double db;
typedef string str;

int main(){
  ll n,m;
  cin >> n >> m;
  ll cnt[n];
  fill(cnt,cnt+n,0);
  REP(i,m){
    ll a,b;
    cin >> a>>b;
    cnt[a-1]++;
    cnt[b-1]++;
  }
  REP(i,n){
    cout << cnt[i] << endl;
  }
  return 0;
}


Measured memory_bytes: 256
#include <iostream>
#include <vector>
#include <algorithm>
#include <cassert>
#include <iomanip>

using namespace std;

int main() {
    int a, b, c, d;
    cin >> a >> b >> c >> d;

    int t = 500;

    vector<vector<int>> x(a, vector<int>(b, t));

    for (int i = 0; i + c - 1 < a; i += c) {
        for (int j = 0; j + d - 1 < b; j += d) {
            x[i + c - 1][j + d - 1] = -t * (c * d - 1) - 1;
        }
    }

    long long s = 0;

    for (int i = 0; i < a; i++) {
        for (int j = 0; j < b; j++) {
            s += x[i][j];
        }
    }

    if (s <= 0) {
        cout << "No" << endl;
    } else {
        cout << "Yes" << endl;

        for (int i = 0; i < a; i++) {
            for (int j = 0; j < b; j++) {
                cout << x[i][j] << " \n"[j + 1 == b];
            }
        }
    }
}


Measured memory_bytes: 2304
x,y=map(int,input().split())
count=0
if y == 2*x or y == 4*x:
    count=1
else:
  for i in range(x):
    for j in range(x):
      if (i+j)==x:
        if (2*i) + (4 * j) == y:
          count+=1     
if count<1:
  print("No")
else:
  print("Yes")

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

using namespace std;
using ll = long long;

int main(void)
{
	map<ll, map<ll, ll>> A;
	map<ll,map<ll,bool>> card;
	ll N, B;
	bool ans=false;

	cin >> A[1][1] >> A[1][2] >> A[1][3];
	cin >> A[2][1] >> A[2][2] >> A[2][3];
	cin >> A[3][1] >> A[3][2] >> A[3][3];
	cin >> N;

	for (ll n = 0; n < N; n++)
	{
		cin >> B;
		for (ll i = 1; i <= 3; i++)
		{
			for (ll j = 1; j <= 3; j++)
			{
				if(A[i][j] == B){
					card[i][j]=true;
				}
			}
		}
	}

	if(card[1][1] && card[1][2] && card[1][3])ans=true;
	if(card[2][1] && card[2][2] && card[2][3])ans=true;
	if(card[3][1] && card[3][2] && card[3][3])ans=true;

	if(card[1][1] && card[2][1] && card[3][1])ans=true;
	if(card[1][2] && card[2][2] && card[3][2])ans=true;
	if(card[1][3] && card[2][3] && card[3][3])ans=true;

	if(card[1][1] && card[2][2] && card[3][3])ans=true;
	if(card[1][3] && card[2][2] && card[3][1])ans=true;

	if(ans){
		cout<<"Yes"<<endl;
	}else{
		cout<<"No"<<endl;
	}



	return 0;
}


Measured memory_bytes: 256
#include <cstdio>
#include <cmath>
#include <cstdint>
#include <cinttypes>
#include <iostream>
#include <vector>
#include <algorithm>
#include <tuple>

using namespace std;

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

    vector<tuple<string, int, int>> restaurants(n);

    for(int i = 0 ; i < n ; i++){
        string name;
        int score;
        int index = i + 1;
        cin >> name >> score;
        get<0>(restaurants[i]) = name;
        get<1>(restaurants[i]) = 100 - score;
        get<2>(restaurants[i]) = index;
    }

    sort(restaurants.begin(), restaurants.end());

    for(tuple<string, int, int>&r : restaurants){
        cout << get<2>(r) << endl;
    }

    return 0;
}



Measured memory_bytes: 256
#include<bits/stdc++.h>
#define INF 1e9
#define llINF 1e18
#define MOD 1000000007
#define pb push_back
#define mp make_pair 
#define F first
#define S second
#define ll long long
#define vi vector<ll>
#define vvi vector<vi>
#define BITLE(n) (1LL<<((ll)n))
#define SHIFT_LEFT(n) (1LL<<((ll)n))
#define SUBS(s,f,t) ((s).substr((f)-1,(t)-(f)+1))
#define ALL(a) (a).begin(),(a).end()
using namespace std;
struct point{
  double x,y,z;
};
ll bitsum(ll a){
  ll ret = 0;
  for(int i=0;i<22;i++)
    if(BITLE(i)&a)ret++;
  return ret;
}
double dist(point a,point b){
  return (a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y)+(a.z-b.z)*(a.z-b.z);
}
int main(){
  cin.tie(0);
  ios::sync_with_stdio(false);
  ll n,m;cin>>n>>m;
  double ans = 0;
  vector<point>hoge(n);
  for(int i=0;i<n;i++){
    cin>>hoge[i].x>>hoge[i].y>>hoge[i].z;
  }
  for(int i=0;i<BITLE(n);i++){
    if(bitsum(i) != m)continue;
    double sum = 0;
    for(int j=0;j<n;j++){
      for(int k=j+1;k<n;k++){
	if((BITLE(j)&i) && (BITLE(k)&i))
	  sum += dist(hoge[j],hoge[k]);
      }
    }
    ans =  max(ans,sum);
  }
  printf("%.10f\n",ans);
  return 0;
}


Measured memory_bytes: 3320
#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define endl "\n"
using namespace std;

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

    int a,b,h,m;
    long double pie=3.141592653589793238;
    cin >> a >> b >> h >> m;
    
    long double hour_angle=(30*h)+(0.5*m);
    long double min_angle=6*m;
    long double t=abs(hour_angle-min_angle);
    long double theta=min(t,360-t);

    theta=(theta/360)*2*pie;
    cout << setprecision(20) << sqrt((a*a)+(b*b)-(2*a*b*cos(theta))) << endl;

    return 0;
}

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

// int/long:		-2,147,483,648 - 2,147,483,648 (-2^31 <= int < 2^31)
// long/long long:	-9,223,372,036,854,775,808 - 9,223,372,036,854,775,807 (-2^63 <= long < 2^63)

#define	INF (1<<29)
//536,870,912

// lower_bound(A.begin(), A.end(), N)
// upper_bound(...

// A.erase(unique(A.begin(), A.end()), A.end())

// bit: &/and, |/or, ^/xor, ~/not

// getline(cin, String)
// while (getline(cin, S)) {}

// cout <<fixed <<setprecision(10)

// priority_queue<int> q;
// priority_queue<int, vector<int>, less<int>> q; // Default: vector, less
// priority_queue<vector<int>, vector<vector<int>>, greater<vector<int>>> q;

// Graph
// Warshall-Floyd: Distance between each node, N^3
// Dijkstra: Distance from the start node, N^2
// DFS(Depth-First Search)

/*
int	gcd(int A, int B) {
	while (0!=B) {
		A %=B;
		swap(A, B);
	}
	return A;
}
*/

#define	MOD 1000000007

int     main() {
	int H, W;
	cin >>H >>W;
	vector<vector<int>> ss(H+1, vector<int> (W+1, 0));
	for (int i=0; i<H; i++) {
		string S;
		cin >>S;
		for (int j=0; j<W; j++) {
			if ('.'==S.at(j))
				ss.at(i).at(j)=1;
		}
	}
	int N=H*W;
	vector<vector<int>> d (N, vector<int> (N, INF));
	//
	// [1]--5--[0]
	//  |     / |
	//  2   8   3
	//  | /     |
	// [3]-20--[2] 
	// 
	// Initialize with direct routes.
	/*
	d.at(0)={0, 5, 3, 8};
	d.at(1)={5, 0, INF, 2};
	d.at(2)={3, INF, 0, 20};
	d.at(3)={8, 2, 20, 0};
	*/
	for (int i=0; i<N; i++)
		d.at(i).at(i)=0;
	for (int i=0; i<H; i++)
		for (int j=0; j<W; j++) {
			if (ss.at(i).at(j)==0)
				continue;
			int p=i*W+j;
			if (ss.at(i).at(j+1)==1) {
				d.at(p).at(p+1)=1;
				d.at(p+1).at(p)=1;
			}
			if (ss.at(i+1).at(j)==1) {
				d.at(p).at(p+W)=1;
				d.at(p+W).at(p)=1;
			}
		}
	// Add waypoints one by one
	for (int k=0; k<N; k++)
		for (int i=0; i<N; i++)
			for (int j=0; j<N; j++)
				d.at(i).at(j) =min(d.at(i).at(j), d.at(i).at(k)+d.at(k).at(j));
	int SSS=0;
	for (int i=0; i<N; i++)
		for (int j=0; j<N; j++)
			if (d.at(i).at(j)!=INF)
				SSS=max(SSS, d.at(i).at(j));
	cout <<SSS <<endl;
	return 0;
};



Measured memory_bytes: 896
//In the name of Allah :)
#include <bits/stdc++.h>
using namespace std;

const int MAXN = 2e5 + 10, MOD = 1e9 + 7, INF = 1e9 + 5;

int main()
{
	ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
	string str;
	cin >> str;
	cout << str[0] << str[1] << str[2] << '\n';
}


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

int main(){
    int n;
    cin >> n ;
    vector<int>bas(n);
    int sum=0;
    for(int i=0;i<n;i++){
        cin >> bas.at(i);
        sum+=bas.at(i);
    }
    int x=round(double(sum)/n);
    int ans=0;
    for(int i=0;i<n;i++){
        ans+=pow(bas.at(i)-x,2);
    }
    cout << ans << endl;
    return 0;
}

Measured memory_bytes: 3724
#include<bits/stdc++.h>
using namespace std;
#define gc getchar_unlocked
#define fo(i,n) for(i=0;i<n;i++)
#define Fo(i,k,n) for(i=k;k<n?i<n:i>n;k<n?i+=1:i-=1)
#define ll long long
#define si(x)	scanf("%d",&x)
#define sl(x)	scanf("%lld",&x)
#define ss(s)	scanf("%s",s)
#define pi(x)	printf("%d\n",x)
#define pl(x)	printf("%lld\n",x)
#define ps(s)	printf("%s\n",s)
#define deb(x) cout << #x << "=" << x << endl
#define deb2(x, y) cout << #x << "=" << x << "," << #y << "=" << y << endl
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define all(x) x.begin(), x.end()
#define clr(x) memset(x, 0, sizeof(x))
#define sortall(x) sort(all(x))
#define tr(it, a) for(auto it = a.begin(); it != a.end(); it++)
#define PI 3.1415926535897932384626
typedef pair<int, int>	pii;
typedef pair<ll, ll>	pl;
typedef vector<int>		vi;
typedef vector<ll>		vl;
typedef vector<pii>		vpii;
typedef vector<pl>		vpl;
typedef vector<vi>		vvi;
typedef vector<vl>		vvl;
int mpow(int base, int exp);
void ipgraph(int m);
void dfs(int u, int par);
const int mod = 1000000007;
const int N = 3e5, M = N;
ll int dp[300][400000];
ll int solve(ll int a[],ll int val[],int n,int w){
if(n==0||w==0)return 0;
if(dp[n][w]!=-1)return dp[n][w];
if(a[n-1]<=w){
    return dp[n][w]=max(solve(a,val,n-1,w-a[n-1])+val[n-1],solve(a,val,n-1,w));
}else
return dp[n][w]=solve(a,val,n-1,w);


}



int main(){
ios_base::sync_with_stdio(0);
ll int t,n,i,j,k,m;
t=1;
while(t--){
ll int w;
cin>>n>>w;
memset(dp,-1,sizeof dp);
ll int a[n];
ll int val[n];
fo(i,n){
    cin>>a[i]>>val[i];
}
cout<<solve(a,val,n,w);
}

}


Measured memory_bytes: 941160
n = int(input())
l, r = 1, n
while l < r:
    m = (l+r)//2
    if (1+m)*m/2 <= n:
        l = m+1
    else:
        r = m
result = [i for i in range(r+1)]
result[(1+r)*r//2 -n] = -1
for i in result[1:]:
    if i < 0:
        continue
    print(i)

Measured memory_bytes: 42736
//
//  atcoder.cpp
//
//
///
#include <bits/stdc++.h>
#include <boost/optional.hpp>
#include <boost/range/irange.hpp>
#include <boost/range/algorithm.hpp>
#include <boost/range/numeric.hpp>
#include <boost/bimap/bimap.hpp>
#include <boost/bimap/multiset_of.hpp>
#include <boost/multi_array.hpp>
#include <boost/math/common_factor.hpp>
#include <boost/icl/interval_map.hpp>
#include <boost/algorithm/string.hpp>
#include <boost/pending/disjoint_sets.hpp>
#include <boost/graph/graph_traits.hpp>
#include <boost/graph/adjacency_list.hpp>
#include <boost/graph/dijkstra_shortest_paths.hpp>
#include <boost/graph/kruskal_min_spanning_tree.hpp>
#include <boost/graph/graph_utility.hpp>
#include <boost/numeric/ublas/vector.hpp>
// ↓これいる？
#include <boost/graph/strong_components.hpp>

#define REP(i, N) for(int i=0; i<(N); i++)
#define REC(f, ...) (void)0; const auto f = [&](const auto f, __VA_ARGS__)
using namespace std;
using namespace boost::math;
using namespace boost::numeric;
using ll = long long;
namespace bst = boost;
template<class T> void fill(T& v) { for (auto&& i : v) cin >> i; }

//#include "atcoder.hpp"

int main() {
    ll N, K;
    
    cin >> N >> K;
  
    vector<ll> A(N), F(N);
  fill(A);
  fill(F);
  
  bst::sort(A, greater<int>());
  bst::sort(F);
  
  ll l = 0, r = 1000000LL*1000000LL;
  
  while (l < r) {
    ll m = (l + r) / 2LL, nowk = K;
 //   if (l == m) m = r;
    REP(i, N) {
      ll sub = A[i] * F[i] - m;
      if (sub > 0LL) {
        nowk -= (sub + F[i] - 1) / F[i];
        
        if (nowk < 0LL) {
        l = m + 1;
          goto FAILSKIP;
        }
      }
    }
    
          r = m;

    FAILSKIP:
    (void)0;
  }
  
    
    cout << l << endl;
    
    return 0;
}







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

int main(){
    int N, M, X;
    cin >> N >> M >> X;
    int C[N], A[N][M];
    rep(i, N){
        cin >> C[i];
        rep(j, M) cin >> A[i][j];
    }

    int S[N]={0};
    int score[M]={0};
    int ans=-1;
    while(true){
        int sum=0;
        rep(i, N) if(S[i]==2){ S[i]=0; S[i+1]++;}
        
        rep(i, N){
            if(S[i]==1){
                rep(j, M) score[j]+=A[i][j];
                sum+=C[i];
            }
        }

        int flag=0;
        rep(i, M) if(score[i]<X) flag=1;
        if(flag==0) if(ans>sum || ans==-1) ans=sum;

        int end=1;
        rep(i, N) if(S[i]==0) end=0;
        if(end==1) break;
        rep(i, M) score[i]=0;
        S[0]=S[0]+1;
    }
    cout << ans << endl;
    return 0;
}
/*
12 1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1
1 1
*/

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

using namespace std;

struct CODE
{
	char before;
	char after;
};

int main(void)
{
	int n;
	
	while (cin >> n, n != 0){
		vector<CODE> code;
		
		while (n-- > 0){
			CODE tmp;
			cin >> tmp.before >> tmp.after;
			code.push_back(tmp);
		}
		
		int m;
		cin >> m;
		while (m-- > 0){
			char c;
			
			cin >> c;
			for (int i = 0; i < code.size(); i++){
				if (c == code[i].before){
					c = code[i].after;
					break;
				}
			}
			cout << c;
		}
		cout << endl;
	}
	
	return 0;
}

Measured memory_bytes: 1212
#include<bits/stdc++.h>
using namespace std;
long long int solve(vector<long long int> &v, vector<long long int> &dp, long long int i, long long int n, long long int k)
{
    if (i==n-1)
    {
        return 0;
    }
    if (dp[i]!=-1)
    {
        return dp[i];
    }

    // long long int one = INT_MAX;
    // long long int two = INT_MAX;
    long long int ans = INT_MAX;
    for (int j=1;j<=k;j++)
    {
        if (i+j<n)
        {
            ans = min(ans, solve(v, dp, i+j, n, k)+abs(v[i+j]-v[i]));
        }
    }
    // if(i+1<n)
    // {
    //     one = solve(v,dp,i+1,n)+abs(v[i+1]-v[i]);
    // }
    // if(i+2<n)
    // {
    //     two = solve(v,dp,i+2,n)+abs(v[i+2]-v[i]);
    // }
    return dp[i] =  ans;
}
int main() {
    long long n, k;
    cin>>n;
    cin>>k;
    vector<long long int> a(n);
    for (long long int i=0;i<n;i++) {
        cin>>a[i];
    }

    vector<long long int> dp(n+1, -1);
    int ans = solve(a, dp, 0, n, k);
    cout<<ans<<endl;
    return 0;
}

Measured memory_bytes: 12740
#coding:utf-8
import sys,os
from collections import defaultdict, deque
from fractions import gcd
from math import ceil, floor
sys.setrecursionlimit(10**6)
write = sys.stdout.write
dbg = (lambda *something: print(*something)) if 'TERM_PROGRAM' in os.environ else lambda *x: 0
def main(given=sys.stdin.readline):
    input = lambda: given().rstrip()
    LMIIS = lambda: list(map(int,input().split()))
    II = lambda: int(input())
    XLMIIS = lambda x: [LMIIS() for _ in range(x)]
    YN = lambda c : print('Yes') if c else print('No')
    MOD = 10**9+7

    N, K = LMIIS()
    A = LMIIS()

    ans = 0
    k = (1+K)*K//2
    for i in range(N-1):
        for j in range(i+1,N):
            if A[i] > A[j]:
                ans += (1+K)*K//2
                ans %= MOD
    for i in range(1,N):
        for j in range(i):
            if A[i] > A[j]:
                ans += K*(K-1)//2
                ans %= MOD

    print(ans)
    

if __name__ == '__main__':
    main()

Measured memory_bytes: 66540
#include<iostream>
#include<vector>
using namespace std;
int main() {
  int N;
  char s[100010];
  cin >> s;
  int i;
  for(i=0;i<100010;i++) {
    if(s[i] == '\0') {
      N = i;
      i = 1000000;
    }
  }
  vector<int> R(N),L(N),ans(N,0),P(N);
  int Rflag=0,Lflag=0,Pflag=0;
  for(i=0;i<N;i++) {
    if(s[i] == 'R') {
      R[Rflag]++;
      if(s[i+1] == 'L') {
        Rflag++;
        P[Pflag] = i;
        Pflag++;
      }
    } else {
      L[Lflag]++;
      if(s[i+1] != 'L')Lflag++;
    }
  }
  for(i=0;i<Pflag;i++) {
    ans[P[i]] = (R[i]+1)/2 + L[i]/2;
    ans[P[i]+1] = R[i]/2 + (L[i]+1)/2;
  }
  for(i=0;i<N;i++) {
    printf("%d ",ans[i]);
  }
  printf("\n");
}

Measured memory_bytes: 2048
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
#define mp make_pair
#define pb push_back
#define ip pair<ll,ll>
#define ff first
#define ss second
#define MAX 100005
const ll MOD = 1000000007;

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

	int n;
	cin>>n;
	int a[n];
	for(int i=0;i<n;i++)
		cin>>a[i];
	ll cur = 1000;
	for(int i=0;i+1<n;i++){
		if(a[i]<a[i+1]){
			ll x = cur/a[i];
			cur += x*(a[i+1]-a[i]);
		}
	}

	cout<<cur<<"\n";
}

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

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

    bool can = false;
    for (int i=0; i<=N/4; ++i) {
        for (int j=0; j<=N/7; ++j) {
            if (4*i+7*j == N) {
                can = true;
                break;
            }
        }
    }

    if (can) cout << "Yes" << endl;
    else cout << "No" << endl;

    return 0;
}

Measured memory_bytes: 256
#include<bits/stdc++.h>
using namespace std;
int main()
{
  int n,k,a[1000];
  cin>>n>>k;
  for(int i=0;i<n;i++)
  {
    cin>>a[i];
  }
  int sum=0;
  sort(a,a+n);
  for(int i=0;i<k;i++)
  {
    sum=sum+a[i];
  }
  cout<<sum<<endl;
}


Measured memory_bytes: 3644
#!/usr/bin/ruby
n,*a=$<.read.split.map &:to_i
n.times{|i|
	(i+1...n).each{|j|
		if (a[i]-a[j])%~-n==0
			puts '%d %d'%[a[i],a[j]]
			exit
		end
	}
}




Measured memory_bytes: 8944
#include<stdio.h>
int main(){

  int H,W;
  int i,j;
  for(i=0;;i++){

  scanf("%d %d",&H,&W);
  if(H==0 && W==0) break;

  for(i=0;i<H;i++){
    for(j=0;j<W;j++){
      printf("#");
    }
    printf("\n");
  }
  printf("\n");

  }
  return 0;
}

Measured memory_bytes: 572
#[allow(dead_code)]
fn read<T: std::str::FromStr>() -> T {
    let mut s = String::new();
    std::io::stdin().read_line(&mut s).ok();
    s.trim().parse().ok().unwrap()
}

#[allow(dead_code)]
fn read_vec<T: std::str::FromStr>() -> Vec<T> {
    read::<String>()
        .split_whitespace()
        .map(|e| e.parse().ok().unwrap())
        .collect()
}

#[allow(dead_code)]
fn yn(result: bool) {
    if result {
        println!("Yes");
    } else {
        println!("No");
    }
}

fn main() {
    loop {
        let v = read_vec::<u32>();
        let a = v[0];
        let b = v[1];
        if a == 0 && b == 0 { break; }

        for i in 0..a {
            let line = match i {
                x if x == 0 || x == a - 1  => {
                    (0..b).map(|_| '#').collect::<String>()
                },
                _ => {
                    (0..b).map(|i| {if i == 0 || i == b-1 { '#' } else { '.' }}).collect::<String>()
                }
            };
            println!("{}", line);
        }
        println!("");
    }
}



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


/* エイリアス */
#define ll long long
#define ld long double
#define vi vector<int>
#define vll vector<ll>
#define pii pair<int,int>
#define pll pair<ll,ll>
#define vpi vector<pii>
#define vpll vector<pll>
#define endl '\n'

#define fi first
#define se second
#define pb push_back
#define eb emplace_back
#define em emplace


/* 関数マクロ */
#define rep(i, n) for (ll i = 0; i < n; ++i)
#define rep2(i, n, m) for (ll i = n; i <= m; ++i)
#define rep3(i, n, m) for (ll i = n; i >= m; --i)
#define all(v) v.begin(), v.end()
#define si(v) int(v.size())
#define UNIQUE(v) sort(all(v)), v.erase(unique(all(v)),v.end())


/* 定数 */
const ll mod = 1e9 + 7;
const ll infll = (1LL << 62) - 1;
const ll inf = (1LL << 30) - 1;


/* その他 */
template<class S, class T> inline bool chmax(S &a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class S, class T> inline bool chmin(S &a, T b) { if (a > b) { a = b; return 1; } return 0; }
template<class T> using pq = priority_queue<T>;
template<class T> using pqg = priority_queue<T, vector<T>, greater<T>>;


signed main()
{
  ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  cout << fixed << setprecision(15);
  
  int n; cin >> n;
  vll a(n);

  rep(i, n) cin >> a[i];

  rep(i, n) {
    if (a[i] == 0) {
      cout << 0 << endl;
      return 0;
    }
  }

  ll ans = 1;
  rep(i, n) {
    if (ans > (1000000000000000000 / a[i])) {
      cout << -1 << endl;
      return 0;
    }
    ans *= a[i];
  }

  cout << ans << endl;
}

Measured memory_bytes: 3980
#include <iostream>
using namespace std;
int main() {
  string s; cin >> s;
  long long N = s.size();
    long long tot = 0;
    long long numb = 0;
    for(long long i = 0; i < N; i++) {
      if(s[i] == 'B') {
          tot += i;
            numb++;
        }
    }
    long long lasts = 0;
    for(long long i = 1; i <= numb; i++) {
      lasts += N - i;
    }
    cout << lasts - tot << endl;
}

Measured memory_bytes: 640
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main() {
    int n, k;
    cin >> n >> k;
    vector<int> v;
    for (int i=0; i<n; i++) {
        int h;
        cin >> h;
        v.push_back(h);
    }
    sort(v.begin(), v.end());
    int minDiff = 1000000000;
    for (int i=0; i+k-1<n; i++) {
        minDiff = min(minDiff, v[i+k-1]-v[i]);
    }
    cout << minDiff << endl;
}

Measured memory_bytes: 3880
#!/usr/bin/env python3
import sys
sys.setrecursionlimit(10**8)
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def ZIP(n): return [LIST() for _ in range(n)]
def NSTR(n): return [input() for _ in range(n)]


INF = float("inf")
MOD = 10**9 + 7


def main():
    X = INT()
    if 30 <= X:
        print("Yes")
    else:
        print("No")

    return


if __name__ == '__main__':
    main()


Measured memory_bytes: 9188
N = gets.chomp.to_i
S = N.times.map{gets.chomp}
hash = {}

S.each do |s|
  if hash[s]
    hash[s] += 1
  else
    hash[s] = 1
  end
end

arr = hash.sort_by { |k, v| v }
arr.reverse!

max = arr[0][1]

arr.select! do |a|
  a[1] == max
end

arr.map{|a| a[0]}
arr.sort!

arr.each do |a|
    puts a[0]
end


Measured memory_bytes: 48608
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <map>
#include <cmath>


typedef long long ll;

#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define _GLIBCXX_DEBAC
/*map<Keyの型, Valueの型> 変数名;
queue<型> 変数名;
priority_queue<型> 変数名;
priority_queue<型, vector<型>, greater<型>> 変数名;*/
//copy(v1.begin(), v1.end(), back_inserter(v2));
using namespace std;

int gcd(int a, int b) {
	int c;
	if (a < b) {
		c = a; a = b; b = c;
	}
	while (b != 0) {
		c = a % b; a = b; b = c;
	}
	return a;
}

int sort_greater(int m,int n) {
	vector<int> a(m);
	for (int i = 0; i < m; i++) {
		cin >> a[i];
	}
	sort(a.begin(), a.end(),greater<int>());
	for (int i = 0; i < n; i++) {
		cout << a[i];
	}
		return 0;
}

int bubblesort(int a[], int n) {
	int sw = 0;
	bool flag = 1;
	for (int i = 0; flag; i++) {
		flag = 0;
		for (int j = n - 1; j >= i + 1; j--) {
			if (a[j] < a[j - 1]) {
				swap(a[j], a[j - 1]);
				flag = 1;
				sw += 1;
			}
		}
	}
	return sw;
}

int a_z()
{
	for (int i = 0; i <= ('Z' - 'A'); i++) {
		cout << (char)('A' + i);
	}
	return 0;
}

int selecttionsort(int a[], int n) {
	int t,sw = 0, minj;
	for (int i = 0; i < n - 1; i++) {
		minj = i;
		for (int j = 0; j < n - 1; j++) {
			if (a[j] < a[minj]) {
				minj = j;
			}
		}
		t = a[i]; a[i] = a[minj]; a[minj] = t;
		if (i != minj) sw += 1;

	}
	return sw;
}

bool is_prime(int x) {
	if (x <= 1) return false;
	for (int i = 2; i * i <= x; i++) {
		if (x % i == 0) return false;
	}
	return true;
}

map<string, int> memo;


int main() {
	//sort(a.begin(),a.end());
	string s,r;
	cin >> s;
	r = s;
	reverse(r.begin(), r.end());
	int ans = 0;
	rep(i, s.size()) {
		if (r[i] != s[i]) ans++;
	}
	cout << ans/2;
	return 0;
}









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

int main() {

  int N;
  cin >> N;

  int answer = 111;

  if (100 <= N && N <= 111) {
    answer = 111;
  } else if (112 <= N && N <= 222) {
    answer = 222;
  } else if (223 <= N && N <= 333) {
    answer = 333;
  } else if (334 <= N && N <= 444) {
    answer = 444;
  } else if (445 <= N && N <= 555) {
    answer = 555;
  } else if (556 <= N && N <= 666) {
    answer = 666;
  } else if (667 <= N && N <= 777) {
    answer = 777;
  } else if (778 <= N && N <= 888) {
    answer = 888;
  } else {
    answer = 999;
  }
  
  cout << answer << endl;

}

Measured memory_bytes: 256
S = input()
for i in range(len(S)+1):
  if i == len(S):
    print('Yes')
    break
  if S[i] =='U' or S[i] =='D':
    pass
  else:
    if i % 2 ==0:
      if S[i] == 'R':
        pass
      else:
        print('No')
        break
    else:
      if S[i]== 'L':
        pass
      else:
        print('No')
        break
        
      
    


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

int main() {
  int A,B,K;
  cin >> A >> B >> K;
  //A,Bを割り切る整数を求める
  int cnt = 0;//整数のカウント
  for(int i = 100; i >= 1; i--){
    if(A % i == 0 && B % i == 0){//もしAをiで割ってあまりがゼロかつBをiで割ってあまりがゼロなら
      cnt++;
      if(cnt == K){
        cout << i << endl;
      }
    }
  }
}

Measured memory_bytes: 3656
x = list(map(int, input().split()))
if x.count(1) == 0:
    print(1)
elif x.count(2) == 0:
    print(2)
elif x.count(3) == 0:
    print(3)
elif x.count(4) == 0:
    print(4)
elif x.count(5) == 0:
    print(5)

Measured memory_bytes: 9120
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(k,i,n) for(ll i=k;i<n;++i)
int main(void){
    // Your code here!
    ll n,a,b;
    cin>>n>>a>>b;
    cout<<min(n*a,b);
    
}


Measured memory_bytes: 256
import sys

readline = sys.stdin.readline
MOD = 10 ** 9 + 7
INF = float('INF')
sys.setrecursionlimit(10 ** 5)


def main():
    def calc(nums):
        ret = 0
        for a, b, c, d in mat:
            if nums[b - 1] - nums[a - 1] == c:
                ret += d

        return ret

    n, m, q = list(map(int, readline().split()))
    mat = [list(map(int, readline().split())) for _ in range(q)]

    ans = 0

    from collections import deque

    que = deque()
    que.append(([1], 1, 1))
    # 本体、長さ、右はじ

    while que:
        nums, l, right = que.pop()

        if l == n:
            temp = calc(nums)
            ans = max(ans, temp)
        else:
            for i in range(right, m + 1):
                nex = nums + [i]
                que.append((nex, l + 1, i))

    print(ans)

if __name__ == '__main__':
    main()


Measured memory_bytes: 69468
#include <iostream>
#include <cmath>

using namespace std;

int main(void) {
    long N;
    cin>>N;
    long n = sqrt(N);
    int MIN = 1<<30;
    for (long a=1; a<=n; ++a) {
        long mod = N % a;
        if (mod == 0) {
            long b = N / a;
            int lena = to_string(a).size();
            int lenb = to_string(b).size();
            MIN = min(max(lena,lenb), MIN);
        }
    }
    cout<<MIN<<endl;
    return 0;
}


Measured memory_bytes: 888
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
const ll MOD = 1e9 + 7;
const ll INF = 1e18;
#define rep(i,m,n) for(ll i = (m); i <= (n); i++)
#define rrep(i,m,n) for(ll i = (m); i >= (n); i--)
#define print(x) cout << (x) << endl;
#define printa(x,m,n) for(ll i = (m); i <= n; i++){cout << (x[i]) << " ";} cout<<endl;

ll n, m, a;
string s;

int main(){
	cin.tie(0); ios::sync_with_stdio(false);
	
	cin >> n >> m;
	
	priority_queue<ll> q;
	rep(i, 1, n){
		cin >> a;
		q.push(a);
	}
	
	rep(j, 1, m){
		ll tmp = q.top(); q.pop();
		q.push(tmp/2);
	}
	
	ll ans = 0;
	while(!q.empty()){
		ans += q.top(); q.pop();
	}
	print(ans)
	
	return 0;
}

Measured memory_bytes: 1400
import math
import functools
import itertools
import numpy as np
import sys
MAX_INT = int(10e10)
MIN_INT = -MAX_INT
mod = 1000000007
sys.setrecursionlimit(1000000)
def IL(): return list(map(int,input().split()))
def SL(): return input().split()
def I(): return int(sys.stdin.readline())
def S(): return input()

A,B,T = list(map(float,input().split()))

num = ((T+0.5)//A )*B

print(int(num))


Measured memory_bytes: 21964
import math
import copy
from operator import mul
from functools import reduce
from collections import defaultdict
from collections import Counter
from collections import deque
# 直積 A={a, b, c}, B={d, e}:のとき，A×B={(a,d),(a,e),(b,d),(b,e),(c,d),(c,e)}: product(A, B)
from itertools import product
# 階乗 P!: permutations(seq), 順列 {}_len(seq) P_n: permutations(seq, n)
from itertools import permutations
# 組み合わせ {}_len(seq) C_n: combinations(seq, n)
from itertools import combinations
# 一次元累積和
from itertools import accumulate
from bisect import bisect_left, bisect_right

# import numpy as np
# from scipy.special import perm
from scipy.misc import comb

def inside(y, x, H, W):
    return 0 <= y < H and 0 <= x < W

# 四方向: 右, 下, 左, 上
dy = [0, -1, 0, 1]
dx = [1, 0, -1, 0]
 
def i_inpl(): return int(input())
def l_inpl(): return list(map(int, input().split()))
INF = float("inf")
import scipy.misc as scm
########

N, K = l_inpl()

def cmb(n, r):
    r = min(n - r, r)
    if r == 0: 
        return 1
    over = reduce(mul, range(n, n - r, -1))
    under = reduce(mul, range(1, r + 1))
    return over // under

def per(n, r):
    if r == 0: 
        return 1
    return reduce(mul, range(n, n - r, -1))

red_space = N - K + 1

MOD = 1000000007
for i in range(1, K+1):
    print(comb(K-1, i-1, 1)*comb(red_space, i, 1) % MOD)

Measured memory_bytes: 14180
#include <iostream>

int main(){
  int n;

  while(std::cin >> n, n){
    int A_point=0,B_point=0;
    int A_card,B_card;
    while(n--){
      std::cin >> A_card >> B_card;
      if(A_card > B_card){
        A_point += A_card + B_card;
      }else if(A_card < B_card){
        B_point += A_card + B_card;
      }else{
        A_point += A_card;
        B_point += B_card;
      }
    }
    std::cout << A_point << " " << B_point << std::endl;
  }

  return 0;
}

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

int main() {
    int d;  cin >> d;
    if (d == 25)  cout << "Christmas" << endl;
    else if (d == 24)  cout << "Christmas Eve" << endl;
    else if (d == 23)  cout << "Christmas Eve Eve" << endl;
    else  cout << "Christmas Eve Eve Eve" << endl;
}

Measured memory_bytes: 256
h, w = [int(i) for i in input().split()]

s = [input() for _ in range(h)]

def search(s_h, s_w):
    stack = []
    isuse = [[False] * w for _ in range(h)]
    def add(a, b,score):
        nonlocal stack, isuse
        if not (0 <= a < h and 0 <= b < w):
            return 0
        if s[a][b] == "#":
            return 0
        if isuse[a][b]:
            return 0
        isuse[a][b] = True
        stack.append((a,b,score+1))
        
    add(s_h, s_w, -1)
    c=0
    while stack:
        a,b,c = stack.pop(0)
        add(a + 1, b, c)
        add(a , b + 1, c)
        add(a, b - 1, c)
        add(a - 1, b, c)
    return c

search(0,0)

ans = 0
for s_h in range(h):
    for s_w in range(w):
        ans = max(search(s_h, s_w), ans)
        #print(s_h,s_w,search(s_h, s_w))
print(ans)


Measured memory_bytes: 74680
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
int n,m,a[100010],sum;
inline bool cmp(int aa,int bb){return aa>bb;}
int main()
{
	scanf("%d%d",&n,&m);
	for (int i=1;i<=n;i++) scanf("%d",&a[i]),sum+=a[i];
	sort(a+1,a+n+1,cmp);
	if (a[m]*4*m>=sum) printf("Yes\n");else printf("No\n");
return 0;
}

Measured memory_bytes: 256
import heapq


def solve(string):
    n, m, *ab = map(int, string.split())
    ab = sorted([(a, b) for a, b in zip(*[iter(ab)] * 2)])
    hq, index, cap, count, ans = [], 0, 1, m, 0
    while index < n:
        _a, _b = ab[index]
        if _a > m:
            break
        if _a <= cap:
            heapq.heappush(hq, -_b)
            index += 1
        else:
            ans -= heapq.heappop(hq) if len(hq) > 0 else 0
            count -= 1
            cap += 1
    ans -= sum([heapq.heappop(hq) if len(hq) > 0 else 0 for _ in range(count)])
    return str(ans)


if __name__ == '__main__':
    n, m = map(int, input().split())
    print(solve('{} {}\n'.format(n, m) + '\n'.join([input() for _ in range(n)])))


Measured memory_bytes: 25840
#include <iostream>
#include <string>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <cctype>
#include <map>
#include <vector>

#define div 1000000000+7

typedef long long ll;

using namespace std;

void solve() {
    int n, m;
    int t[100], p[100], x[100];
    long time = 0;

    cin >> n;
    for(int i = 0; i < n; i++){
        cin >> t[i];
    }
    cin >> m;
    for(int i = 0; i < m; i++){
        cin >> p[i] >> x[i];
    }

    for(int i = 0; i < m; i++){
        for(int j = 0; j < n; j++){
            if(j == p[i]-1){
                time += x[i];
            }else{
               time += t[j];
            }
        }
        cout << time << endl;
        time = 0;
    }
}

int main() {
    solve();
    return 0;
}


Measured memory_bytes: 256
#include<bits/stdc++.h>
#define ll long long int
using namespace std;
const int mod=1e9+7;
// Not for Rating """"""""""I'm in Love with Experience :)""""""""""""""""""
ll check(ll a[],ll n,ll k,ll mid)
{
    ll i,j;
    ll res=0;
    for(i=0;i<n;i++)
    {
        res+=(((a[i]+mid-1)/mid)-1); 
    }
    if(res<=k)
    {
        return 1;
    }
    return 0;
}
void solve()
{
    ll n,k;
    ll i,j;
    cin>>n>>k;
    ll a[n];
    for(i=0;i<n;i++)
    {
        cin>>a[i];
    }
    ll maxi = *max_element(a,a+n);
    
    ll l=1,r=maxi;
    ll ans=maxi;
    while(l<=r)
    {
        ll mid = l+(r-l)/2;
        
        if(check(a,n,k,mid))
        {
            ans=min(ans,mid);
            r=mid-1;
        }
        else{
            l=mid+1;
        }
    }
    cout<<ans<<"\n";
 
    
 
}
int main(void)
{
    ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
    int T=1;while(T--){solve();}return 0;
}/*Solved By:- RITIK AGARWAL*/

Measured memory_bytes: 5136
#include <iostream>
#include <iomanip>
#include <climits>
#include <algorithm>
#include <vector>
#include <deque>
#include <queue>
#include <list>
#include <stack>
#include <string>
#include <functional>
#include <numeric>
#include <map>
#include <set>
#include <cstdlib>
#include <bitset>
#include <unordered_map>
#include <random>
#define _USE_MATH_DEFINES
#include <math.h>
#include <complex>

using namespace std;

#define INF (int)1e8
#define _INF (int)-1e8
#define INFLL (long long)1e15
#define _INFLL (long long)-1e15
#define Loop(i, n) for(int i = 0; i < (int)n; i++)
#define Loop1(i, n) for(int i = 1; i <= (int)n; i++)
#define Loopr(i, n) for(int i = (int)n - 1; i >= 0; i--)
#define Loopr1(i, n) for(int i = (int)n; i >= 1; i--)
#define bitmanip(m,val) static_cast<bitset<(int)m>>(val)
typedef long long int ll;
typedef vector<int> vi;
typedef vector<vector<int>> vvi;
typedef pair<int, int> P;
typedef vector<ll> vll;
typedef vector<vector<ll>> vvll;
typedef vector<double> vd;
typedef vector<vector<double>> vvd;
typedef complex<double> cdouble;
typedef vector<complex<double>> vcd;
typedef vector<vector<complex<double>>> vvcd;

/*******************************************************/

ll ceil_div(ll a, ll b) {
  if (a % b == 0) return a / b;
  else return a / b + 1;
}

class Binary_Search {
private:
  ll l;
  ll r;
  ll m;
public:
  ll result;
  // search [l, r), rule should be [y,...,y,n...,n], and return the index that causes the last 'y'
  Binary_Search(ll range_l, ll range_r, bool(*rule)(ll), bool upper_flag) {
    l = range_l;
    r = range_r;
    while (r - l > 1) {
      m = (l + r) / 2;
      if (rule(m)) l = m;
      else r = m;
    }
    result = upper_flag ? l + 1 : l;
  }
};

namespace BS_Rules {
  ll K;
  ll A;
  ll B;
  bool bs_rule(ll a) {
    if (B - a < 0) return false;
    ll b = (B - a) / K;
    if (a * K + b <= A) return true;
    else return false;
  }
}

int main() {
  int Q; cin >> Q;
  Loop(q, Q) {
    ll A, B, C, D; cin >> A >> B >> C >> D;
    ll K;
    if (A >= B) K = ceil_div(A, B + 1);
    else K = ceil_div(B, A + 1);
    BS_Rules::K = K;
    BS_Rules::A = A;
    BS_Rules::B = B;
    Binary_Search *bs = new Binary_Search(0, A / K + 1, BS_Rules::bs_rule, false);
    ll a = bs->result;
    ll b = (B - a) / K;
    ll mid_A = A - a * K - b;
    ll mid_B = B - a - b * K;
    ll L = D - C + 1;
    Loop(i, L) {
      ll focus = C + i;
      if (focus <= a * (K + 1)) {
        if (focus % (K + 1) == 0) cout << "B";
        else cout << "A";
      }
      else if (focus <= a * (K + 1) + mid_A) cout << "A";
      else if (focus <= a * (K + 1) + mid_A + mid_B) cout << "B";
      else {
        if ((focus - a * (K + 1) - mid_A - mid_B) % (K + 1) == 1) cout << "A";
        else cout << "B";
      }
    }
    cout << endl;
  }
  return 0;
}

Measured memory_bytes: 384
def myAnswer(S:str)-> str:
   N = len(S)
   if(N == 1): return "YES"
   dic = {"a":0,"b":0,"c":0}
   for s in S:
      dic[s] += 1
   # print(dic)
   dic = list(dic.values())
   dic.sort(reverse = True)
   first = dic.pop(0)
   seccond = dic.pop(0)
   third = dic.pop(0)
   if(first - third <= 1):
      return "YES"
   else:
      return "NO"




def modelAnswer():
   return
def main():
   S = (input())
   print(myAnswer(S))
if __name__ == '__main__':
   main()

Measured memory_bytes: 3316
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define zero_pad(num) setfill('0') << std::right << setw(num)
using namespace std;
using ll = long long;
using ld = long double;
using P = pair<int, int>;

int main() {
    int n, c;
    cin >> n >> c;
    vector<vector<int>> time(c, vector<int>(100005, 0));
    rep(i, n){
        int s, t, C;
        cin >> s >> t >> C;
        C--;
        time[C][s]++;
        time[C][t]--;
    }

    rep(i, c){
        for(int j = 1; j < 100005; ++j){
            if(time[i][j] > 0){
                time[i][j - 1] += time[i][j];
                time[i][j] = 0;
            }
        }
    }

    rep(i, 100005){
        for(int j = 1; j < c; ++j){
            time[0][i] += time[j][i];
        }
    }

    vector<int> sum(100005, 0);
    sum[0] = time[0][0];
    int ans = sum[0];
    rep(i, 100004){
        sum[i + 1] = sum[i] + time[0][i + 1];
        ans = max(ans, sum[i + 1]);
    }
    cout << ans << endl;
}

Measured memory_bytes: 12416
#pragma comment(linker, "/STACK:32000000")
#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0, _= (n); i < _; ++i)
#define DWN(i, n) for (int i = (n) - 1; i >= 0; --i)
#define FOR(i, l, r) for (int i = (l), _ = (r); i < _; ++i)
#define EDGE(u, v, e) for (int e = head[u], v; e != nil && (v = to[e], true); e = next[e])

using namespace std;

typedef unsigned int uint;
typedef long long int64;
typedef unsigned long long uint64;
typedef complex<double> comp;

inline bool read(int& val)    { return scanf("%d",    &val) != -1;}
inline bool read(int64& val)  { return scanf("%I64d", &val) != -1;}
inline bool read(double& val) { return scanf("%lf",   &val) != -1;}
inline bool read(char* val)   { return scanf("%s",     val) != -1;}
template<class T1, class T2>
inline bool read(T1& a, T2& b)
{ return read(a) && read(b); }
template<class T1, class T2, class T3>
inline bool read(T1& a, T2& b, T3& c)
{ return read(a) && read(b) && read(c); }
template<class T1, class T2, class T3, class T4>
inline bool read(T1& a, T2& b, T3& c, T4& d)
{ return read(a) && read(b) && read(c) && read(d); }
template<class T1, class T2, class T3, class T4, class T5>
inline bool read(T1& a, T2& b, T3& c, T4& d, T5& e)
{ return read(a) && read(b) && read(c) && read(d) && read(e); }

const int nil = -1;
const int N = 211111;

int cnt[N];
int64 pow(int64 a, int64 b, int64 mod) {
	int64 x = 1;
	for (; b != 0; b >>= 1, a = a * a % mod)
		if (b & 1) x = x * a % mod;
	return x;
}

int main() {
	int n; read(n);
	REP (i, n) {
		int a; read(a); cnt[a]++;
	}
	int ans = 0;
	bool ok = true;
	if (n % 2 == 0) {
		for (int i = 1; i < n; i += 2) {
			if (cnt[i] != 2) ok = false;
		}
	} else {
		for (int i = 2; i < n; i += 2) {
			if (cnt[i] != 2) ok = false;
		}
	}
	if (ok) ans = pow(2, n / 2, 1000000007);
	printf("%d\n", ans);
	return 0;
}



Measured memory_bytes: 764
#-*-coding:utf-8-*-
import sys

def main2():
    n,y=map(int,input().split())
    for i in range(n+1):
        for j in range(n+1-i):
            if 10000*i+5000*j+1000*(n-i-j)==y:
                print(i,j,n-i-j)
                sys.exit()
    print(-1,-1,-1)

if __name__=="__main__":
    main2()

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

using uint = unsigned int;
using ll = long long;
using ull = unsigned long long;
#include <string>
#include <cstdio>
#include <algorithm>
#include <vector>
#include <cmath>
#include <climits>
#include <bitset>
#include <array>
#include <deque>
#include <queue>
#include <map>
#define all(x) (x).begin(),(x).end()

void solve(){

}
int main(){
    int N,T;
    cin >> N >> T;
    vector<int> t(N);
    for (int i = 0; i < N; i++){
        cin >> t[i];
    }

    ll ans = T;
    for (int i = 0; i < N-1; i++){
        ans += min(t[i+1] - t[i], T);
    }

    cout << ans << endl;



    return 0;
}


Measured memory_bytes: 1024
#include <iostream>
#include <set>
#include <map>
#include <vector>
#include <algorithm>
#include <iomanip>
#include <climits>
#include <numeric>
#include <cmath>
#include <queue>
#include <sstream>

using namespace std;
typedef long long ll;
vector<ll> all(55);
vector<ll> p(55);

ll search(ll n, ll x) {
  if (x <= 0) {
    return 0;
  }
  if (n == 0) {
    return 1;
  }
  if (all[n] == x) {
    return p[n];
  }
  ll mid = (all[n] + 1) / 2;
  if (mid == x) {
    return p[n - 1] + 1;
  }
  if (mid > x) {
    return search(n - 1, x - 1);
  }
  return p[n - 1] + 1 + search(n - 1, x - all[n - 1] - 2);
}

int main() {
  ll n, x;
  cin >> n >> x;
  
  // 初期値
  all[0] = 1;
  p[0] = 1;
  for (ll i = 1; i <= n; i++) {
    all[i] = all[i - 1] * 2 + 3;
    p[i]   = p[i - 1] * 2 + 1;
  }
  cout << search(n, x) << endl;
  return 0;
}


Measured memory_bytes: 256
from collections import deque,defaultdict,Counter
from itertools import accumulate
import bisect
from heapq import heappop,heappush
from fractions import gcd
from copy import deepcopy
import math
import queue
#import numpy as np
#import sympy as syp(素因数分解とか)
Mod = 1000000007
import sys

sys.setrecursionlimit(100000) 
  
def sieve_of_eratosthenes(n):
    if not isinstance(n,int):
        raise TypeError("n is not int")
    if n<2:
        raise ValueError("n is not effective")
    prime = [1]*(n+1)
    for i in range(2,int(math.sqrt(n))+1):
        if prime[i] == 1:
            for j in range(2*i,n+1):
                if j%i == 0:
                    prime[j] = 0
    res = []
    for i in range(2,n+1):
        if prime[i] == 1:
            res.append(i)
    return res

def factorial(i):
    if i == 1:
        return 1
    else:
        return i*factorial(i-1)
 
class UnionFind:
    def __init__(self,n):
        self.parent = [i for i in range(n+1)]
        self.rank = [0 for i in range(n+1)]
    
    def findroot(self,x):
        if x == self.parent[x]:
            return x
        else:
            y = self.parent[x]
            y = self.findroot(self.parent[x])
            return y
    
    def union(self,x,y):
        px = self.findroot(x)
        py = self.findroot(y)
        if px < py:
            self.parent[py] = px
        else:
            self.parent[px] = py
 
    def same_group_or_no(self,x,y):
        return self.findroot(x) == self.findroot(y)




def main(): #startline--------------------------------------------
    N,C = map(int,input().split())
    D = [list(map(int,input().split())) for i in range(C)]
    c = [list(map(int,input().split())) for i in range(N)]
    t = [[0]*C for i in range(3)]
    ans = float("inf")
    for i in range(N):
        for j in range(N):
            t[(i+j)%3][c[i][j]-1] += 1
    for i in range(C):
        for j in range(C):
            if i!=j:
                for k in range(C):
                    if i != k and j != k:
                        tt = 0
                        for l in range(C):
                            tt += D[l][i]*t[0][l]+D[l][j]*t[1][l]+D[l][k]*t[2][l]
                        ans = min(ans,tt)
    print(ans)
if __name__ == "__main__":
    main() #endline===============================================

Measured memory_bytes: 7600