akhauriyash/Code-Regression huggingface dataset:
from time import *
from random import *
t=time()
T0,T1=2000,600
Tt=2000
D=int(input())
C=list(map(int,input().split()))
S=[list(map(int,input().split())) for i in range(D)]
X=[0]*26
XX=[0]*26
P=[]
for i in range(D):
  for j in range(26):
    X[j]+=C[j]
    XX[j]+=X[j]
  P.append([])
  for j in range(26):
    P[i].append(j)
  P[i].sort(reverse=True,key=lambda x:X[x]+S[i][x])
  XX[P[i][0]]-=X[P[i][0]]
  X[P[i][0]]=0
SC,SC2=0,0
for i in range(D):
  SC+=S[i][P[i][0]]
YY=[]
Y=[]
A,B=0,0
e=0
E=1
for i in range(50):
  e+=E
  E*=1/(i+1)
def f(x):
  global e,Tt
  if x>=0:
    return True
  else:
    return randrange(0,10000000)<=pow(e,x/Tt)
for l in range(8):
  for k in range(25):
    for i in range(D):
      SC2=SC
      YY=XX[:]
      Y=X[:]
      SC2-=S[i][P[i][0]]
      SC2+=S[i][P[i][k+1]]
      for j in range(26):
        Y[j]=0
      YY[P[i][0]],YY[P[i][k+1]]=0,0
      for j in range(D):
        if j==i:
          Z=P[j][k+1]
        else:
          Z=P[j][0]
        Y[P[i][0]]+=C[P[i][0]]
        Y[P[i][k+1]]+=C[P[i][k+1]]
        Y[Z]=0
        YY[P[i][0]]+=Y[P[i][0]]
        YY[P[i][k+1]]+=Y[P[i][k+1]]
      if f((SC2-sum(YY))-(SC-sum(XX))):
        P[i].insert(0,P[i][k+1])
        del P[i][k+2]
        XX=YY[:]
        SC=SC2
    for i in range(1,D-1):
      SC2=SC
      YY=XX[:]
      Y=X[:]
      A=i
      B=randrange(A+1,D)
      SC2-=S[A][P[A][0]]+S[B][P[B][0]]
      SC2+=S[A][P[B][0]]+S[B][P[A][0]]
      for j in range(26):
        Y[j]=0
      YY[P[A][0]],YY[P[B][0]]=0,0
      for j in range(D):
        if j==A:
          Z=P[B][0]
        elif j==B:
          Z=P[A][0]
        else:
          Z=P[j][0]
        Y[P[A][0]]+=C[P[A][0]]
        Y[P[B][0]]+=C[P[B][0]]
        Y[Z]=0
        YY[P[A][0]]+=Y[P[A][0]]
        YY[P[B][0]]+=Y[P[B][0]]
      if f((SC2-sum(YY))-(SC-sum(XX))):
        P[A],P[B]=P[B][:],P[A][:]
        XX=YY[:]
        SC=SC2
      if k&1:
        continue
      B=randrange(0,A)
      SC2-=S[A][P[A][0]]+S[B][P[B][0]]
      SC2+=S[A][P[B][0]]+S[B][P[A][0]]
      for j in range(26):
        Y[j]=0
      YY[P[A][0]],YY[P[B][0]]=0,0
      for j in range(D):
        if j==A:
          Z=P[B][0]
        elif j==B:
          Z=P[A][0]
        else:
          Z=P[j][0]
        Y[P[A][0]]+=C[P[A][0]]
        Y[P[B][0]]+=C[P[B][0]]
        Y[Z]=0
        YY[P[A][0]]+=Y[P[A][0]]
        YY[P[B][0]]+=Y[P[B][0]]
      if f((SC2-sum(YY))-(SC-sum(XX))):
        P[A],P[B]=P[B][:],P[A][:]
        XX=YY[:]
        SC=SC2
    Tt=pow(T0,1-(time()-t))*pow(T1,time()-t)
'''
X=[0]*26
SC=0
for i in range(D):
  for j in range(26):
    X[j]+=C[j]
  X[P[i][0]]=0
  SC-=sum(X)
  SC+=S[i][P[i][0]]
print(SC)
'''
for i in range(D):
  print(P[i][0]+1)

Measured memory_bytes: 94052
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<set>
#include<map>
#include<queue>
#include<cassert>
#define PB push_back
#define MP make_pair
#define sz(v) (in((v).size()))
#define forn(i,n) for(in i=0;i<(n);++i)
#define forv(i,v) forn(i,sz(v))
#define fors(i,s) for(auto i=(s).begin();i!=(s).end();++i)
#define all(v) (v).begin(),(v).end()
using namespace std;
typedef int in;
typedef vector<in> VI;
typedef vector<VI> VVI;
VI avl;
VI psm;
int main(){
  ios::sync_with_stdio(0);
  cin.tie(0);
  in n,m;
  cin>>n>>m;
  avl.resize(m+3);
  psm.resize(m+3);
  in l,r;
  in tlm=1;
  while((tlm+1)*(tlm+1)<m)
    ++tlm;
  in nxt;
  in nr,nl;
  forn(zz,n){
    cin>>l>>r;
    for(in i=1;i<=tlm;++i){
      if(r/i!=(l-1)/i)
	++avl[i];
    }
    nxt=tlm+1;
    for(in hts=tlm+4;hts>=1;--hts){
      nl=(l-1)/hts+1;
      nr=r/hts;
      nl=max(nl,nxt);
      if(nl>nr)
	continue;
      ++psm[nl];
      --psm[nr+1];
      nxt=nr+1;
    }
  }
  for(in i=1;i<=m;++i){
    psm[i]+=psm[i-1];
    avl[i]+=psm[i];
    cout<<avl[i]<<"\n";
  }
  return 0;
}


Measured memory_bytes: 1664
using System;

class Circle_in_a_Rectangle
{
    public static void Main()
    {
        String[] str = Console.ReadLine().Split(' ');
        int w = int.Parse(str[0]);
        int h = int.Parse(str[1]);
        int x = int.Parse(str[2]);
        int y = int.Parse(str[3]);
        int r = int.Parse(str[4]);
        if ((0 <= x - r) && (x + r <= w) && (0 <= y - r) && (y + r <= h))
        {
            Console.WriteLine("Yes");
        }
        else
        {
            Console.WriteLine("No");
        }
    }
}


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

using namespace std;

long long int check(vector<vector<long long int>> &dp,vector<long long int> &v,int start,int end){
  if(start>end) return 0;
  if(start == end) return v[start];
  
  if(dp[start][end] != -1) return dp[start][end];
  
  long long int left = v[start] + min(check(dp,v,start+2,end),check(dp,v,start+1,end-1));
  long long int right = v[end] + min(check(dp,v,start+1,end-1),check(dp,v,start,end-2));
  dp[start][end] = max(left,right);
  
  return dp[start][end];
}

int main(){
  long long int x;
  int n;
  cin >> n;
  vector<long long int> v;
  long long int summ=0;
  for(int i=0;i<n;i++){
    cin >> x;
    summ = summ + x;
    v.push_back(x);
  }
  vector<vector<long long int>> dp(n,vector<long long int>(n,-1));
  long long int result = check(dp,v,0,n-1);
  long long int result1 = summ-result;
  cout << result-result1 << endl;
}

