akhauriyash/Code-Regression huggingface dataset:
#include <bits/stdc++.h>
using namespace std;
#include <boost/foreach.hpp>
typedef std::map<std::string, int> container;
typedef std::pair<std::string, int> value_type;

struct second_order {
  bool operator()(const value_type& x, const value_type& y) const {
    return x.second > y.second;
  }
};

int main() {
  int N;
  cin>>N;
  container src;
  vector<string> a(N);
  for(int i=0;i<N;i++) cin>>a[i];


  for(int i=0;i<N;i++){
    src[a[i]]--;
  }
  priority_queue<value_type, vector<value_type>, second_order> que;
  pair<string, int> p;
  BOOST_FOREACH(p, src) {
    que.push(p);
  }
  vector<string> d;
  int value= que.top().second;
  while ( !que.empty() ) {
    value_type item = que.top();
    if(value ==item.second) d.push_back(item.first);
    else break;
    que.pop();
  }

  sort(d.begin(),d.end());
  for(int i=0;i<d.size();i++){
    cout<<d[i]<<endl;
  }
}


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

#define SPEED ios::sync_with_stdio(false); cin.tie(0); cout.tie(0)
#define read()          freopen("fibsubseq.in","r",stdin)
#define write()         freopen("fibsubseq.out","w",stdout)
#define sf(n)           scanf("%d",&n)
#define sfd(n)          scanf("%lf",&n)
#define sl(x)           scanf("%I64d",&x)
#define sfl(n)          scanf("%lld", &n)
#define sfc(n)          scanf(" %c",&n)
#define sful(n)         scanf("%llu",&n)
#define ull             unsigned long long int
#define endll           printf("\n")
#define pf              printf
#define pi              acos(-1.0)
#define eps             1e-10
#define mem(a,b)        memset(a,b,sizeof(a))
#define pb              push_back
#define xx              first
#define yy              second
#define pii             pair<int, int>
#define pll             pair<ll, ll>
#define MP make_pair
#define ll              long long
#define uniq(a)      a.erase(unique(a.begin(), a.end()), a.end())

bool Check(unsigned int  N,int pos){return (bool)(N & (1LL<<pos));}
ll  Set(unsigned int N,int pos){    return N=N | (1LL<<pos);}
#define cnd tree[idx]
#define lnd tree[idx<<1]
#define rnd tree[(idx<<1)+1]
#define lndp (idx<<1),(b),((b+e)>>1)
#define rndp ((idx<<1)+1),(((b+e)>>1)+1),(e)
#define inf 1999999999
#define MX 100005
#define mod  1000000007
#define mod1 1000000009
#define var    int ii,i,j,k,z=0,c=0,t,x,y,l,r,mid
//int dx[8]={0,0,1,1,-1,-1,1,-1};  //8 direction
//int dy[8]={1,-1,1,-1,1,-1,0,0};

bool chk(ll x,ll pos){return (bool)(x&(1<<pos));}
ll Set(ll x,ll pos){return x=x|(1<<pos);}
ll clr(ll x,ll pos){return x=x&~(1<<pos);}

ll n,m,a[405];
pll dp[405][405];
bool v[405][405];

pll fn(ll l,ll r)
{
    if(l==r)return {0LL,a[l]};
    pll &z=dp[l][r];
    if(v[l][r]==1)return z;
    v[l][r]=1;
    z.xx=1e18;z.yy=0;
    for(ll i=l;i<r;i++)
    {
        pll x=fn(l,i);
        pll y=fn(i+1,r);
        ll c=x.xx+y.xx+x.yy+y.yy;
        if(c<z.xx)
        {
            z.xx=c;
            z.yy=x.yy+y.yy;
        }
    }
    return z;
}

int main()
{
    ll var;
    sfl(n);
    mem(dp,-1);
    for(i=1;i<=n;i++)
    {
        sfl(a[i]);
    }
    z=fn(1,n).xx;
    cout<<z<<endl;
}


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

using namespace std ;

const int MAX = 1e5 + 10 ;

long long arr[2][3] ;
int n ;

vector< pair<long long , long long> >v[2] ;

long long calc1(bool t , long long money)
{
	long long x = money / v[t][0].first * v[t][0].second ;
	x += money % v[t][0].first ;
	return x ;
}

long long calc2(bool t , long long money)
{
	long long Max = 0 ;
	for(int i = 0 ; i * v[t][0].first <= money ; ++i)
	{
		long long x = i * v[t][0].second ;
		long long rem = money - i * v[t][0].first ;
		x += rem / v[t][1].first * v[t][1].second ;
		x += rem % v[t][1].first ;
		Max = max(Max , x) ;
	}
	return Max ;
}

long long calc3(bool t , long long money)
{
	long long Max = 0 ;
	for(int i = 0 ; i * v[t][0].first <= money ; ++i)
	{
		for(int j = 0 ; i * v[t][0].first + j * v[t][1].first <= money ; ++j)
		{
			long long x = i * v[t][0].second + j * v[t][1].second ;
			long long rem = money - i * v[t][0].first - j * v[t][1].first ;
			x += rem / v[t][2].first * v[t][2].second ;
			x += rem % v[t][2].first ;
			Max = max(Max , x) ;
		}
	}
	return Max ;
}

int main()
{
	ios_base::sync_with_stdio(0) ;
	cin.tie(0) ;
	cin>>n ;
	cin>>arr[0][0]>>arr[0][1]>>arr[0][2] ;
	cin>>arr[1][0]>>arr[1][1]>>arr[1][2] ;
	int cnt = 0 ;
	for(int i = 0 ; i < 3 ; ++i)
	{
		if(arr[0][i] < arr[1][i])
		{
			v[0].emplace_back(arr[0][i] , arr[1][i]) ;
			cnt++ ;
		}
		else
			v[1].emplace_back(arr[1][i] , arr[0][i]) ;
	}
	if(cnt == 0)
		return cout<<calc3(1 , n)<<"\n" , 0 ;
	if(cnt == 1)
		return cout<<calc2(1 , calc1(0 , n))<<"\n" , 0 ;
	if(cnt == 2)
		return cout<<calc1(1 , calc2(0 , n))<<"\n" , 0 ;
	if(cnt == 3)
		return cout<<calc3(0 , n)<<"\n" , 0 ;
	return 0 ;
}		

Measured memory_bytes: 3656
#input of int(split by space)
def get_i()
  return gets.chomp.split(" ").map(&:to_i)
end
#input of float(split by space)
def get_f()
  return gets.chomp.split(" ").map(&:to_f)
end
#input of string(split by space)
def get()
  return gets.chomp.split(" ")
end
#input of string(split per one character)
def get_nsp()
  return gets.chomp.split("")
end
#yes or no decision
def yn_judge(bool,y="Yes",n="No")
  return bool ? y : n 
end
#create of array
def array(size1,init=nil,size2=-1)
  if size2==-1
      return Array.new(size1){init}
  else
      return Array.new(size2){Array.new(size1){init}}
  end
end

def max(a,b)
    return a>b ? a : b
end

def min(a,b)
    return a>b ? b : a
end

INF=Float::INFINITY