Measured memory_bytes: 73996
// #define _GLIBCXX_DEBUG
// #pragma GCC optimize("Ofast")
#pragma GCC diagnostic ignored "-Wsign-compare"
#include <bits/stdc++.h>
// #include <atcoder/all>
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
#define reps(i, s, n) for (int i = s; i < (int)(n); ++i)
using namespace std;
using ll = long long;
template <class T> using V = vector<T>;
template <class T> using VV = V<V<T>>;
template <class T> inline bool chmax(T& a, T b) {
    if (a < b) {
        a = b;
        return true;
    }
    return false;
}
template <class T> inline bool chmin(T& a, T b) {
    if (a > b) {
        a = b;
        return true;
    }
    return false;
}
constexpr int INF = 1e9 + 7;
constexpr ll LINF = 1LL << 60;

void solve() {
    int n, k;
    cin >> n >> k;
    V<int> D(10, 0);
    rep(i, k) {
        int d;
        cin >> d;
        D[d] = 1;
    }
    int ans = n;
    while (true) {
        int v = ans;
        bool ok = true;
        while (v > 0) {
            int d = v % 10;
            if (D[d]) ok = false;
            v /= 10;
        }
        if (ok) break;
        ans++;
    }
    cout << ans << "\n";
}
int main() {
    cin.tie(nullptr);
    // ios_base::sync_with_stdio(false);
    // cout << fixed << setprecision(15);
    solve();
}

Measured memory_bytes: 3612
#include<iostream>
#include<algorithm>
#include<map>
#include<queue>

using namespace std;

typedef pair<int, int> P;

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


int solve(int a[100][100],int w,int h){
	int d[100][100]={0};
	d[1][1]=1;

	P sp(1,1);
	queue<P> q;
	q.push(sp);

	while(!q.empty()){
		P p=q.front();
		q.pop();
		int x=p.first;
		int y=p.second;
		int cnt=d[y][x];

		if(x==w*2-1 && y==h*2-1){
			break;
		}

		for(int i=0;i<4;i++){
			int mx=x+dx[i];
			int my=y+dy[i];
			int nx=mx+dx[i];
			int ny=my+dy[i];

			if(nx<0 || ny<0 || nx>w*2 || ny>h*2){
				continue;
			}

			if(a[my][mx]==0 && d[ny][nx]==0){
				P np(nx,ny);
				q.push(np);
				d[ny][nx]=cnt+1;
			}
		}
	}
	return d[h*2-1][w*2-1];
}


int main(){
	int x,y;

	while(cin>>x>>y,x||y){
		
		int MazeWall[100][100]={0};
		
		for(int xr=0;xr<=x*2;xr++){
			MazeWall[0][xr]=MazeWall[y*2][xr]=1;
		}

		for(int yr=0;yr<=y*2;yr++){
			MazeWall[yr][0]=MazeWall[yr][x*2]=1;
		}
		
		
		for(int yr=0;yr<(y*2-1);yr++){
			if(yr%2==0){
				for(int xr=0;xr<x-1;xr++){
					//(x,y)=>(2xr+2,yr+1);
					cin>>MazeWall[yr+1][xr*2+2];
				}
			}
			else{
				for(int xr=0;xr<x;xr++){
					//(x,y)=>(2xr+1,yr+1);
					cin>>MazeWall[yr+1][xr*2+1];
				}
			}
		}
		cout<<solve(MazeWall,x,y)<<endl;
	}
}

Measured memory_bytes: 1296
while 1:
    r, a = map(int, input().split())
    if r == 0:
        break

    pro = list(str(r).zfill(4))
    ans = list(str(a).zfill(4))

    hit = 0
    blow = 0
    for p, a in zip(pro, ans):
        if p == a:
            hit += 1
        elif a in pro:
            blow += 1

    print(hit, blow)



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

struct Node{
  int key;
  struct Node *p;
  struct Node *l;
  struct Node *r;
};
typedef struct Node * Nodepointer;

Nodepointer root,nil;

void insert(int i){
  Nodepointer x = root;
  Nodepointer y = nil; /*parent*/
  Nodepointer z;

  z = (struct Node *)malloc(sizeof(struct Node));
  z->key = i;
  z->l = nil;
  z->r = nil;

  while(x != nil){
    y = x;
    if(x->key > z->key) x = x->l;
    else x = x->r;
  }

  z->p = y;

  if(y == nil) root = z;
  else{
    if(y->key > z->key) y->l = z;
    else y->r = z;
  }

}

void inorder(Nodepointer x){
    if(x == nil) return;
    inorder(x->l);
    printf(" %d",x->key);
    inorder(x->r);
}

void preorder(Nodepointer x){
    if(x == nil) return;
    printf(" %d",x->key);
    preorder(x->l);
    preorder(x->r);
}

Nodepointer find(Nodepointer x,int i){
  while(x != nil && i != x->key){
    if(x->key > i) x = x->l;
    else x = x->r;
  }
  return x;
}

int main(){
  int n,i,num;
  char c[20];

  scanf("%d",&n);

  for(i = 0;i < n;i++){
    scanf("%s",&c);
    if(c[0] == 'i'){
      scanf("%d",&num);
      insert(num);
    }
    else if(c[0] == 'p'){
      inorder(root);
      printf("\n");
      preorder(root);
      printf("\n");
    }
    else if(c[0] == 'f'){
      scanf("%d",&num);
      if(find(root,num) != nil) printf("yes\n");
      else printf("no\n");
    }
  }
  return 0;
}



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

#define COUT(x) cout << x << endl
#define CIN(x) cin >> x
#define FOR(i, a, b) for (int i = a; i < b; i++)
#define REP(i, n) FOR(i, 0, n)
#define EXIST(s, e) (s.find(e) != s.end())
#define SORT(c) sort(c.begin(), c.end())
#define ERASEUNIQE(v) v.erase(unique(v.begin(), v.end()), v.end())

// conversion
inline int toInt(string s)
{
    int v;
    istringstream sin(s);
    sin >> v;
    return v;
}
template <class T>
inline string toString(T x)
{
    ostringstream sout;
    sout << x;
    return sout.str();
}

// math
template <class T>
inline T sqr(T x) { return x * x; }

// typedef
typedef long long LL;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;

// container util
#define ALL(c) c.begin(), c.end()
#define RALL(c) c.rbegin(), c.rend()

// constant
const double EPS = 1e-10;
const double PI = acos(-1.0);

// debug
#define dump(x) cerr << #x << " = " << x << endl;
#define debug(x) cerr << #x << " = " << x << " (L" << __LINE__ << ")" \
                      << " " << __FILE__ << endl;

// clear memory
#define CLR(a) memset((a), 0, sizeof(a))

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

int main(int argc, char const *argv[])
{
    string n;
    CIN(n);

    for(int i = 0; i < n.size(); i++)
    {
        if(n.at(i) == '1') {
            n.at(i) = '9';
        }

        else if (n.at(i) == '9') {
            n.at(i) = '1';
        }

    }

    COUT(n);

    return 0;
}


Measured memory_bytes: 256
#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int maxn = 1e5 + 10;
const ll mod = 1e9 + 7;
ll dp[maxn][20];
int Mod[maxn][10];
void init()
{
    ll num = 1;
    for (int i = 1;i < maxn;i ++)
    {
        for (int j = 0;j < 10;j ++)
            Mod[i][j] = j * num % 13;
        num = num * 10 % 13;
    }
}
int main()
{
    ios::sync_with_stdio(false);
    init();
    string s;
    cin>>s;
    reverse(s.begin(),s.end());
    dp[0][0] = 1;
    for (int i = 0;i < s.size();i ++)
    {
        if (isdigit(s[i]))
        {
            for (int j = 0;j < 13;j ++)
                dp[i+1][(Mod[i+1][s[i]-'0']+j)%13]=dp[i][j];
        }
        else
            for (int j = 0;j < 10;j ++)
                for (int k = 0;k < 13;k ++)
                {
                    ll xx = (Mod[i+1][j]+k) % 13;
                    dp[i+1][xx] = (dp[i+1][xx] + dp[i][k]) % mod;
                }
    }
    cout<<dp[s.size()][5]<<'\n';
    return 0;
}

Measured memory_bytes: 20048
from collections import deque

H, W = [int(x) for x in input().split()]
cell = [list(input()) for i in range(H)]

painted = deque()
for h in range(H):
    for w in range(W):
        if cell[h][w] == '#':
            painted.append([h, w, 0])

ans = 0
while len(painted) > 0:
    blackcell = painted.popleft()
    for dy, dx in [[1, 0], [0, 1], [-1, 0], [0, -1]]:
        nh = blackcell[0] + dy
        nw = blackcell[1] + dx
        d = blackcell[2]
        if 0 <= nh < H and 0 <= nw < W and cell[nh][nw] == '.':
            cell[nh][nw] = '#'
            painted.append([nh, nw, d + 1])
        ans = d

print(ans)