N,K=get_i
A=get_i.sort!{|x,y| y<=>x}
def fulfil?(x)
    res=0
    A.each do|a|
        break if a<=x
        res+=a/x
    end
    return res<=K
end
if K.zero?
    puts A[0]
    exit
end
head=1
tail=A[0]
while tail-head>1
    mid=head+(tail-head)/2
    if fulfil?(mid)
      tail=mid
    else
      head=mid
    end
end
if fulfil?(head)
    puts head
else
    puts tail
end

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

int main(){
	int N, M, Q; cin >> N >> M >> Q;

	vector<int> L(M), R(M);
	for(int i=0; i<M; ++i) cin >> L[i] >> R[i];

	vector<vector<int>> sum(N+1, vector<int>(N+1));
	for(int i=0; i<M; ++i) ++sum[L[i]][R[i]];
	for(int i=1; i<N+1; ++i){
		for(int k=1; k<N+1; ++k){
			sum[i][k] += sum[i-1][k] + sum[i][k-1] - sum[i-1][k-1];
		}
	}

	while(Q--){
		int p, q; cin >> p >> q;

		cout << sum[q][q] - sum[p-1][q] - sum[q][p-1] + sum[p-1][p-1] << endl;
	}
}


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

	public static void main(String[] args) {

		int A = in.nextInt();
		Set<String> set = new HashSet<>();
		for(int i = 0; i < A; i++) {
			set.add(in.next());
		}


		System.out.println(set.size());

	}



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

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

typedef long long ll;

int main() {
  ll N, Q;
  cin >> N >> Q;
  vector<ll> a(N), x(Q);
  for (int i = 0; i < N; i++) {
    cin >> a[i];
  }
  for (int i = 0; i < Q; i++) {
    cin >> x[i];
  }
  for (int i = 0; i < Q; i++) {
    ll ans = 0, sum = 0, r = 0;
    for (ll l = 0; l < N; l++) {
      while (r < N && sum + a[r] <= x[i]) {
        sum += a[r];
        r++;
      }
      ans += r - l;
      if (l == r) {
        r++;
      }
      else {
        sum -= a[l];
      }
    }
    cout << ans << endl;
  }
  return 0;
}


Measured memory_bytes: 3548
n, x, y = map(int, input().split())
ans = [0 for i in range(n-1)]
for i in range(n-1):
  k = i + 1
  b_1 = k - x
  c_1 = k - y
  if b_1 < 0:
      b_1 = - b_1
  if c_1 < 0:
      c_1 = - c_1
  for j in range(i+1, n):
    l = j + 1
    a = l - k
    b_2 = l - y
    c_2 = l - x
    if b_2 < 0:
      b_2 = - b_2
    if c_2 < 0:
      c_2 = - c_2
    b = b_1 + b_2 + 1
    c = c_1 + c_2 + 1
    if b < a:
      a = b
    if c < a:
      a = c
    ans[a-1] += 1
for i in range(n-1):
  print(ans[i])

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

using namespace std;
using vi = vector<int>;
using vvi = vector<vi>;


int get(const vvi &g, int H, int W, int h, int w)
{
  if (h < 0 || h >= H || w < 0 || w >= W)
    return 0;
  else
    return g[h][w];
}

int f(const vvi &g, int H, int W, int h, int w)
{
  int ans = 0;
  for (int i = -1; i <= 1; ++i)
    for (int j = -1; j <= 1; ++j)
      if (get(g, H, W, h+i, w+j) < 0)
        ++ans;
  return ans;
}

int main()
{
  int H, W;
  cin >> H >> W;
  vvi g(H, vi(W));
  for (int h = 0; h < H; ++h)
  {
    string s;
    cin >> s;
    for (int w = 0; w < W; ++w)
      g[h][w] = (s[w] == '.') ? 0 : -1;
  }

  for (int h = 0; h < H; ++h)
  {
    for (int w = 0; w < W; ++w)
    {
      if (g[h][w] < 0)
        cout << '#';
      else
        cout << f(g, H, W, h, w);
    }
    cout << endl;
  }  
}

Measured memory_bytes: 3600
n, a, b, c, d = map(lambda x: int(x) - 1, input().split())
s = list(input())

if c < d:
    x = a + 1
    while x < d:
        if s[x] == '#':
            if s[x+1] == '#':
                print('No')
                exit()
            else:
                x += 2
        else:
            x += 1
    print('Yes')
else:
    x = a + 1
    check = 0
    while x < c:
        if s[x] == '#':
            if s[x+1] == '#':
                print('No')
                exit()
            else:
                x += 2
        else:
            if b <= x <= d and check == 0:
                if s[x-1] == s[x] and s[x] == s[x+1]:
                    check = 1
            x += 1
    if check == 1:
        print('Yes')
    else:
        print('No')

Measured memory_bytes: 4916
#include <stdio.h>
#include <iostream>
#include <vector>
#include <iterator>
using namespace std;

int main(){
    int N, n, j;
    cin >> N;
    vector<int> A(N);
    j = 1;
    for (int i = 0; i < A.size(); i++)
    {
        cin >> n;
        A[n-1] = j++;
    }
    copy(begin(A), end(A), ostream_iterator<int>(cout, " "));
    cout << endl;
}

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

using namespace std;
typedef long long ll;
typedef unsigned long ul;
typedef pair<ul, ul> P;

const int INF = 1e9;
const int MOD =1e9+7;
const ll LINF = 1e18;

int main(){
    int N;
    vector<int> A;

    cin >> N;
    A.resize(N);

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

    sort(A.begin(), A.end(), greater<int>());
    cout << A[0] - A[A.size() - 1] << endl;
} 

Measured memory_bytes: 256
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>



using namespace std;


int main()
{
    unsigned long long a,b,c;
    cin>>a>>b;
    c=b/a+1;
    if(c==2) cout<<'1'<<endl;
    else
    {
        long long t=0;
        unsigned long long i;
        for(i=1;i<c;i*=2)
        {
            t++;
        }
        cout<<t<<endl;
    }


}


Measured memory_bytes: 256
#include<iostream>
#include<queue>
#include<string>

using namespace std;

typedef long long ll;

int N;
int E[200 + 10][200 + 10];
int used[200 + 10];

int main() {
	cin >> N;
	for (int i = 0; i < N; i++){
		string s;
		cin >> s;
		for (int j = 0; j < N; j++) {
			if (s[j] == '0' && i != j)E[i][j] = 1919810;
			else if(i != j)E[i][j] = 1;
			else E[i][j] = 0;
		}
	}
	
	used[0] = 1;
	queue<int> q;
	q.push(0);
	while (q.size()) {
		int now = q.front(); q.pop();
		for (int i = 0; i < N; i++) {
			if (E[now][i] == 1) {
				if (used[i] == 0) {
					used[i] = used[now] == 1 ? 2 : 1;
					q.push(i);
				}
				else if (used[i] == used[now]) {
					cout << -1 << endl;
					return 0;
				}
			}
		}
	}
	for (int k = 0; k < N; k++)for (int i = 0; i < N; i++)for (int j = 0; j < N; j++)
		if (E[i][j] > E[i][k] + E[k][j])E[i][j] = E[i][k] + E[k][j];
	int ans = 0;
	for (int i = 0; i < N; i++) {
		for (int j = 0; j < N; j++) {
			if (ans < E[i][j] && E[i][j] < 1919810)ans = E[i][j];
		}
	}
	cout << ans + 1 << endl;
	return 0;
}