Measured memory_bytes: 176092
//#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<long long> vll;
typedef vector<bool> vb;
#define pb push_back
#define fi first
#define se second
#define in insert
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define speed ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
const int mod = 1e9 + 7;
void solve(){
    int n, k;
    cin >> n >> k;
    if (n >= 10) cout << k;
    else cout << 100 * (10 - n) + k;    
}
int main(){
    speed;
    int t = 1;
    //cin >> t;
    for (int i = 1; i <= t; ++i) {
        solve();
    }
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
#define rep(var,cnt) for(int (var)=0; (var)<(int)(cnt); ++(var))
#define REP(var,init,cnt) for(int (var)=(init); (var)<(cnt); ++(var))
#define ran(var,vec) for(auto &(var):(vec))
#define all(v) (v).begin(),(v).end()
#define TF(flag) (flag)?1:0
using namespace std;

using ll = long long;
using ull = unsigned long long;
using pi = pair<int,int>;
using pl = pair<ll,ll>;

template<typename T>
using vec = vector<T>;
template<typename T>
using mat = vector<vec<T>>;
template<typename T>
using cub = vector<mat<T>>;
template<typename T>
using val = valarray<T>;

template<typename T>
using pq = priority_queue<T>;
template<typename T>
using rpq = priority_queue<T,vec<T>,greater<T>>;

template<typename T1,typename T2>
ostream &operator<<(ostream &os, const pair<T1,T2> &p){
  os<<"P("<<p.first<<", "<<p.second<<") ";
  return os;
}

template<typename T1,typename T2>
istream &operator>>(istream &is, pair<T1,T2> &p){
  is>>p.first>>p.second;
  return is;
}

template<typename T>
ostream &operator<<(ostream &os, const vector<T> &v){
  cout<<"V{";
  for(int i=0; i<(int)v.size(); ++i){
    os<<v[i]<<(i+1!=v.size()?" ":"");
  }
  cout<<"}";
  return os;
}

template<typename T>
istream &operator>>(istream &is, vector<T> &v){
  for(T &in:v) is>>in;
  return is;
}

template<typename T>
ostream &operator<<(ostream &os, const valarray<T> &v){
  cout<<"V{";
  for(int i=0; i<(int)v.size(); ++i){
    os<<v[i]<<(i+1!=v.size()?" ":"");
  }
  cout<<"}";
  return os;
}

template<typename T>
istream &operator>>(istream &is, valarray<T> &v){
  for(T &in:v) is>>in;
  return is;
}

// Usual Template End ================================================

// Template End ======================================================

int main(void){
  int N; cin>>N;
  string s; cin>>s;
  int open=0;
  int needopen=0;
  rep(i,N){
    if(s[i]==')'){
      if(open==0){
        ++needopen;
      }
      else{
        --open;
      }
    }
    else{
      ++open;
    }
  }
  rep(i,needopen) cout<<"(";
  cout<<s;
  rep(i,open) cout<<")";
}














Measured memory_bytes: 256
#include<algorithm>
#include<cmath>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<functional>
#include<iomanip>
#include<iostream>
#include<list>
#include<map>
#include<queue>
#include<random>
#include<set>
#include<stack>
#include<string>
#include<utility>
#include<vector>

#define FOR(i,a,b) for(long long i=(a);i<(b);++i)
#define rep(i,n)   FOR(i,0,n)
#define FORR(i,a,b) for(long long i=(a);i>=(b);--i)
#define REPR(i,n)   FORR(i,n,0)

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

ll h,w,n,sr,sc,cnt[2][4]={0};
string s[2];
bool f=true;

signed main() {
	cin>>h>>w>>n>>sr>>sc;
    cin>>s[0];
    cin>>s[1];
    /*
    for(int j=0;j<n;++j){
        for(int i=0;i<2;++i){
            if(s[i][j]=='U')cnt[i][0]++;
            else if(s[i][j]=='R')cnt[i][1]++;
            else if(s[i][j]=='D')cnt[i][2]++;
            else if(s[i][j]=='L')cnt[i][3]++;
            if(i==0){
                ll k,l;
                if(s[i][j]=='U'){
                    k=0;
                    l=sr;
                }
                else if(s[i][j]=='R'){
                    k=1;
                    l=w+1-sc;
                }
                else if(s[i][j]=='D'){
                    k=2;
                    l=h+1-sr;
                }
                else if(s[i][j]=='L'){
                    k=3;
                    l=sc;
                }
                if(k==0){
                    if(cnt[0][k]-cnt[1][(k+2)%4]>=l)f=false;
                }
            }
        }
    }
    */

   for(int i=0;i<4;++i){
       ll pos;
       if(i%2==0){
           pos=sr;
       }
       else{
           pos=sc;
       }

       for(int j=0;j<n;++j){
           if(i==0){
               if(s[0][j]=='U'){
                pos--;
                if(pos<=0)f=false;
               }
               if(s[1][j]=='D'){
                   pos++;
                   pos=min(pos,h);
               }
           }
            else if(i==1){
               if(s[0][j]=='R'){
                pos++;
                if(pos>w)f=false;
               }
               if(s[1][j]=='L'){
                   pos--;
                   pos=max(pos,1LL);
               }
           } 
           else if(i==2){
               if(s[0][j]=='D'){
                pos++;
                if(pos>h)f=false;
               }
               if(s[1][j]=='U'){
                   pos--;
                   pos=max(pos,1LL);
               }
           }
           else if(i==3){
               if(s[0][j]=='L'){
                pos--;
                if(pos<=0)f=false;
               }
               if(s[1][j]=='R'){
                   pos++;
                   pos=min(pos,w);
               }
           }     

       }
   }

    if(f){cout<<"YES"<<endl;}
    else{cout<<"NO"<<endl;}
	
	
	return 0;
}


Measured memory_bytes: 776
from collections import defaultdict


n = int(input())
Ss = list()
Ps = list()
p_dict = defaultdict(list)
for i in range(n):
    s, p = input().split()
    Ss.append(s)
    Ps.append(int(p))
    p_dict[s].append((int(p), i + 1))

s_unique = sorted(list(set(Ss)))
for s in s_unique:
    # sort by value
    pairs = sorted(p_dict[s], key=lambda e: e[0], reverse=True)
    for pair in pairs:
        print(pair[1])



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

int main() {
    int n, c[4]={};
    string t[4] = {"AC", "WA", "TLE", "RE"};
    string s;
    cin >> n;
    for (int i=0;i<n;++i){
        cin >> s;
        for (int j=0;j<4;++j){
            if (s==t[j]){
                c[j]+=1;
            }
        }
    }
    for(int j=0;j<4;++j){
        cout << t[j] << " x " << c[j] << endl;
    }
    return 0;
}

Measured memory_bytes: 3628
# -*- coding: utf-8 -*-
def inpl(): return list(map(int, input().split()))
N, M, C = inpl()
L = {i+1:v for i, v in enumerate(inpl())}

balls = []
for _ in range(N):
    c, w = inpl()
    balls.append([w, c])

balls = sorted(balls, reverse=True)

i = 0
count = 0
ans = 0
for i in range(N):
    w, c = balls[i]
    if count == M:
        break
    if L[c] == 0:
        continue
    else:
        count += 1
        ans += w
        L[c] -= 1

print(ans)


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

using namespace std;
using namespace __gnu_pbds;
 
typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> pii;
template <class T> using Tree = tree<T, null_type, less<T>, rb_tree_tag,tree_order_statistics_node_update>;

#define FOR(i, a, b) for (int i=a; i<(b); i++)
#define F0R(i, a) for (int i=0; i<(a); i++)
#define FORd(i,a,b) for (int i = (b)-1; i >= a; i--)
#define F0Rd(i,a) for (int i = (a)-1; i >= 0; i--)

#define sz(x) (int)(x).size()
#define mp make_pair
#define pb push_back
#define f first
#define s second
#define lb lower_bound
#define ub upper_bound

const int MOD = 1000000007;

int N,K, A[100000];
ll B[100000], ans;

void test(int pos) {
    if (pos == -1) {
        ll cur = 0;
        F0R(i,N) {
            bool f = 1;
            FOR(j,pos+1,30) if ((A[i]&(1<<j)) && !(K&(1<<j))) f = 0;
            if (f) cur += B[i];
        }
        ans = max(ans,cur);
    } else {
        ll cur = 0;
        F0R(i,N) {
            bool f = 1;
            FOR(j,pos+1,30) if ((A[i]&(1<<j)) && !(K&(1<<j))) f = 0;
            if (A[i]&(1<<pos)) f = 0;
            if (f) cur += B[i];
        }
        ans = max(ans,cur);
    }
}

int main() {
	ios_base::sync_with_stdio(0);cin.tie(0);
	cin >> N >> K;
	F0R(i,N) cin >> A[i] >> B[i];
	F0Rd(i,30) if (K&(1<<i)) test(i);
	test(-1);
	cout << ans;
}

// read!
// ll vs. int!

Measured memory_bytes: 1408
#pragma GCC optimize(2)
#include<bits/stdc++.h>
#define LL long long
#define int LL
const LL inf=0x3f3f3f3f;
using namespace std;
template<typename tp> inline void read(tp &x)
{
	x=0; char c=getchar(); bool f=0;
	for(;c<'0'||c>'9';f|=(c=='-'),c = getchar());
	for(;c>='0'&&c<='9';x=(x<<3)+(x<<1)+c-'0',c = getchar());
	if(f) x=-x;
}
int n,ans;
int s[]={105,135,165,189,195};
signed main()
{
	read(n);
	while(n>=s[ans]) ans++;
	printf("%lld\n",ans);
	return 0;
}

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

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

using namespace std;

int main(){
  int n,a,b;
  cin>>n>>a>>b;
  if(n*a>b){
    cout<<b;
  }else{
    cout<<n*a;
  }
  return 0;
}


Measured memory_bytes: 256
import sys
sys.setrecursionlimit(10 ** 7)
read = sys.stdin.buffer.read 
inp = sys.stdin.buffer.readline

import numpy as np
def resolve():
    K, Q = map(int, inp().split())
    D = np.array(inp().split(), dtype=np.int64)
    nxm = [list(map(int, inp().split())) for _ in range(Q)]

    for n, x, m in nxm:
        # 最初にmの余りをとっても変わらない
        # 与えられた要素をすべてmod mにとりあえずする
        E = D % m
        acc_E = np.cumsum(E)

        # 数列がN項であるということは、末項は数列dをN-1回足したものである
        loop, rem = divmod(n - 1, K)
        tot = x % m + acc_E[-1] * loop

        # Bi = Bi+1 になっている個所：0の個数
        zero_acc = np.cumsum(E == 0).astype(np.int64)
        tot_zero = zero_acc[-1] * loop

        if rem > 0:
            tot += acc_E[rem-1]
            tot_zero += zero_acc[rem - 1]
        dame = tot // m
        print(n - 1 - dame - tot_zero)

if __name__ == "__main__":
    resolve()

Measured memory_bytes: 28248
#pragma region head
    #include <bits/stdc++.h>
    using namespace std;
    using ll = long long;
    using vi = vector<int>;
    using vll = vector<ll>;
    using pi = pair<int, int>;
    using pll = pair<ll,ll>;
    template <class T> using vv = vector<vector<T>>;
    #define rep(i,n) for(int i = 0; i < (int)(n); i++)
    #define repi(i,a,b) for(int i = (int)(a); i < (int)(b); i++)
    #define rrep(i,n) for(int i = (int)(n)-1; i >= 0; i--)
    #define rrepi(i,a,b) for(int i = (int)(b)-1; i >= (int)(a); i--)
    const int INF = 1002003004;
    const ll LINF = 1002003004005006007ll;
    struct preprocess{preprocess()
    {cin.tie(nullptr);ios::sync_with_stdio(false);cout<<fixed<<setprecision(20);}}____;
#pragma endregion head

#pragma region library

#pragma endregion library
vi dp;
vector<tuple<int, int, int>> history;
vi ans;
vi a;
void dfs(vv<int>& g, int u,int p=-1){
    auto it = lower_bound(dp.begin(), dp.end(),a[u]);
    int idx = distance(dp.begin(),it);
    int prv = dp[idx];
    dp[idx]=a[u];
    history.emplace_back(idx,prv,a[u]);
    it = lower_bound(dp.begin(), dp.end(),INF);
    idx = distance(dp.begin(),it);
    ans[u]=idx;
    for(auto v:g[u]){
        if(v == p) continue;
        dfs(g,v,u);
        int idxl,prvl;
        tie(idxl,prvl,ignore)=history.back();
        history.pop_back();
        dp[idxl]=prvl;
    }
}

int main(){
    int n;cin >> n;
    dp.assign(n,INF); ans.resize(n); a.resize(n);
    rep(i,n) cin >> a[i];
    vv<int> to(n);
    rep(i,n-1){
        int u,v;cin >> u >> v;
        u--; v--;
        to[u].emplace_back(v);
        to[v].emplace_back(u);
    }
    dfs(to,0);
    rep(i,n){
        cout << ans[i] << '\n';
        
    }

}

Measured memory_bytes: 38912
n,m = map(int,input().split())
if m>n:
    n,m = m,n
mod = 10**9+7
ans = 1
if n>m+1:
    print(0)
    exit()
if n==m:
    ans = 2
for i in range(1,n+1):
    ans = (ans*i)%mod
for i in range(1,m+1):
    ans = (ans*i)%mod
print(ans)


Measured memory_bytes: 3060
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
// Welcome to my source code!

int f(int x) {
    int t = x, d = 1;
    while (t / 10) {
        t /= 10;
        d++;
    }
    return d;
}

int main() {
    int n;
    string s;
    cin >> n >> s;
    int ans = 0;
    for (int i = 0; i <= 999; i++) {
        string c;
        if (f(i) == 1) c += "00";
        else if (f(i) == 2) c += '0';
        c += to_string(i);
        int k = 0;
        for (int j = 0; j < n; j++) {
            if (s[j] == c[k]) k++;
        }
        if (k == c.size()) ans++;
    }
    cout << ans << endl;
}

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

string s;
bool bad=false;
int main(){
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);

	cin>>s;
	for(int i=1;i<4;i++){
		if(s[i]==s[i-1])
			bad=true;
	}
	if(bad)
		cout<<"Bad";
	else
		cout<<"Good";
	return 0;
}

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

int main() {
    int n, m;
    cin >> n >> m;
    vector<int> cnt(m, 0);
    for (int i = 0; i < n; i++) {
        int K;
        cin >> K;
        for (int j = 0; j < K; j++) {
            int A;
            cin >> A;
            A--;
            cnt[A]++;
            
        }
    }
    int ans = 0;
    
    for (int i = 0; i < m; i++) {
        if (cnt[i] == n) {
            ans++;
        }
    }
    
    cout << ans << endl;
    return 0;
}


Measured memory_bytes: 256
#include <iostream>
#include<algorithm>
#include<string>
#include <cmath>
#include <vector>
#include <map>
#include <cstdio>
#pragma region Macros
#define rep(i,n) for(int i=0;i<n;i++)
#define REP(i,n) for(int i=1;i<=n;i++)
#define int long long
#define all(x) (x).begin(),(x).end()
#define swap(a,b) (a += b,b = a - b,a -= b)
#define ceil(a,b) ((a+b-1)/b)
int gcd(int a, int b) // assuming a,b >= 1
{
    if (a < b)
        swap(a, b);
    if (b == 0)
        return a;
    if (a % b == 0)
        return b;
    return gcd(b, a % b);
}
int lcm(int a, int b) // assuming a,b >= 1
{
    return  a * b / gcd(a, b);
}
using namespace std;
#pragma endregion
signed main() {
    int N,i;
    cin >> N >> i;
    if (N % i == 0)
        cout << 0;
    else
        cout << 1;
}

Measured memory_bytes: 256
#include <vector>
#include <list>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <ctime>
#include <climits>

#define int long long
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define ALL(a) (a).begin(), (a).end()
#define SZ(a) (signed)((a).size())
#define EACH(i, c) for (typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define EXIST(s, e) ((s).find(e) != (s).end())
#define SORT(c) sort((c).begin(), (c).end())

using namespace std;

typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;

const int MOD = 1000000007;

#define dump(x) cerr << #x << " = " << (x) << endl;
#define debug(x) cerr << #x << " = " << (x) << " (L" << __LINE__ << ")"  << " " << __FILE__ << endl;

signed main(void) {
    ios::sync_with_stdio(false);
    cout.setf(ios::fixed, ios::floatfield);
    cout.precision(10);
    cin.tie(0);

    VS s(3);
    REP(i, 3) {
        cin >> s[i];
    }

    REP(i, 3) {
        printf("%c", s[i][0] + 'A' - 'a');
    }
    cout << endl;

    return 0;
}


Measured memory_bytes: 256
#include<bits/stdc++.h>
#define watch(x) cout << (#x) << " is " << (x) << endl
typedef long long ll;
using namespace std;
int static fast = [](){
  ios::sync_with_stdio(false);
  cin.tie(0); cout.tie(0); return 0;
}();

int main() {
    int H, W, N;
    int sr, sc;
    string S, T;
    cin >> H >> W >> N;
    cin >> sr >> sc;
    cin >> S >> T;
    // LRUD

    vector<int> dis = {sc, W+1-sc, sr, H+1-sr};
    vector<int> exp_LR = {0, 0};
    vector<int> exp_UD = {0, 0};
    for(int i = 0; i < N; i++) {
        if (S[i] == 'R')
            exp_LR[1] += 1;
        else if (S[i] == 'L')
            exp_LR[0] += 1;

        if (S[i] == 'D')
            exp_UD[1] += 1;
        else if (S[i] == 'U')
            exp_UD[0] += 1;

        if (exp_LR[1] >= dis[1] || exp_LR[0] >= sc || exp_UD[1] >= dis[3] || exp_UD[0] >= sr) {
            cout << "NO" << endl;
            return 0;
        }


        if (T[i] == 'L')
            exp_LR[1] = max(exp_LR[1]-1, -(sc-1));
        else if (T[i] == 'R')
            exp_LR[0] = max(exp_LR[0]-1, -(W-sc));

        if (T[i] == 'U')
            exp_UD[1] = max(exp_UD[1]-1, -(sr-1));
        else if (T[i] == 'D')
            exp_UD[0] = max(exp_UD[0]-1, -(H-sr));


    }
    cout << "YES" << endl;
    return 0;
}



Measured memory_bytes: 920
import sys
import bisect
all_flg = False
n, k = [int(i) for i in sys.stdin.readline().split()]
d_ls = [int(i) for i in sys.stdin.readline().split()]
candidate = [i for i in range(10) if i not in d_ls]
set_candidate = set(candidate)
len_cand = len(candidate)
last_ans = n
while True:
    ans = list(str(last_ans))
    flg = False
    flg_ls = [0 for i in range(len(str(n)))]
    for i, s in enumerate(str(last_ans)):
        s = int(s)
        res = bisect.bisect_left(candidate, s)
        if flg:
            ans[i] = str(candidate[0])
        else:
            if res >= len_cand:
                flg_ls[i] = 1
            ans[i] = str(candidate[res if res < len_cand else 0])
        if s not in set_candidate:
            flg = True
    ans = int("".join(ans))
    hoge = int("".join([str(i) for i in flg_ls]) + "0")
    ans += hoge
    if ans == last_ans:
        break
    last_ans = ans