Measured memory_bytes: 384
from collections import Counter, defaultdict
Q = 10**9+7
def main():
    N, M = map( int, input().split())
    A = list( map( int, input().split()))
    B = list( map( int, input().split()))
    CA = Counter(A)
    CB = Counter(B)
    for a in A:
        if CA[a] >= 2:
            print(0)
            return
    for b in B:
        if CB[b] >= 2:
            print(0)
            return
    R = defaultdict( lambda: -1)
    C = defaultdict( lambda: -1)
    for i,a in enumerate(A):
        R[a] = i
    for j, b in enumerate(B):
        C[b] = j
    columns = 0
    rows = 0
    ditermined = 0
    ans = 1
    for n in range(N*M,0,-1):
        if R[n] >= 0 and C[n] >= 0:
            columns += 1
            rows += 1
            ditermined += 1
            continue
        if R[n] >= 0:
            rows += 1
            ans *= columns
            ans %= Q
            ditermined += 1
            continue
        if C[n] >= 0:
            columns += 1
            ans *= rows
            ans %= Q
            ditermined += 1
            continue
        if columns*rows == ditermined:
            print(0)
            return
        ans *= (columns*rows-ditermined)
        ans %= Q
        ditermined += 1
    print(ans)
if __name__ == '__main__':
    main()


Measured memory_bytes: 134296
// template version 1.6
// {{{ define basic macro
using namespace std;
#include <iostream>
#include <bits/stdc++.h>
// varibable settings
#define int long long
#define INF 1000000000000000000LL
// #define INF 2147483647
#define MOD 1000000007LL
// #define MOD 998244353LL
#define infile "../test/sample-1.in"

#define _overload3(_1,_2,_3,name,...) name
#define _rep(i,n) repi(i,0,n)
#define repi(i,a,b) for(int i=(int)(a);i<(int)(b);++i)
#define rep(...) _overload3(__VA_ARGS__,repi,_rep,)(__VA_ARGS__)
#define _rrep(i,n) rrepi(i,0,n)
#define rrepi(i,a,b) for(int i=(int)(b-1);i>=(int)(a);--i)
#define rrep(...) _overload3(__VA_ARGS__,rrepi,_rrep,)(__VA_ARGS__)
#define all(x) (x).begin(),(x).end()
#define sz(x) ((int)(x).size())
#define pb(a) push_back(a)
#define mp(a, b) make_pair(a, b)
#define uni(x) sort(all(x));x.erase(unique(all(x)),x.end())
#define ten(n) ((int)1e##n)
template<class T, size_t N> size_t sza(const T (&array)[N]) { return N; }
template<class T> inline void chmax(T &a, const T &b) { if(a < b) a = b; }
template<class T> inline void chmin(T &a, const T &b) { if(a > b) a = b; }
template<class T=int> T in() {T x; cin>>x; return (x);}
struct Fast { Fast(){ std::cin.tie(0); ios::sync_with_stdio(false); } } fast;

// dump macro
#ifdef PCM
  #include "dump.hpp"
#else
  #define dump(...) 42
  #define dump_1d(...) 42
  #define dump_2d(...) 42
#endif

typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef long double ld;
typedef pair<int,int> pii;
typedef tuple<int,int,int> iii;
template<typename T> using PQ = priority_queue<T, vector<T>, greater<T>>;
int dx[]={1, -1, 0, 0};
int dy[]={0, 0, 1, -1};
#define fi first
#define se second

// }}}

int solve(){
  int n,k;cin>>n>>k;
  string a[n]; rep(i, n) {
    int tmp;
    cin>>tmp;
    stringstream ss;
    ss << bitset<41>(tmp);
    a[i] = ss.str();
  }
  dump_1d(a, n);


  int P[41] = {};
  P[0] = 1;
  rep(i, 1, 41){
    P[i] = P[i-1]*2;
  }

  int mult = 40;
  int s = 0;
  int res = 0;
  rep(i, 41){
    int p = 0;int q = 0;
    rep(j, n){
      if (a[j][i]=='1') p++;
      else q++;
    }

    if (i>=38) {
      dump(p, q, mult, P[mult], s);
    }
    if (q>p && s+P[mult]<=k) {  // 1
      s += P[mult];
      res += P[mult]*q;
    }
    else{
      res += P[mult]*p;
    }
    mult -= 1;
  }
  cout << res << endl;

  return 0;
}

signed main() { //{{{
#ifdef INPUT_FROM_FILE
  std::ifstream in(infile);
  std::cin.rdbuf(in.rdbuf());
#endif
  solve();
  return 0;
} //}}}


Measured memory_bytes: 8832
#include<stdio.h>
int main(){
    int n,i;
    int max = 0;
    int tmp = 0;
    int h[100000];
    
    scanf("%d",&n);
    for(i = 0 ; i < n ; i++) scanf("%d",&h[i]);
    
    for(i = 0 ; i < n-1 ; i++){
        if(h[i] >= h[i+1]){
            tmp++;
            if(tmp > max) max = tmp;
//            printf("tmp is %d\n",tmp);
        }else{
            tmp = 0;
        }
    }
    printf("%d\n",max);
    return 0;
}


Measured memory_bytes: 512
// created_by  Rishi_Mohan

#include<bits/stdc++.h>

#define    ll           long long
#define    db1(x)       cout<<#x<<"="<<x<<'\n'
#define    db2(x,y)     cout<<#x<<"="<<x<<","<<#y<<"="<<y<<'\n'
#define    db3(x,y,z)   cout<<#x<<"="<<x<<","<<#y<<"="<<y<<","<<#z<<"="<<z<<'\n'
#define    rep(i,n)     for(int i=0;i<(n);++i)
#define    repA(i,a,n)  for(int i=a;i<=(n);++i)
#define    repD(i,a,n)  for(int i=a;i>=(n);--i)

using namespace std;

void solve()
{
    int n;
    cin >> n;
    ll sum=0;
    for(int i=1;i<=n;i++)
    {
      for(int j=1;j<=n;j++)
      {
      	for(int k=1;k<=n;k++)
      	{
      		sum+=__gcd(i,__gcd(j,k)); 
      	}
      }
    }
   cout <<  sum;
}

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

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

using namespace std;

#define int long long

int const maxn = 2e5 + 5;
int sum[4 * maxn], nxt[maxn];

void update(int i, int l, int r, int lq, int rq, int x) {
    if (lq >= r || l >= rq) return;
    if (lq <= l && r <= rq) {
        sum[i] += x;
        return;
    }
    int m = (r + l) / 2;
    update(2 * i + 1, l, m, lq, rq, x);
    update(2 * i + 2, m, r, lq, rq, x);
}

int get(int i, int l, int r, int lq) {
    if (r - l == 1) return sum[i];
    int m = (r + l) / 2;
    if (lq < m) return get(2 * i + 1, l, m, lq) + sum[i];
    return sum[i] + get(2 * i + 2, m, r, lq);
}