print(ans)

Measured memory_bytes: 38256
/**
*    created: 24.06.2020 03:29:44
**/
#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
#define int long long
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep2(i, s, n) for (int i = (s); i < (int)(n); i++)
#define all(x) (x).begin(),(x).end()
#define pcnt(bit) __builtin_popcount(bit)
#define mod 1000000007
#define pi acos(-1.0)
using namespace std;

signed main() {

    cin.tie(0);
    ios_base::sync_with_stdio(false);

    int n;
    cin >> n;
    int k = to_string(n).size();
    vector<int> tmp;
    vector<int> al;
    int cnt = 0;
    tmp.push_back(3);
    tmp.push_back(5);
    tmp.push_back(7);
    al = tmp;
    rep(j,k-1) {
        vector<int> swp;
        for (int i : tmp) {
            swp.push_back(10*i+3);
            swp.push_back(10*i+5);
            swp.push_back(10*i+7);
            al.push_back(10*i+3);
            al.push_back(10*i+5);
            al.push_back(10*i+7);
        }
        swap(tmp,swp);
    }
    auto ok = [&] (int i) {
        string s = to_string(i);
        int l = s.size();
        int cnt3 = 0, cnt5 = 0, cnt7 = 0;
        rep(j,l) {
            if (s[j] == '3') cnt3++;
            if (s[j] == '5') cnt5++;
            if (s[j] == '7') cnt7++;
        }
        if (cnt3 == 0 || cnt5 == 0 || cnt7 == 0) return false;
        else return true;
    };
    int t = upper_bound(all(al),n) - al.begin();
    rep(i,t) {
        if (ok(al[i])) {
            cnt++;
        }
    }
    cout << cnt << endl;
    return 0;
}

Measured memory_bytes: 4048
#include <iostream>

using namespace std;
typedef long long ll;
ll a[100005];
int main() {
  ll k;
  cin >> k;
  for(int i = 0; i < k; i++) cin >> a[i];

  if(a[k-1] != 2) {
    cout << -1 << endl;
    return 0;
  }
  
  ll min_ans = a[k-1];
  ll max_ans = 2 * a[k-1] - 1;

  for(int i = k - 2; i >= 0; i--) {
    min_ans = (min_ans + a[i] - 1) / a[i];
    max_ans = max_ans / a[i];
    if(max_ans == 0 || min_ans > max_ans) {
      cout << -1 << endl;
      return 0;
    }
    min_ans *= a[i];
    max_ans  = max_ans * a[i] + (a[i] - 1);
    // cout << min_ans <<"   "<<max_ans << endl;
  }

  cout << min_ans <<" "<< max_ans << endl;
}
    


Measured memory_bytes: 1024
import java.util.*;
 
public class Main {
  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    int a = sc.nextInt();
    int b = sc.nextInt();
    int sum = 1;
    int count = 1; 
    
    for(int i = 0; sum < b; i++) {
      sum = sum + a - 1;
      count++;
    }
    System.out.println(count - 1);
  }
}

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

public class Main {

	
	public static void main(String[] args) throws IOException {
		Scanner sc = new Scanner();
		PrintWriter out = new PrintWriter(System.out);
		out.println(Math.min(sc.nextInt()*sc.nextInt(),sc.nextInt()));
		out.close();

	}
	
	static class Scanner {
		BufferedReader br;
		StringTokenizer st;

		Scanner() {
			br = new BufferedReader(new InputStreamReader(System.in));
		}

		Scanner(String fileName) throws FileNotFoundException {
			br = new BufferedReader(new FileReader(fileName));
		}

		String next() throws IOException {
			while (st == null || !st.hasMoreTokens())
				st = new StringTokenizer(br.readLine());
			return st.nextToken();
		}

		String nextLine() throws IOException {
			return br.readLine();
		}

		int nextInt() throws IOException {
			return Integer.parseInt(next());
		}

		long nextLong() throws NumberFormatException, IOException {
			return Long.parseLong(next());
		}

		double nextDouble() throws NumberFormatException, IOException {
			return Double.parseDouble(next());
		}

		boolean ready() throws IOException {
			return br.ready();
		}

	}

}

Measured memory_bytes: 21204
#include<iostream>
int main() {
	int a = 0, b = 0, c = 0, d = 0;
	std::cin >> a >> b >> c >> d;
	int n = a + b + c + d;
	std::cout << n/60 << std::endl;
	std::cout << n%60 << std::endl;
	return 0;
}

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

int main() {
  int n;
  cin >> n;
  vector<int> a(n);
  vector<int> order(n);
  for(int i = 0; i < n; i++) {
    cin >> a.at(i);
    order.at(a.at(i) - 1) = i + 1;
  }
  for(int i = 0; i < n; i++) {
    cout << order.at(i) << " ";
  }
  cout << endl;
}

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

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

long long N, K;
long long a[200001];
long long visited[200001] = {0};
vector<long long> cit(0);

void solve() {
  cit.push_back(1);
  visited[1] = 1;
  long long i = 1;
  long long key;
  while(true) {
    if(visited[a[i]] == 0) {
      cit.push_back(a[i]);
      visited[a[i]] = 1;
    } else {
      cit.push_back(a[i]);
      key = a[i];
      break;
    }
    i = a[i];
  }

  long long start;

  for(long long i = 0; i < cit.size(); i++) {
    if(cit[i] == key) {
      start = i;
      break;
    }
  }

  long long cycle = cit.size() - 1 - start;

  long long cyc[cycle];

  for(long long i = 0; i < cycle; i++) {
    cyc[i] = cit[start+i];
  }

  if(K >= start) {
    long long ind = (K + 1 - start) % cycle;
    long long place;

    if(ind != 0) place = cyc[ind-1];
    if(ind == 0) place = cyc[cycle-1];


    cout << place << endl;
  }

  else {
    cout << cit[K] << endl;
}
}

int main() {
  cin >> N >> K;
  for(int i = 1; i <= N; i++) {
    cin >> a[i];
  }
  solve();
  return 0;
}

Measured memory_bytes: 9280
#include <bits/stdc++.h>
using namespace std;
 
int main(){
  int a,b,x,y,z,max;
  
  cin >>a >>b ;
  
  x = a+b;
  y=a-b;
  z=a*b;
  max=x;
  
  if(max<y){
    max=y;
  }
  if(max<z){
    max=z;
  }
	
  cout << max << endl;
 
}

Measured memory_bytes: 256
#include "bits/stdc++.h"
using namespace std;
#define rep(i,n)for(int i=0;i<(int)(n);i++)
#define PI 3.141592653589793
//vector < vector<int>>a(0, vector<int>(0));二次元配列宣言


int main()
{
	int a, b, x;
	cin >> x >> a >> b;
	if (a>=b)
		cout << "delicious" << endl;
	else if (b - a <= x)
		cout << "safe" << endl;
	else
		cout << "dangerous" << endl;

} 

Measured memory_bytes: 3600
#ifdef DEBUG
#define _GLIBCXX_DEBUG
#endif
//#pragma GCC optimize("O3")
#include <bits/stdc++.h>
using namespace std;
typedef long double ld;
typedef long long ll;
int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
//    freopen("input.txt", "r", stdin);
    string s;
    cin >> s;
    for (char& c : s) {
        if (c == '?') c = 'D';
        cout << c;
    }
    return 0;
}

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

int main() {
  int X, N;
  cin >> X >> N;
  vector<int> P(N);
  for (int i = 0; i < N; i++) cin >> P.at(i);
  set<int> SE;
  for (auto p : P) SE.insert(p);

  int mi = 1e9;
  int ans;
  for (int i = X - 200; i < X + 200; i++) {
    if (SE.count(i)) continue;
    int tmp = abs(i - X);
    if (tmp < mi) mi = tmp, ans = i;
  }
  cout << ans << "\n";
}

Measured memory_bytes: 3636
#include <iostream>
#include <vector>
#include <queue>
using namespace std;

int main() {
    int N, M;
    cin >> N >> M;
    vector<int> jobs[M];
    for (int i = 0; i < N; i++) {
        int a, b;
        cin >> a >> b;
        if (a > M) continue;
        jobs[M - a].push_back(b);
    }

    priority_queue<int> que;
    long long res = 0;
    for (int i = M - 1; i >= 0; i--) {
        for (int j = 0; j < jobs[i].size(); j++) {
            int t = jobs[i][j];
            que.push(t);
        }
        if (!que.empty()) {
            res += que.top();
            que.pop();
        }
    }
    cout << res << endl;
    return 0;
}

Measured memory_bytes: 5632
#include<iostream>

using namespace std;

int N,A,B,C,X;

void solve() {

    int cnt=0;
    for(int i=0;i<N;i++) {
        int y;
        cin>>y;
        if(cnt==-1) continue;
        while(true) {
            if(y==X) {
                X=(A*X+B)%C;
                cnt++;
                break;
            }
            X=(A*X+B)%C;
            cnt++;
            if(10000<cnt) {
                cnt=-1;
                break;
            }
        }

    }
    if(cnt!=-1) cnt--;
    cout<<cnt<<endl;

}

int main() {
    while(cin>>N>>A>>B>>C>>X) {
        if(N+A+B+C+X==0) return 0;
        solve();
    }

    return 0;
}

Measured memory_bytes: 1160
#include<bits/stdc++.h>
#define rep(i,n) for(int i=0;i<(int)n;i++)
#define all(c) (c).begin(),(c).end()
#define pb push_back
#define dbg(...) do{cerr<<__LINE__<<": ";dbgprint(#__VA_ARGS__, __VA_ARGS__);}while(0);

using namespace std;

namespace std{template<class S,class T>struct hash<pair<S,T>>{size_t operator()(const pair<S,T>&p)const{return ((size_t)1e9+7)*hash<S>()(p.first)+hash<T>()(p.second);}};template<class T>struct hash<vector<T>>{size_t operator()(const vector<T> &v)const{size_t h=0;for(auto i : v)h=h*((size_t)1e9+7)+hash<T>()(i)+1;return h;}};}
template<class T>ostream& operator<<(ostream &os, const vector<T> &v){os<<"[ ";rep(i,v.size())os<<v[i]<<(i==v.size()-1?" ]":", ");return os;}template<class T>ostream& operator<<(ostream &os,const set<T> &v){os<<"{ "; for(const auto &i:v)os<<i<<", ";return os<<"}";}
template<class T,class U>ostream& operator<<(ostream &os,const map<T,U> &v){os<<"{";for(const auto &i:v)os<<" "<<i.first<<": "<<i.second<<",";return os<<"}";}template<class T,class U>ostream& operator<<(ostream &os,const pair<T,U> &p){return os<<"("<<p.first<<", "<<p.second<<")";}
void dbgprint(const string &fmt){cerr<<endl;}template<class H,class... T>void dbgprint(const string &fmt,const H &h,const T&... r){cerr<<fmt.substr(0,fmt.find(","))<<"= "<<h<<" ";dbgprint(fmt.substr(fmt.find(",")+1),r...);}
typedef long long ll;typedef vector<int> vi;typedef pair<int,int> pi;const int inf = (int)1e9;const double INF = 1e12, EPS = 1e-9;

const int mod = 1e9 + 7;

int main(){
	cin.tie(0); cin.sync_with_stdio(0);
	int n;
	vector<string> in(2);
	cin >> n;
	rep(i, 2) cin >> in[i];
	
	int sz = 0;
	vector<vi> e(n);
	map<char, int> id;
	rep(i, n) rep(j, 2) if(!id.count(in[j][i])) id[in[j][i]] = sz++;
	rep(i, n) rep(j, 2) rep(k, 2){
		int y = i + k, x = j + 1 - k;
		if(y < n && x < 2){
			int a = id[in[x][y]], b = id[in[j][i]];
			e[a].pb(b);
			e[b].pb(a);
		}
	}
	//dbg(id);
	
	map<map<int,int>, int> dp[60];
	dp[0][{}] = 1;
	rep(i, n) for(const auto &p : dp[i]){
		
		rep(c, 3){
			bool ok = 1;
			for(int j : e[i]) if(j < i && p.first.count(j) && p.first.at(j) == c){
				ok = 0;
				break;
			}
			if(!ok) continue;
			map<int,int> m = p.first;
			m[i] = c;
			while(m.size() > 4) m.erase(m.begin());
			
			(dp[i + 1][m] += p.second) %= mod;
		}
	}
	ll ans = 0;
	for(const auto &p : dp[n]) ans += p.second;
	cout << ans % mod << endl;
	
	return 0;
}

Measured memory_bytes: 512
N=int(input())
Ins=[[] for _ in range(N)]
ans=0

#bit演算用にx-1
for i in range(N):
  A=int(input())
  for _ in range(A):
    x,y=map(int,input().split())
    Ins[i].append((x-1,y))

for i in range(2**N):
  flag=1
  honest=[]
  unkind=[]
  for j in range(N):
    if (i>>j)&1:
      honest.append(j)
    else:
      unkind.append(j)
  for k in honest:
    for l in Ins[k]:
      if l[1]==1 and l[0] in unkind:
        flag=0
        break
      elif l[1]==0 and l[0] in honest:
        flag=0
        break
  if flag==1:
    ans=max(ans,len(honest))
print(ans)

Measured memory_bytes: 3064
package main

import (
	"bufio"
	"container/heap"
	"fmt"
	"os"
	"strconv"
)

var sc, wr = bufio.NewScanner(os.Stdin), bufio.NewWriter(os.Stdout)

func scanString() string   { sc.Scan(); return sc.Text() }
func scanRunes() []rune    { return []rune(scanString()) }
func scanInt() int         { a, _ := strconv.Atoi(scanString()); return a }
func scanInt64() int64     { a, _ := strconv.ParseInt(scanString(), 10, 64); return a }
func scanFloat64() float64 { a, _ := strconv.ParseFloat(scanString(), 64); return a }

func scanInts(n int) []int {
	res := make([]int, n)
	for i := 0; i < n; i++ {
		res[i] = scanInt()
	}
	return res
}

func debug(a ...interface{}) { fmt.Fprintln(os.Stderr, a...) }