main() {
#ifdef HOME
    freopen("input.txt", "r", stdin);
#endif // HOME
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int n, d, a, x, h, ans = 0;
    cin >> n >> d >> a;
    vector < pair < int, int > > b;
    for (int i = 1; i <= n; ++i) {
        cin >> x >> h;
        b.push_back({x, h});
    }
    sort(b.begin(), b.end());
    int Lef = 0, Righ = 0;
    while (Lef < n) {
        while (Righ < n && b[Righ].first - b[Lef].first <= 2 * d) Righ++;
        nxt[Lef] = Righ - 1;
        Lef++;
    }
    for (int i = 0; i < n; ++i) {
        int f = get(0, 0, n, i);
        b[i].second -= f * a;
        if (b[i].second > 0) {
            int cnt = (b[i].second + a - 1) / a;
            ans += cnt;
            update(0, 0, n, i, nxt[i] + 1, cnt);
        }
    }
    cout << ans << '\n';
    return 0;
}



Measured memory_bytes: 10220
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
using ll = long long;
using ld = long double;
const ll ATCODER_AMARI = 1000000007; //10^9 + 7
const ll INF = 10000000000000;

//階乗関数　ATCODER_AMARIに注意
ll factorial(ll k){
  ll sum = 1;
  for(int i = 1 ; i < k + 1; i++) {
    sum *= i;
    //sum = sum % ATCODER_AMARI;
  }
  return sum;
}

int main() {
  ll n,m;
  cin >> n >> m;
  vector<bool> prob(n,false);
  vector<ll> num(n,1);
  
  prob.at(0) = true;

  rep(i,m) {
    ll x,y;
    cin >> x >> y;
    x--;
    y--;

    num.at(x)--;
    num.at(y)++;

    if(prob.at(x)) {
      prob.at(y) = true;
    }

    if(num.at(x) == 0) {
      prob.at(x) = false;
    }

  }

  ll ans = 0;

  rep(i,n) {
    if(prob.at(i)) ans++;
  }

  cout << ans << endl;
}


Measured memory_bytes: 3916
import sys

# sys.stdin = open('a1.in')


def read_int_list():
    return list(map(int, input().split()))


def read_str_list():
    return input().split()


def read_int():
    return int(input())


def read_str():
    return input()


def main():
    n = read_str()
    if n == ''.join(list(reversed(n))):
        res = 'Yes'
    else:
        res = 'No'
    print(res)


main()


Measured memory_bytes: 2940
#include <bits/stdc++.h>
using namespace std;
#define debug(...) { printf("#  "); printf(__VA_ARGS__); puts(""); }
#define ll long long
#define endl "\n" 
constexpr int N = 1e5 + 10;
constexpr ll mod = 1e9 + 7;
template <class T1, class T2> inline void chadd(T1 &x, T2 y) { x += y; if (x >= mod) x -= mod; else if (x < 0) x += mod; } 
template <class T1, class T2> inline void chmax(T1 &x, T2 y) { if (x < y) x = y; }
template <class T1, class T2> inline void chmin(T1 &x, T2 y) { if (x > y) x = y; }
inline int rd() { int x; cin >> x; return x; }
template <class T> inline void rd(T &x) { cin >> x; }
template <class T > inline void out(T s) { cout << s << "\n"; }
inline ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
inline ll qpow(ll base, ll n) { ll res = 1; while (n) { if (n & 1) res = res * base % mod; base = base * base % mod; n >>= 1; } return res; }
int n, k, a[N], f[N], g[N];
inline ll F(ll n) {
	return (n * (n + 1) / 2) % mod;
}
void run() {
	for (int i = 1; i <= n; ++i) cin >> a[i];
	ll res = 0;
	for (int i = n; i >= 1; --i) {
		f[i] = 0; g[i] = 0;
		for (int j = i + 1; j <= n; ++j) 
			f[i] += a[j] < a[i];
		for (int j = 1; j <= n; ++j)
			g[i] += a[j] < a[i]; 
		chadd(res, 1ll * k * f[i] % mod); 
		chadd(res, 1ll * F(k - 1) * g[i] % mod);
	}
	cout << res << endl;
}

int main() {
	ios::sync_with_stdio(false);
	cin.tie(nullptr); cout.tie(nullptr);
	cout << fixed << setprecision(20);
	while (cin >> n >> k) run();
	return 0;
}



Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
const long long mod = 1e9 + 7;
using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
using ull = unsigned long long;
using ld = long double;
#define rep(i, n) for (int i = 0; i < n; ++i)
#define rep1(i, n) for (int i = 1; i < n; ++i)
#define pb push_back
#define mp make_pair
#define INF (1e9)
#define PI (acos(-1))
#define EPS (1e-7)
ull gcd(ull a, ull b) { return b ? gcd(b, a % b) : a; }
ull lcm(ull a, ull b) { return a / gcd(a, b) * b; }

void solve(long long A, long long B, long long C) {
  // main coding
  cout<<max(0LL,C-(A-B))<<endl;
}

int main() {
  long long A;
  scanf("%lld", &A);
  long long B;
  scanf("%lld", &B);
  long long C;
  scanf("%lld", &C);
  solve(A, B, C);
  return 0;
}


Measured memory_bytes: 256
#include <stdio.h>
#include <algorithm>
#include <iostream>
#include <memory.h>
#include <queue>

using namespace std;
typedef long long LL;

const int maxn = 200005;
int n,ehead[maxn],ecnt,f[maxn];
struct edge {
	int u,v,next;
}edg[maxn];
void add(int u,int v) {
	edg[++ecnt]=(edge){u,v,ehead[u]};
	ehead[u]=ecnt;
	edg[++ecnt]=(edge){v,u,ehead[v]};
	ehead[v]=ecnt;
}
void solve(int u,int fa)
{
	for (int v,j=ehead[u];j;j=edg[j].next)
	if ((v=edg[j].v)!=fa)
		solve(v,u),f[u]^=f[v]+1;
}
int main()
{
	#ifdef LOCAL_JUDGE
		freopen("tree.in","r",stdin);
		freopen("tree.out","w",stdout);
	#endif
	scanf("%d",&n);
	for (int u,v,i=1;i<n;i++)
		scanf("%d %d",&u,&v),add(u,v);
	solve(1,0);printf("%s",f[1]?"Alice":"Bob");
	return 0;
}

Measured memory_bytes: 8064
import sys