func abs(a int) int {
	if a < 0 {
		return -a
	}
	return a
}
func min(a, b int) int {
	if a < b {
		return a
	}
	return b
}
func max(a, b int) int {
	if a > b {
		return a
	}
	return b
}

//•*¨*•.¸¸♪main•*¨*•.¸¸♪(　-ω-)ノ　(　・ω・)
func main() {
	defer wr.Flush()
	sc.Split(bufio.ScanWords)
	sc.Buffer(make([]byte, 10000), 1001001)

	h, w, k := scanInt(), scanInt(), scanInt()
	x1, y1, x2, y2 := scanInt()-1, scanInt()-1, scanInt()-1, scanInt()-1
	c := make([]string, h)
	done := make([][][4]bool, h)
	d := make([][][4]int, h)

	for i := 0; i < h; i++ {
		c[i] = scanString()
		done[i] = make([][4]bool, w)
		d[i] = make([][4]int, w)
		for j := 0; j < w; j++ {
			for l := 0; l < 4; l++ {
				d[i][j][l] = inf
			}
		}
	}
	for i := 0; i < 4; i++ {
		d[x1][y1][i] = 0
	}

	pq := &pqAsc{pair{x1, y1, 0, 0}, pair{x1, y1, 1, 0}, pair{x1, y1, 2, 0}, pair{x1, y1, 3, 0}}
	heap.Init(pq)
	dx, dy := []int{0, -1, 0, 1}, []int{1, 0, -1, 0}

	for len(*pq) != 0 {
		p := heap.Pop(pq).(pair)
		if p.co > d[p.x][p.y][p.d] {
			continue
		}

		for i := 0; i < 4; i++ {
			nx, ny := p.x+dx[i], p.y+dy[i]
			if nx < 0 || ny < 0 || nx >= h || ny >= w || c[nx][ny] == '@' {
				continue
			}
			var co int
			if p.d == i {
				co = p.co + 1
			} else {
				co = (p.co+k-1)/k*k + 1
			}
			if d[nx][ny][i] > co {
				d[nx][ny][i] = co
				heap.Push(pq, pair{nx, ny, i, co})
			}
		}
	}

	ans := inf
	for i := 0; i < 4; i++ {
		ans = min(ans, d[x2][y2][i])
	}
	if ans == inf {
		fmt.Fprintln(wr, -1)
	} else {
		fmt.Fprintln(wr, (ans+k-1)/k)
	}

}

type pair struct{ x, y, d, co int }

const inf = 1<<63 - 1

type pqAsc []pair

func (pq pqAsc) Len() int            { return len(pq) }
func (pq pqAsc) Swap(i, j int)       { pq[i], pq[j] = pq[j], pq[i] }
func (pq pqAsc) Less(i, j int) bool  { return pq[i].co < pq[j].co }
func (pq *pqAsc) Push(x interface{}) { *pq = append(*pq, x.(pair)) }
func (pq *pqAsc) Pop() interface{} {
	x := (*pq)[len(*pq)-1]
	*pq = (*pq)[0 : len(*pq)-1]
	return x
}


Measured memory_bytes: 96480
#include <bits/stdc++.h>
#define boost ios_base::sync_with_stdio(0); cin.tie(); cout.tie()
#define all(x) x.begin(), x.end()
#define endl '\n'
#define len(a) a.length()
#define TIME cout << endl << "Time execute: " << clock() / (double)CLOCKS_PER_SEC << " sec" << endl;

using namespace std;

const int inf = 1e9;
const double eps = 1e-1;
const int mod = 1e9 + 7;

typedef long long ll;

bool check_pow_2 (ll n) {return (n & (n - 1) == 0);}
bool is_prime (int a) { for (int i = 2; i <= sqrt(a); i++) if (a % i == 0) return false; return true; }
ll div_up (ll x, ll y) { if (x == 0) return 0; return (x - 1) / y + 1; }

int32_t main()
{
    boost;
    int a, b, c; cin >> a >> b >> c;
    cout << c << " " << a << " " << b;
    return 0;
}
/*
*/


Measured memory_bytes: 3544
#include <bits/stdc++.h>
using namespace std;
#define Hello ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define ll long long
#define endl '\n'
int n, m, dp[1005][1 << 12];
vector<pair<int, int>>v;
int solve(int idx, int mask){
    if(__builtin_popcount(mask) == n)
        return 0;
    else if(idx >= m)
        return 1e9;
    if(dp[idx][mask] != -1)
        return dp[idx][mask];
    int ans = 1e9;
    ans = min(ans, solve(idx + 1, mask));
    ans = min(ans, solve(idx + 1, mask | v[idx].second) + v[idx].first);
    dp[idx][mask] = ans;
    return ans;
}
int main()
{
    Hello
    memset(dp, -1, sizeof dp);
    cin >> n >> m;
    for(int i = 0; i < m; i++){
        int cost, elements, bitmask = 0;
        cin >> cost >> elements;
        for(int j = 0; j < elements; j++){
            int numb;
            cin >> numb;
            bitmask = bitmask | (1 << (numb - 1));
        }
        v.push_back({cost, bitmask});
    }
    int ans = solve(0, 0);
    if(ans >= 1e9)
        cout << -1;
    else
        cout << ans;
    return 0;
}

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

const int INF = 1e8;

int main(){ 
  int N, M;
  vector<int> C(50010, 0);

  cin >> N >> M;
  for(int i=1; i<=M; i++) cin >> C[i];

  //dp[i][j]: i番目のコインまででj円支払うときの最小枚数
  vector<vector<int>> dp(21, vector<int>(50010, INF));
  dp[0][0] = 0;

  for(int i=0; i<M; i++){
    for(int j=0; j<=N; j++){
      if(j == 0) dp[i+1][j] = 0;

      if(j >= C[i+1]){
        dp[i+1][j] = min(dp[i][j], dp[i+1][j-C[i+1]] + 1);
      } else {
        dp[i+1][j] = dp[i][j];
      }
    }
  }

  cout << dp[M][N] << endl;
}


Measured memory_bytes: 7372
#include <iostream>
#include <utility>
#include <algorithm>
#include <vector>
#include <string>
using namespace std;

int main() {
	int n;
	while(cin >> n, n) {
		vector<pair<pair<int,int>, pair<int,string> > > v;
		for(int i = 0; i < n; i++) {
			string s;
			int win = 0;
			int lose = 0;
			cin >> s;
			for(int j = 0; j < n - 1; j++) {
				int t;
				cin >> t;
				if(t == 0)
					win--;
				else if(t == 1)
					lose++;
			}
			v.push_back(pair<pair<int,int>,pair<int,string> >(pair<int,int>(win, lose), pair<int,string>(i,s)));
		}
		sort(v.begin(), v.end());
		for(int i = 0; i < v.size(); i++) {
			cout << v[i].second.second << endl;
		}
	}
}

Measured memory_bytes: 1208
N,M = map(int,input().split())
left,right = 0,N
for i in range(M):
    L,R = map(int,input().split())
    if(left<L):
        left = L
    if(R<right):
        right = R
if(left<=right):
    print(right-left+1)
else:
    print(0)

Measured memory_bytes: 3060