sys.setrecursionlimit(10 ** 6)
int1 = lambda x: int(x) - 1
p2D = lambda x: print(*x, sep="\n")
def II(): return int(sys.stdin.readline())
def MI(): return map(int, sys.stdin.readline().split())
def LI(): return list(map(int, sys.stdin.readline().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]
def SI(): return sys.stdin.readline()[:-1]

def cal(xx):
    res=1
    for x in xx:
        res=res*x%md
    return res

md=10**9+7
n,k=MI()
aa=LI()
if n==k:
    print(cal(aa))
    exit()
aa.sort(key=lambda x:-abs(x))
#print(aa)
pre=aa[:k]
pos=aa[k:]
mnp=mnn=md
mnpi=mnni=-1
neg=0
for i,a in enumerate(pre):
    if a==0:
        print(0)
        exit()
    if a<0:neg+=1
    if a>0 and a<mnp:
        mnp=a
        mnpi=i
    if a<0 and -a<mnn:
        mnn=-a
        mnni=i
if neg%2:
    mxp=mxn=0
    for a in pos:
        if a > 0 and a > mxp:
            mxp = a
        if a < 0 and -a > mxn:
            mxn = -a
    if (mnni!=-1 and mxp) or (mnpi!=-1 and mxn):
        if mnpi==-1 or mxp*mnp>mxn*mnn:pre[mnni]=mxp
        else:pre[mnpi]=-mxn
    else:
        pre=aa[-k:]

print(cal(pre))


Measured memory_bytes: 31776
A = int(input())

AC = 0
WA = 0
TLE = 0
RE = 0


for i in range(A):
  B = input()
  
  if B == "AC":
    AC+=1
  elif B == "WA":
    WA+=1
  elif B == "TLE":
    TLE+=1
  elif B == "RE":
    RE+=1

print("AC x {0}".format(AC))
print("WA x {0}".format(WA))
print("TLE x {0}".format(TLE))
print("RE x {0}".format(RE))

Measured memory_bytes: 9216
#include <stdio.h>
#include <stdlib.h>
#include <math.h>

int main(void) {
  long a, b, x, ans;

  if(scanf("%ld %ld %ld", &a, &b, &x) == 1);

  if(a % x == 0) {
    ans = (b / x) - (a / x) + 1;
  } else {
    ans = (b / x) - (a / x);
  }

  printf("%ld", ans);

  return 0;
}


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

int main()
{
    int l,r,d;
    cin >> l >> r >> d;

    int i = 1;
    int ans = 0;
    int tmp = 1;
    while(true) {    
    tmp = d * i;
    if(tmp > r) break;
        if(tmp >= l) {
            ans++;
        }
        i++;
    }
    cout << ans << endl;
}

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


int main()
{

    string s;

    int n;


    char c = 'A';

    //cout << (int)(c) << endl;

    string s1;


    cin >> n >> s;



    int k = n%26;

    for(int i=0; i<s.length(); i++)
    {
        int a = (int)(s[i]);

        int b = (a + k);



         //cout << b << endl;


        if(b>=91)
        {
            b =b - 26;
        }
        else
            b = b;

        s1.push_back((char)(b));
    }

    cout << s1 << endl;



    return 0;
}


Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vll = vector<ll>;
#define all(x) (x).begin(), (x).end()
#define fsp(x) fixed << setprecision(x)
//const ll p = 1e9 + 7;
//const ll p = 998244353;
const ll inf = LLONG_MAX;
const long double pi = acos(-1);
void Yes() {cout << "Yes" << endl;}
void No() {cout << "No" << endl;}
void YES() {cout << "YES" << endl;}
void NO() {cout << "NO" << endl;}

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

	string n;
	ll k;
	cin >> n >> k;
	ll m = n.size();

	ll ans = pow(9, k);
	for (ll i = 0; i < k; i++) ans *= m - 1 - i;
	for (ll i = 0; i < k; i++) ans /= i + 1;

	vector<vector<vll>> dp(2, vector<vll>(m, vll(k + 1, 0)));
	dp[0][0][1] = n[0] - '0' - 1;
	dp[1][0][1] = 1;
	for (ll i = 1; i < m; i++) {
		for (ll j = 1; j <= k; j++) {
			dp[0][i][j] = dp[0][i - 1][j] + dp[0][i - 1][j - 1] * 9;
			if (n[i] != '0') {
				dp[0][i][j] += dp[1][i - 1][j];
				dp[0][i][j] += dp[1][i - 1][j - 1] * (n[i] - '0' - 1);
			}
			if (n[i] == '0') dp[1][i][j] = dp[1][i - 1][j];
			else dp[1][i][j] = dp[1][i - 1][j - 1];

			//cout << endl;
			//cout << 0 << " " << i + 1 << " " << j << " " << dp[0][i][j] << endl;
			//cout << 1 << " " << i + 1 << " " << j << " " << dp[1][i][j] << endl;
			//cout << endl;
		}
	}
	cout << ans + dp[0][m - 1][k] + dp[1][m - 1][k] << endl;
}


Measured memory_bytes: 256
A = int(input())
B = int(input())
C = int(input())
X = int(input())

ans = 0

for i in range(0,A+1):
    for j in range(0,B+1):
        for k in range(0,C+1):
            if (i*500 + 100*j + 50*k) == X:
                ans +=1

print(ans)

Measured memory_bytes: 3060
N, T = map(int, input().split())
cList = []; tList = []
for i in range(N):
    c, t = map(int, input().split())
    cList.append(c); tList.append(t)
ans = 10**4
for i in range(N):
    if tList[i] <= T:
        ans = min(ans, cList[i])
print(ans if not ans == 10**4 else "TLE") 


Measured memory_bytes: 3060
n=int(input())
t=list(int(input())for i in range(n))
def gcd(a,b):
    while b!=0:
        a,b=b,a%b
    return a
def lcm(a,b):
    return a*b//gcd(a,b)
ans=t[0]
for i in range(n):
    ans=lcm(ans,t[i])
print(ans)


Measured memory_bytes: 3060
import java.util.*;
import java.math.*;

public class Main {
  
  	private static final BigInteger MAX 
      = new BigInteger("1000000000000000000");
  
	public static void main (String[] args) throws Exception {
      
		final Scanner in = new Scanner(System.in);
      	final int N = in.nextInt();
      	BigInteger product = BigInteger.ONE;
       boolean hasZero = false;
        boolean tooLarge = false;

        for (int i = 0; i < N; ++i) {
            final BigInteger next = in.nextBigInteger();
            if (next.compareTo(BigInteger.ZERO) == 0) {
                hasZero = true;
            }
            if (!tooLarge) {
                product = product.multiply(next);
                if (product.compareTo(MAX) > 0) {
                    tooLarge = true;
                }
            }            
        }
        
        if (hasZero) {
            System.out.println(0);
        } else if (tooLarge) {
            System.out.println(-1);
        } else {
            System.out.println(product);
        }      

		
	}
}

Measured memory_bytes: 58744
// C++ includes
#include <bits/stdc++.h>
// 俳句
using namespace std;

// alias templates
template <class T> using V = vector<T>;
template <class T> using VV = V<V<T>>;

// alias declaration
using ll = long long;
using str = string;

using vll = V<ll>;
using vvll = V<vll>;
using vs = V<str>;

// loops
#define FOR(i, a, b)  for (ll i = (a); i <= (ll)(b); i++) // [a,b]
#define rFOR(i, a, b) for (ll i = (b); i >= (ll)(a); i--) // [b,a]
#define REP(i, a) FOR((i), 0, (ll)(a)-1) // [0,a-1]

// for debug
#define dbg(a) cerr << "  " << (#a) << ": " << (a) << "\n";

// procon common constants
const ll MOD_BIG = 1e9 + 7;

ll n;
vll p;

int main(void){
    cin >> n;
    p.resize(n);
    REP(i, n){
        cin >> p[i];
    }

    ll chigau = 0;
    REP(i, n){
        if(p[i]!=(i+1)){
            chigau++;
        }
    }

    if(chigau>=3){
        puts("NO");
    }else if(chigau==2 || chigau==0){
        puts("YES");
    }else{
        puts("?????");
        //ありえない
    }

}

Measured memory_bytes: 256
#include <bits/stdc++.h>
#include <vector>
#include <queue>
#include <set>
#include <iostream>
#include <iomanip>
using namespace std;
typedef long long LL;
#define REP(i,n) for(int i=0;i<(n);i++)
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
template<typename T1,typename T2> inline void chmin(T1 &a,T2 b){if(a>b) a=b;}
template<typename T1,typename T2> inline void chmax(T1 &a,T2 b){if(a<b) a=b;}

int main(){
    int N, K;
    cin >> N >> K;
    vector<int> L(K), R(K);
    vector<LL> Sums(K, 0);
    REP(i,K){
        cin >> L[i] >> R[i];
    }

    LL ans = 0;
    LL M = 998244353;

    vector<LL> route(N+1,0);
    route[1] = 1;

    for(int i=2; i<=N; i++){

        for(int k=0; k<K; k++){

            if(i - L[k] < 0){continue;}

            if(i - L[k] >= 0){
                int index = i - R[k] - 1;
                if(index >= 0){
                    LL tmp = (Sums[k] - route[index])%M;
                    if(tmp < 0){tmp += M;}
                    Sums[k] = (route[i - L[k]] + tmp )%M;
                }else{
                    Sums[k] = (Sums[k] + route[i - L[k]])%M;   
                }
            }

        }

        for(int k=0; k<K; k++){
            route[i] = (route[i] + Sums[k])%M;
        }
    }

    cout << route[N] << endl;
    return 0;

}

Measured memory_bytes: 4840
#include<bits/stdc++.h>
using namespace std;
int main(){
	int n;
	while(cin >> n , n){
		int a,b,c;
		int g[12][12];
		for(int i = 0; i < 12; i++)
			for(int j = 0; j < 12; j++){
				if(i == j)
					g[i][j] = 0;
				else
					g[i][j] = 100000000;
			}
		int judge[12] = {0};
		for(int i = 0; i < n; i++){
			cin >> a >> b >> c;
			g[a][b] = c;
			g[b][a] = c;
			judge[a] = judge[b] = 1;
		}
		vector <int> used;
		vector <int> dist;
		int ans = 100000000,num;
		for(int i = 0; i < 11; i++){
			if(!judge[i])
				continue;
			used = vector <int> (11,0);
			dist = vector <int> (11,100000000);
			dist[i] = 0;
			int sum = 0;
			while(1){
				int va = -1;
				for(int j = 0; j < 11; j++){
					if(!judge[j])
						continue;
					if(used[j] == 0 && (va == -1 || dist[va] > dist[j]))
						va = j;
				}
				if(va == -1)
					break;
				used[va] = 1;
				for(int j = 0; j < 11; j++){
					if(!judge[j])
						continue;
					dist[j] = min(dist[j] , dist[va] + g[j][va]);
				}
			}
			for(int j = 0; j < 11; j++){
				if(!judge[j])
					continue;
				sum += dist[j];
			}
			if(ans > sum){
				ans = sum;
				num = i;
			}
		}
		cout << num << " " << ans << endl;
	}
	return 0;
}

Measured memory_bytes: 3152
#include<bits/stdc++.h>
using namespace std;
const int mod=1e9+7;
  int h,w;
int valid(int x,int y)
{
    if((x>=0&&x<h)&&(y>=0&&y<w)) return 1;
    return 0;
}
void add_self(long long &x,long long &y)
{
    x=(x+y)%mod;
}
int main()
{
  
    cin>>h>>w;
    int grid[h][w];
    long long dp[h][w];
    for(int i=0;i<h;i++)
    {
        for(int j=0;j<w;j++)
        {
            dp[i][j]=0;
            char x;
            cin>>x;
            if(x=='#')
            grid[i][j]=0;
            else  grid[i][j]=1;
        }
    }
    
    dp[0][0]=1;
    for(int i=0;i<h;i++)
    {
        for(int j=0;j<w;j++)
        {
            if(valid(i-1,j)&&grid[i-1][j]!=0) add_self(dp[i][j],dp[i-1][j]);
            if(valid(i,j-1)&&grid[i][j-1]!=0) add_self(dp[i][j],dp[i][j-1]);
        }
    }
    cout<<dp[h-1][w-1]%mod;
    

}

Measured memory_bytes: 11904
#include <bits/stdc++.h>
using namespace std;
 
int main(){
  
  int n, a, b;
  
  cin >> n >> a;
  
  b = n % 500;
    
  if (a >= b){
    cout << "Yes" << endl;
  }
  
  else{
    cout << "No" << endl;
  }
}


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

#define IS_EVEN(a) ( ((a)&0x01) == 0 )

int digit_sum(int n) {
    int ret = 0;
    while(n!=0) {
        ret += n%10;
        n = n / 10;
    }
    return ret;
}

int main() {
    int n,a,b;
    cin >> n;
    cin >> a;
    cin >> b;

    int cnt=0;
    for(int i = 1 ; i < n+1 ; i++) {
        int sum = digit_sum(i);
        if( a <= sum && sum <= b) {
            cnt+=i;
        }
    }
    cout << cnt << endl;
    return 0;
}


Measured memory_bytes: 256
#include <iostream>
using namespace std;
#define REP(i,n) for (int i=0;i<(n);++i)
using ll = long long;
constexpr ll MOD = 1e9 + 7;

int main() {
    int N;cin>>N;
    string S1,S2;cin>>S1>>S2;
    ll ans,i;
    bool isvert;
    if(S1[0]==S1[1]) ans = 6, i = 2, isvert = false;
    else ans = 3, i = 1, isvert = true;
    
    for(;i < N;++i){
        if(S1[i]==S2[i]){
            if(isvert){
                ans = (ans * 2)%MOD;
            }
            isvert = true;
        } else {
            if(isvert){
                ans = (ans * 2)%MOD;
            } else {
                ans = (ans * 3)%MOD;
            }
            isvert = false;
            ++i;
        }
    }
    cout << ans << '\n';
    return 0;
}

Measured memory_bytes: 256
#include<stdio.h>
#include<iostream>
#include<vector>
#include<algorithm>
#include<string>
#include<string.h>
using namespace std;

typedef long long LL;
typedef vector<int> VI;

#define REP(i,n) for(int i=0, i##_len=(n); i<i##_len; ++i)
#define EACH(i,c) for(__typeof((c).begin()) i=(c).begin(),i##_end=(c).end();i!=i##_end;++i)
#define eprintf(...) fprintf(stderr, __VA_ARGS__)

template<class T> inline void amin(T &x, const T &y) { if (y<x) x=y; }
template<class T> inline void amax(T &x, const T &y) { if (x<y) x=y; }
template<class Iter> void rprintf(const char *fmt, Iter begin, Iter end) {
    for (bool sp=0; begin!=end; ++begin) { if (sp) putchar(' '); else sp = true; printf(fmt, *begin); }
    putchar('\n');
}

int N;
char S[300111];

void MAIN() {
    scanf("%d", &N);
    scanf("%s", S);

    int w = 0, e = count(S, S+N, 'E');
    int ans = N;
    REP (i, N) {
	if (S[i] == 'E') e--;
	amin(ans, w + e);
	if (S[i] == 'W') w++;
    }
    printf("%d\n", ans);
}

int main() {
    int TC = 1;
//    scanf("%d", &TC);
    REP (tc, TC) MAIN();
    return 0;
}



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

int main(){

    string s;cin >> s;

    int sum=0;
    for(int i=0;i<4;++i){
        if(s.at(i)=='+')sum+=1;
        else sum-=1;
    }

    cout << sum << endl;
    return 0;

}


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

public class Main {
    static int bisectLeft(int[] a, int key) {
        int idx = Arrays.binarySearch(a, key);
        if (idx < 0) {
            return -idx - 1;
        }
        while (a[idx] == key) {
            idx--;
        }
        idx++;
        return idx;
    }

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

        int N = sc.nextInt();
        int[] L = new int[N];
        for (int i = 0; i < N; i++) {
            L[i] = sc.nextInt();
        }

        Arrays.sort(L);
        int result = 0;
        for (int i = 0; i < N - 2; i++) {
            for (int j = i + 1; j < N - 1; j++) {
                result += bisectLeft(L, L[i] + L[j]) - j - 1;
            }
        }
        System.out.println(result);
    }
}


Measured memory_bytes: 26600
from collections import defaultdict
import heapq

case = 1
while True:
    n, t, k = map(int, input().split())
    if n == t == k == 0:
        break

    costs = [0] * 10001

    # 全ての辺をまず壊して考える
    E = defaultdict(list)
    for i in range(n-1):
        fr, to, c = map(int, input().split())
        E[fr - 1].append((c, to - 1))
        E[to - 1].append((c, fr - 1))
        costs[c] += 1

    bases = [None]*t
    # isUnitedが負の点がなくなるようにしなければいけない
    isUnited = [False] * n
    for i in range(t):
        b = int(input()) - 1
        bases[i] = b
        isUnited[b] = True

    q = []
    for b in bases:
        for c, y in E[b]:
            if not isUnited[y]:
                # heapqで降順にするためにコストに負を掛ける
                heapq.heappush(q, (-c, y))

    while q:
        c_minus, y = heapq.heappop(q)
        if isUnited[y]:
            continue
        isUnited[y] = True
        # -c_minus == c
        # 壊してはいけない辺を復活させる
        costs[-c_minus] -= 1

        for nc, ny in E[y]:
            if not isUnited[ny]:
                heapq.heappush(q, (-nc, ny))

    p = 0
    ans = 0
    # 壊せる辺の中でコストの低い順からk個だけとる
    while k > 0:
        while k > 0 and costs[p] > 0:
            costs[p] -= 1
            k -= 1
            ans += p
        p += 1

    print("Case {}: {}".format(case, ans))
    case += 1




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

int main() {
  int N, a=0, b=0;
  cin >> N;
  vector<int> c(N+2, 0);
  for(int i=0;i<N;i++){
    cin >> c.at(i);
  }
  
  sort(c.begin(), c.end());
  reverse(c.begin(), c.end());
  
  for(int i=0; i<N/2+1;i++){
    a+=c.at(2*i);
    b+=c.at(2*i+1);
  }
  cout << a-b <<endl;
}

Measured memory_bytes: 256
macro_rules! input {
    (source = $s:expr, $($r:tt)*) => {
        let mut iter = $s.split_whitespace();
        let mut next = || { iter.next().unwrap() };
        input_inner!{next, $($r)*}
    };
    ($($r:tt)*) => {
        let stdin = std::io::stdin();
        let mut bytes = std::io::Read::bytes(std::io::BufReader::new(stdin.lock()));
        let mut next = move || -> String {
            bytes
                .by_ref()
                .map(|r| r.unwrap() as char)
                .skip_while(|c| c.is_whitespace())
                .take_while(|c| !c.is_whitespace())
                .collect()
        };
        input_inner!{next, $($r)*}
    };
}

macro_rules! input_inner {
    ($next:expr) => {};
    ($next:expr, ) => {};

    ($next:expr, $var:ident : $t:tt $($r:tt)*) => {
        let $var = read_value!($next, $t);
        input_inner!{$next $($r)*}
    };
}

macro_rules! read_value {
    ($next:expr, ( $($t:tt),* )) => {
        ( $(read_value!($next, $t)),* )
    };

    ($next:expr, [ $t:tt ; $len:expr ]) => {
        (0..$len).map(|_| read_value!($next, $t)).collect::<Vec<_>>()
    };

    ($next:expr, chars) => {
        read_value!($next, String).chars().collect::<Vec<char>>()
    };

    ($next:expr, usize1) => {
        read_value!($next, usize) - 1
    };

    ($next:expr, $t:ty) => {
        $next().parse::<$t>().expect("Parse error")
    };
}

use std::collections::HashSet;

fn main() {
    input!{
        h: usize,
        w: usize,
        n: usize,
        ps: [[usize; 2]; n]
    }
    if h == 1 {
        println!("1");
        return;
    }
    let mut tab = vec![Vec::new(); w];
    let mut set = HashSet::new();
    for i in 0..n {
        if ps[i][0] < ps[i][1] {
            continue;
        }
        tab[ps[i][1] - 1].push(ps[i][0]);
        set.insert(ps[i].clone());
    }
    let mut r = vec![0; w];
    let mut pos = 0;
    for i in 0..w {
        pos += 1;
        while set.contains(&vec![pos, i + 1]) {
            pos += 1;
        }
        let mut min = h + 1;
        for j in 0..tab[i].len() {
            if tab[i][j] > pos && min > tab[i][j] {
                min = tab[i][j];
            }
        }
        r[i] = min;
    }
    println!("{}", r.iter().min().unwrap() - 1);
}


Measured memory_bytes: 34984
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <vector>
#include <cstring>
#include <queue>
#include <cmath>
#include <map>
#include <set>
#include <string>
#define SIZE 10005

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

int A[SIZE];

int main()
{
	int n;
	scanf("%d",&n);
	int all=1;
	for(int i=0;i<n;i++) all*=3;
	int ret=1;
	for(int i=0;i<n;i++)
	{
		scanf("%d",&A[i]);
		if(A[i]%2==0) ret*=2;
	}
	printf("%d\n",all-ret);
	return 0;
}


Measured memory_bytes: 128
n, l = map(int, input().split())
tastes = [l+(i-1) for i in range(1, n+1)]
a = sum(tastes)
tastes_min = min([abs(a - sum(tastes[:i] + tastes[i+1:])) for i in range(len(tastes))])
print(a - tastes_min if a >= 0 else a + tastes_min)

Measured memory_bytes: 3060
n = int(input())
a = list(map(int, input().split()))
two = []
four = []

for i in a:
	if i % 4 == 0:
		four.append(i)
	elif i % 2 == 0:
		two.append(i)

if (2 * len(four) + len(two) >= n) or 2 * len(four) + 1 >= n:
	print("Yes")
else:
	print("No")

Measured memory_bytes: 15020
import sys
from heapq import *
stdin=sys.stdin
    
ns = lambda:stdin.readline().rstrip()
ni = lambda:int(stdin.readline().rstrip())
nm = lambda:map(int,stdin.readline().split())
nl = lambda:list(map(int,stdin.readline().split()))
    
H,W=nm()
    
INF=10**18
def dijkstra(start,edge):
    d=[[INF]*W for i in range(H)]
    d[start[0]][start[1]]=0
    q=[(0,start[0],start[1])]
    while(len(q)!=0):
        ci,i,j=heappop(q)
        #if(d[start[0]][start[1]]<ci):
        #    continue 
        for p in edge[i][j]:
            if(d[p[0]][p[1]]>d[i][j]+1):
                d[p[0]][p[1]]=d[i][j]+1
                heappush(q,(d[p[0]][p[1]],p[0],p[1]))
    return d
    
E=[[[] for _ in range(W)] for _ in range(H)] 
S=[] 
    
for i in range(H):
    tmp=ns()
    S.append(tmp)
    
    
for i in range(H):
    for j in range(W):
        if( j+1<=W-1 and S[i][j+1]=='.'):
            E[i][j].append((i,j+1))
        if( i+1<=H-1 and S[i+1][j]=='.'):
            E[i][j].append((i+1,j))
        if(S[i][j-1]=='.' and 0<=j-1):
            E[i][j].append((i,j-1))
        if(S[i-1][j]=='.' and 0<=i-1):
            E[i][j].append((i-1,j))
    
    
    
ans=0
    
d=[[0]*W for i in range(H)]
for i in range(H):
    for j in range(W):
        if(S[i][j]!='.'):
            continue
        d=dijkstra((i,j),E)
        for k in range(H):
            for l in  range(W):
                if(ans<d[k][l] and d[k][l]<10**18):
                    ans=d[k][l]
                    
print(ans)


Measured memory_bytes: 3192
#include <algorithm>
#include <cstring>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <map>

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

using namespace std;
typedef pair<int, int> i_i;

int a,w,h;
bool t[22][22],s[22][22];

void dfs(i_i p) {
    s[p.first][p.second] = 1;
    a += 1;
    rep(j,2){
        if(t[p.first][p.second+(int)pow(-1,j)] && s[p.first][p.second+(int)pow(-1,j)] == 0) {
            dfs(make_pair(p.first,p.second+(int)pow(-1,j)));
        }
    }
    rep(i,2) {
        if(t[p.first+(int)pow(-1,i)][p.second] && s[p.first+(int)pow(-1,i)][p.second] == 0) {
            dfs(make_pair(p.first+(int)pow(-1,i),p.second));
        }
    }
}

int main() {
    while(1) {
        i_i m;
        scanf("%d %d",&w,&h);
        if(w == 0) break;
        FOR(i,1,h+1){
            char a[20];
            scanf("%s",a);
            FOR(j,1,w+1) {
                if(a[j-1] == '.') t[i][j] = 1; 
                if(a[j-1] == '@') m = make_pair(i,j);
            }
        }
        dfs(m);
        printf("%d\n",a);
        a = 0;
        rep(i,22) {
            rep(j,22) {
                t[i][j] = 0,s[i][j] =0;
            }
        }
    }
    return 0;
}

Measured memory_bytes: 2768
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <climits>
#include <cfloat>
#include <cstring>
#include <map>
#include <utility>
#include <set>
#include <iostream>
#include <memory>
#include <string>
#include <vector>
#include <list>
#include <algorithm>
#include <functional>
#include <sstream>
#include <complex>
#include <stack>
#include <queue>
#include <unordered_set>
#include <unordered_map>
#include <array>
#include <cassert>
#include <bitset>
using namespace std;
using LL = long long;

int N, K;
string S;
int A[123456];

int main() {
	cin >> N >> K;
	cin >> S;
	for (int i = 0; i < N; ++i)A[i] = (S[i] == '1');
	int now = 0;
	vector<int>G;
	if (S.front() == '0')G.push_back(0);
	while (now < N) {
		int j = now;
		while (j < N && A[j] == A[now]) {
			++j;
		}
		G.push_back(j - now);
		now = j;
	}
	if (S.back() == '0')G.push_back(0);
	int M = G.size();
	vector<int>imos;
	imos.push_back(0);
	for (int i = 0; i < M; ++i) {
		imos.push_back(imos.back() + G[i]);
	}
	K = 2 * K + 1;
	int ans = imos.back();
	if (M >= K) {
		ans = 0;
		for (int i = 0; i <= M - K; ++i) {
			if(i % 2 == 0)
				ans = max(ans, imos[i + K] - imos[i]);
		}
	}
	cout << ans << endl;
	return 0;
}

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

int main(){
	int n;
	cin >> n;
	int d[110];
	for(int i=0; i<n; i++) cin >> d[i];
	
	int ans;
	for(int i=0; i<n; i++){
		for(int j=0; j<n; j++){
			if(i==j) continue;
			else ans += d[i]*d[j];
		}
	}
	cout << ans/2 << endl;
}

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

using namespace std;
using ll=long long;
using ull=unsigned long long;

typedef pair<int,int>Pi;
typedef pair<long long,long long>P;
typedef pair<long long,P>PP;
typedef pair<P,P>PPP;

const long long MOD=1e9+7;
const long long INF=5e18;
const int di[4]={1,0,-1,0};
const int dj[4]={0,1,0,-1};

#define fr first
#define sc second
#define pb push_back
#define eb emplace_back
#define ALL(x) (x).begin(),(x).end()
#define ALLR(x) (x).rbegin(),(x).rend()

ll n,a[100010],b[100010];
ll c[100010];
ll d[100010];
ll asum,bsum,ans;
ll diff;
priority_queue<ll>pq;

void input(){
	cin>>n;
	for(int i=0;i<n;i++){
		cin>>a[i];
		asum+=a[i];
	}
	for(int i=0;i<n;i++){
		cin>>b[i];
		bsum+=b[i];
	}
	return;
}

void solve(){
	if(asum<bsum){
		cout<<-1<<endl;
		return;
	}
	for(int i=0;i<n;i++){
		if(a[i]<b[i]){
			c[i]=b[i];
			diff+=c[i];
			ans++;
		}
		else diff+=a[i];
	}
	for(int i=0;i<n;i++){
		if(a[i]>b[i]){
			d[i]=a[i]-b[i];
			pq.push(d[i]);
		}
	}
	diff-=asum;
	if(diff==0){
		cout<<ans<<endl;
		return;
	}
	while(!pq.empty()){
		ll s=pq.top();
		pq.pop();
		if(diff<s){
			ans++;
			cout<<ans<<endl;
			return;
		}
		else {
			ans++;
			diff-=s;
		}
	}
	return;
}

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

Measured memory_bytes: 3960
S = input()
N = len(S)

# 101010101010 か 010101010101 のどちらかになる。
A = str()
B = str()
if N%2 == 0:
  for i in range(int(N/2)):
    A += '10'
    B += '01'
else:
  for i in range(int((N-1)/2)):
    A += '01'
    B += '10'
  A += '0'
  B += '1'

# SとA,Bの差分：abs(S[i]-A[i])の和のminを取ればいい。
a = 0
b = 0
for i in range(N):
  a += abs(int(S[i])-int(A[i]))
  b += abs(int(S[i])-int(B[i]))

num = min(a, b)
print(num)

Measured memory_bytes: 3316