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

using namespace std;

#define int long long

const int INF = 1LL << 58;


int N, M, W, T;
map< string, int > beet;
int V[7], P[7], X[7], Y[7];
int dp[1 << 7][10001];
int visit[1 << 7][7], fact[1 << 7];
int dp2[10001];
vector< pair< int, int > > latte[7];

int get_dist(int a, int b, int c, int d)
{
  return (abs(a - c) + abs(b - d));
}

signed main()
{
  cin >> N >> M >> W >> T;
  for(int i = 0; i < M; i++) {
    string s;
    cin >> s >> V[i] >> P[i];
    beet[s] = i;
  }

  for(int i = 0; i < N; i++) {
    int L;
    cin >> L >> X[i] >> Y[i];
    for(int j = 0; j < L; j++) {
      string s;
      int v, w;
      cin >> s >> v;
      w = V[beet[s]];
      v = P[beet[s]] - v;
      latte[i].emplace_back(w, v);
    }
  }

  fill_n(*visit, (1 << 7) * 7, INF);
  for(int i = 0; i < N; i++) {
    visit[1 << i][i] = get_dist(0, 0, X[i], Y[i]);
  }
  for(int i = 0; i < 1 << N; i++) {
    for(int j = 0; j < N; j++) {
      if(dp[i][j] == INF) continue;
      for(int k = 0; k < N; k++) {
        if((i >> k) & 1) continue;
        visit[i | (1 << k)][k] = min(visit[i | (1 << k)][k], visit[i][j] + get_dist(X[j], Y[j], X[k], Y[k]));
      }
    }
  }

  fill_n(fact, 1 << 7, INF);
  for(int i = 0; i < 1 << N; i++) {
    for(int j = 0; j < N; j++) {
      fact[i] = min(fact[i], visit[i][j] + get_dist(X[j], Y[j], 0, 0));
    }
  }


  for(int i = 0; i < 1 << N; i++) {
    for(int j = 0; j < N; j++) {
      if((i >> j) & 1) {
        for(auto &p : latte[j]) {
          for(int k = 0; k <= W - p.first; k++) {
            dp[i][k + p.first] = max(dp[i][k + p.first], dp[i][k] + p.second);
          }
        }
      }
    }
  }

  for(int i = 0; i <= T; i++) {
    for(int j = 0; j < 1 << N; j++) {
      if(i + fact[j] > T) continue;
      dp2[i + fact[j]] = max(dp2[i + fact[j]], dp2[i] + dp[j][W]);
    }
  }

  cout << dp2[T] << endl;

}

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

using namespace std;

#define int long long
#define FR first
#define SC second
#define all(v) (v).begin(), (v).end()
#define rep(i, n) for(int i = 0; i < (int)(n); i++)
#define reps(i, f, n) for(int i = (int)(f); i < (int)(n); i++)
#define each(a, b) for(auto& a : b)

typedef pair<int, int> P;

const int inf = 1LL << 55;
const int mod = 1e9 + 7LL;

int extgcd(int a, int b, int &x, int &y)
{
  int d = a;
  if(b != 0) d = extgcd(b, a % b, y, x), y -= (a / b) * x;
  else x = 1, y = 0;
  return d;
}

int modinv(int a, int m)
{
  int x, y;
  extgcd(a, m, x, y);
  return (m + x % m) % m;
}

int f[200020];
void fact()
{
  f[0] = 1;
  for(int i = 1; i < 200020; i++) {
    f[i] = f[i-1] * i % mod;
  }
}

int combi(int n, int k, int m)
{
  if(k < 0 || n < k) return 0;
  return f[n] * modinv(f[k] * f[n - k] % m, m) % m;
}

int dp[2002][2002];

signed main()
{
  cin.tie(0);
  ios_base::sync_with_stdio(0);
  cout << fixed << setprecision(12);

  fact();
  
  int N, D, X;
  while(cin >> N >> D >> X, N) {
    memset(dp, 0, sizeof(dp));
    dp[0][0] = 1;
    for(int i = 0; i < min(D, N); i++) {
      for(int j = 0; j < N; j++) {
	dp[i+1][j+1] = (dp[i+1][j] + dp[i][j]) % mod;
	if(j+1 >= X) dp[i+1][j+1] = (dp[i+1][j+1] - dp[i][j+1-X] + mod) % mod;
      }
    }
    
    int ans = 0, nCk = 1;
    for(int i = 1; i <= min(N, D); i++) {
      // ans = (ans + dp[i][N] * combi(D, i, mod)) % mod;
      nCk = (nCk * ((D-i+1)%mod * modinv(i, mod)%mod)%mod) % mod;
      ans = (ans + dp[i][N] * nCk) % mod;
    }

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

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



void solve(vector<int>& a){
  int f[3]={};
  for(int i=0;i<a.size();i++){
    f[a[i]-1]=1;
  }
  if( (f[0]+f[1]+f[2])%2 == 1 )
    for(int i=0;i<5;i++) cout << "3\n";

  else if( f[0]==1 && f[1]==1 ){
    for(int i=0;i<5;i++){
      if(a[i] == 1) cout << "1\n";
      if(a[i] == 2) cout << "2\n";
    }
  }

  else if( f[1]==1 && f[2]==1 ){
    for(int i=0;i<5;i++){
      if(a[i] == 2) cout << "1\n";
      if(a[i] == 3) cout << "2\n";
    }
  }

  else if( f[0]==1 && f[2]==1 ){
    for(int i=0;i<5;i++){
      if(a[i] == 3) cout << "1\n";
      if(a[i] == 1) cout << "2\n";
    }
  }


}



int main(){
  bool f=true;
  while(1){
    vector<int> x;
    for(int i=0;i<5;i++){
      int tmp;
      cin >> tmp;
      if(i==0 && tmp == 0){
	f=0;
	break;
      }
      x.push_back(tmp);
    }
    if(!f) break;
    solve(x);
  }
}

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

class Main
{
    public static void main(String[] args)
    {
	Scanner sc = new Scanner(System.in);
	
	int a = sc.nextInt();
	int b = sc.nextInt();
	
	System.out.println(a*b + " " + ((a*2)+(b*2)));
    }
}

Measured memory_bytes: 25660
import java.util.*;

// さきほどのソースコードを提出したあと、やっていることはつまり
// 「すべての入力のXORを求めて、各入力とのXORを取っている」
// だと気づいた。つまり2進数変換とか一切不要。さっそく実装してみる。
public class Main {

	public static void main(String[] args) {

		// 入力
		Scanner sc = new Scanner(System.in);
		int n = Integer.parseInt(sc.next());
		long a[];
		a = new long[n];
		long XorOfAll = 0;

		// 入力を受けつつ、すべての入力どうしのXORも計算
		for (int i = 0; i < n; i++) {
			a[i] = Long.parseLong(sc.next());
			XorOfAll = XorOfAll ^ a[i];
		}

		// 各入力と XorOfAll とのXORがそのまま答え
		StringBuilder result = new StringBuilder(" ");
		for (int i = 0; i < n; i++) {
			long hoge = XorOfAll ^ a[i];
			result.append(" ").append(hoge);
		}

		System.out.println(result.toString().replace("  ", ""));
	}
}


Measured memory_bytes: 65296
#include<bits/stdc++.h>
using namespace std;
#define INF 100000000

int ans=2000000000;

void dfs(vector<bool>&visit,int rn,int total,vector<vector<int>> &cost,int now,vector<int>&r)
{
  int i;
  bool all_visit=true;
  for(i=0;i<rn;i++)if(!visit.at(i))all_visit=false;
  if(all_visit)
  {
    if(total<ans)ans=total;
  }
  else
  {
    for(i=0;i<rn;i++)
    {
      if(!visit.at(i))
      {
        visit.at(i)=true;
        dfs(visit,rn,total+cost.at(now).at(r.at(i)),cost,r.at(i),r);
        visit.at(i)=false;
      }
    }
  }
  return;
}

int main()
{
  int n,m,rn;cin>>n>>m>>rn;
  vector<int> r(rn);
  int i,j,k;
  for(i=0;i<rn;i++)cin>>r.at(i);
  vector<int> a(m);
  vector<int> b(m);
  vector<int> c(m);
  for(i=0;i<m;i++)cin>>a.at(i)>>b.at(i)>>c.at(i);
  vector<vector<int>> cost(n,vector<int>(n,INF));
  for(i=0;i<n;i++)cost.at(i).at(i)=0;
  for(i=0;i<m;i++)
  {
    a.at(i)--;
    b.at(i)--;
  }
  for(i=0;i<rn;i++)r.at(i)--;
  for(i=0;i<m;i++)
  {
    cost.at(a.at(i)).at(b.at(i))=c.at(i);
    cost.at(b.at(i)).at(a.at(i))=c.at(i);
  }
  for(k=0;k<n;k++)
  {
    for(i=0;i<n;i++)
    {
      for(j=0;j<n;j++)
      {
        cost.at(i).at(j)=min(cost.at(i).at(j),cost.at(i).at(k)+cost.at(k).at(j));
      }
    }
  }
  vector<bool>visit(rn,false);
  for(i=0;i<rn;i++)
  {
    visit.at(i)=true;
    dfs(visit,rn,0,cost,r.at(i),r);
    visit.at(i)=false;
    for(j=0;j<rn;j++)
    {
      visit.at(j)=false;
    }
  }
  cout<<ans<<endl;
}


Measured memory_bytes: 640
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<vector>
#include<map>
#include<set>
#include<string>
#include<queue>
#include<stack>
#include<complex>
#include<numeric>
using namespace std;
#define MOD 1000000007
#define MOD2 998244353
#define PRIM 3
#define INF (1<<29)
#define LINF (1LL<<60)
#define EPS (1e-10)
#define PI 3.1415926535897932384626433832795028
typedef long long Int;
typedef pair<Int, Int> P;
typedef long double Real;
typedef complex<Real> CP;

vector<char> ans_str(128, 0);
int cnt[256];


string chrs="";

string reals = "ax";

int cnter =0;
int query(string str){
    if(++cnter > 850)exit(1);
//    cout << cnter++ << endl;
    int ans = 0;
/*    for(int i = 0;i < str.size();i++)if(i >= reals.size() || reals[i] != str[i])ans++;
    return str.size() - ans;
*/    
    cout << "? " << str << endl << flush;
    cin >> ans;
    return str.size() - ans;
}

void answer(string str){
    cout << "! " << str << endl << flush;    exit(0);
}

int memo[256][129][129];

string ans;
int len;
void solve(string chars, int l, int r){
    if(l + 1 == r){
        ans[l] = chars[0];
        return;
    }
    int mid = (l + r) / 2;
    string tmp(len, chars[0]);
    int qsum = memo[chars[0]][l][r];
    for(int i = 1;i < chars.size();i++){
        char c = chars[i];
        for(int j = 0;j < mid;j++)tmp[j] = c;
        memo[c][l][mid] = query(tmp) - memo[c][0][l] - memo[chars[0]][r][len];
        qsum += memo[c][l][mid];
    }
    qsum -= mid - l;
    qsum /= chars.size();
    memo[chars[0]][mid][r] = qsum;
    memo[chars[0]][l][mid] = memo[chars[0]][l][r] - qsum;
    for(int i = 1;i < chars.size();i++){
        char c = chars[i];
        memo[c][l][mid] -= qsum;
        memo[c][mid][r] = memo[c][l][r] - memo[c][l][mid];
    }

    string lchars, rchars;
    
    for(int i = 0;i < chars.size();i++){
        char c = chars[i];
        memo[c][0][mid] = memo[c][0][l] + memo[c][l][mid];
        memo[c][mid][len] = memo[c][mid][r] + memo[c][r][len];
        if(memo[c][l][mid])lchars.push_back(c);
        if(memo[c][mid][r])rchars.push_back(c);
    }
    solve(lchars, l, mid);
    solve(rchars, mid, r);
}

int main(){
//    cin >> reals;
    string chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
    len = 0;
    set<int> cnts;
    string exist;
    for(auto c:chars){
        string all_c(128, c);
        cnt[c] = query(all_c);
        len += cnt[c];
        if(cnt[c])exist.push_back(c);
    }
    ans.resize(len);
    for(auto c:chars){
        memo[c][0][len]=cnt[c];
    }
    
    solve(exist, 0, len);
    answer(ans);
    return 0;
}

Measured memory_bytes: 4984
#include <bits/stdc++.h>
//#pragma GCC optimize("O3")

#define pb push_back
#define pii pair<int,int>
#define fi first
#define se second
#define pp pair<pii,pii>
#define ll long long
#define MOD 1000000007
#define mp make_pair
#define ins insert


const double PI= acos(-1);

#define yu using
#define don namespace
#define sei std

yu don sei;



int main()
{
	string s;
	cin>>s;
	for(int i=0;i<s.size()-8;i++)
	{
		cout<<s[i];
	}cout<<endl;
	
	return 0;
}

Measured memory_bytes: 256
#include <iostream>
using namespace std;
int main()
{
	int a,b;
	cin>>a>>b;
	if(a<b){
		cout<<"a < b"<<endl;
	}
	else if(a>b){
		cout<<"a > b"<<endl;
	}
	else if(a==b){
		cout<<"a == b"<<endl;
	}	
   return 0;
}

Measured memory_bytes: 1156
n=int(input())
p=list(map(int,input().split()))

count=0
for i in range(1,n-1,1):
  if (min(p[i-1],p[i],p[i+1])!=p[i]) and (max(p[i-1],p[i],p[i+1])!=p[i]):
    count = count + 1
  
print(count)

Measured memory_bytes: 3060
#include<bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i = 0; i < (int) n; i++)
using ll = long long;
template <class T>
using vt = vector<T>;
using vvi = vector<vector<int>>;

int main(){
  //type
  int n;
  cin >> n;
  vt<int> a(n);
  rep(i,n)
    cin >> a[i];

  //solve
  sort(a.begin(),a.end());
  reverse(a.begin(),a.end());
  int ans = 0;
  rep(i,n){
    if(i%2==0)
      ans += a[i];
    else
      ans -= a[i];
  }

  cout << ans << endl;


  return 0;
}


Measured memory_bytes: 256
import collections

N, L = map(int, input().split())
a = list(map(int, input().split()))
deq = collections.deque()
ans = []

for i in range(N):
    while deq and a[i] <= deq[-1][1]:deq.pop()
    deq.append((i,a[i]))
    ans.append(deq[0][1])
    if deq and deq[0][0] <=i-L+1:deq.popleft()
print(*ans[L-1:N])



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

using namespace std;

#define rep(i, n) for(int i = 0; i < (n); i++)
typedef pair<int, int> P;
typedef long long ll;
typedef unsigned long long ull;

int main(){
  int N;
  cin >> N;
  int a, sum = 0;
  rep(i, N){
    cin >> a;
    if(!(i%2)){
      if(a%2) sum++;
    }
  }

  cout << sum << endl;
}


Measured memory_bytes: 3616
#include<cstdio>
using namespace std;
int main() {
  int flag = 1;
  int flag2 = 1;
  int ans = 0;
  int c;
  int n;
  while ((c = getchar_unlocked()) >= '0') {
    n = c - '0';
    flag2 = flag;
    if (n == 5) {
      if (flag == 1) {
        flag = 0;
        ans += 5;
      } else {
        flag = -1;
        ans += 4;
      }
    } else if (n < 4) {
      if (flag == -1) {
        ans += n + 1;
      } else {
        ans += n;
      }
      flag = 1;
    } else if (n > 5) {
      if (flag == 1) {
        ans += 10 - n;
      } else {
        ans += 9 - n;
      }
      flag = -1;
    } else {
      if (flag == -1) {
        flag = 0;
        ans += 5;
      } else {
        flag = 1;
        ans += 4;
      }
    }
  }
  if (n > 5 || (n == 5 && flag2 != 1)){
    ++ans;
  }
  printf("%d\n", ans);
  return 0;
}

Measured memory_bytes: 128
#include<iostream>
#include<vector>
using namespace std;
using ll = long long;

int main(){
    int n,m;
    cin>>n>>m;
    vector<bool> a(n+2,true);
    vector<ll> dp(n+1);
    for(int i=1;i<=m;i++){
        int b;
        cin>>b;
        a[b]=false;
    }

    dp[0]=1;
    for(int i=0;i<=n;i++){
        if(a[i]){
            for(int j=1;j<=2&&i+j<=n;j++){
                if(a[i+j]){
                    dp[i+j]+=dp[i];
                    dp[i+j]%=1000000007;
                }
            }
        }
    }
    cout<<dp[n]<<endl;
}

Measured memory_bytes: 3984
#include <bits/stdc++.h>
#define ll long long
#define endl "\n"
#define vi vector<int>
#define vvi vector<vector<int>>
#define FASTIO ios_base::sync_with_stdio(NULL); cin.tie(NULL);
#define FOR(i,n) for(int i=0;i<n;i++)
#define FORE(i,a,b) for(int i=a;i<=b;i++)
#define pb push_back
#define MOD 1000000007

using namespace std;

void init(){
  #ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin) ;
  freopen("output.txt", "w", stdout) ;
  #endif
}

// code from below

void solve(){
	int n;
    cin>>n;
    vi arr(n),dp(n);
    FOR(i,n)
        cin>>arr[i];
    dp[n-1]=0;
    dp[n-2]=abs(arr[n-1]-arr[n-2]);
    for(int i=n-3;i>=0;i--){
        dp[i]=min(dp[i+1]+abs(arr[i]-arr[i+1]),dp[i+2]+abs(arr[i]-arr[i+2]));
    }
    cout<<dp[0]<<endl;
}

int main(){
    int t=1;
    // cin>>t;
    // cin.ignore(numeric_limits<streamsize>::max(),'\n'); 
    while(t--)
        solve();
}

Measured memory_bytes: 3980
#include<cstdio>
#include<algorithm>

using namespace std;

typedef long long ll;

#define INF 1LL << 61

ll n, s;

ll f(ll b, ll n){
	if(n < b) return n;
	return f(b, n / b) + n % b;
}

ll solve(){
	if(n == s) return n + 1;
	for(ll b = 2; b * b <= n; b++) if(f(b, n) == s) return b;
	if(n < s) return -1;
	ll res = INF;
	for(ll i = 1; i * i < n; i++){
		if((n - s) % i != 0) continue;
		ll b = (n - s + i) / i;
		if(b <= n && f(b, n) == s) res = min(res, b);
	}
	return res == INF ? -1 : res;
}

int main(){
	scanf("%lld %lld", &n, &s);
	printf("%lld\n", solve());
	return 0;
}

Measured memory_bytes: 128
# coding: utf-8

def main():
    N = int(input())
    A = list(map(int, input().split()))
    e = 0
    for a in A:
        if a % 2 == 0:
            e += 1

    ans = 3 ** N - 2 ** e

    print(ans)

if __name__ == "__main__":
    main()


Measured memory_bytes: 9004
s = input().split('T')
gx, gy = tuple(map(int, input().split()))

dir = 0
xmoves = []
ymoves = []
for moves in s:
    if dir % 2 == 0:
        xmoves.append(len(moves))
    else:
        ymoves.append(len(moves))
    dir += 1

curx, cury = xmoves.pop(0), 0

for move in sorted(xmoves, reverse=True):
    if curx > gx:
        curx -= move
    else:
        curx += move

for move in sorted(ymoves, reverse=True):
    if cury > gy:
        cury -= move
    else:
        cury += move

if curx == gx and cury == gy:
    print('Yes')
else:
    print('No')

Measured memory_bytes: 3188
h,w=gets.chomp.split(" ").map { |e| e.to_i }
s=[]
h.times do |i|
  s[i] = gets.chomp.split("")
end

$dp = Array.new(h).map { Array.new(w,0)}

def judge(i, j, h, w)
  if 0 <= i && i <= h-1 && 0 <= j && j<= w-1
    return true
  else
    return false
  end
end

def bfs(s, i, j, h, w)
  queue = [[i,j]]
  $dp[i][j] = 1
  ary = [[1, 0],[-1, 0],[0, 1],[0, -1]]
  white = 0
  black = 1

  until queue.empty?
    temp = queue.pop
    x = temp[0]
    y = temp[1]

    for a in ary do
      if judge(x+a[0], y+a[1], h, w)
        if $dp[x+a[0]][y+a[1]] == 0 && s[x][y] != s[x+a[0]][y+a[1]]
          queue.push([x+a[0],y+a[1]])
          $dp[x+a[0]][y+a[1]] = 1
          if s[x+a[0]][y+a[1]] == "#"
            black += 1
          else
            white += 1
          end
        end
      end
    end
  end
  return black*white
end

ans = 0

for i in 0..h-1 do
  for j in 0..w-1 do
    if $dp[i][j] == 0 && s[i][j] == "#"
      ans += bfs(s, i, j, h, w)
    end
  end
end

puts ans


Measured memory_bytes: 19324
class UnionFind:
    def __init__(self, n):
        self.par = [i for i in range(n+1)]
        self.rank = [0] * (n+1)
        self.size = [1] * n

    def find(self, x):
        if self.par[x] == x:
            return x
        else:
            self.par[x] = self.find(self.par[x])
            return self.par[x]

    def same_check(self, x, y):
        return self.find(x) == self.find(y)

    def union(self, x, y):
        x = self.find(x)
        y = self.find(y)
        if x == y:
            return
        if self.rank[x] > self.rank[y]:
            self.par[y] = x
            self.size[x] += self.size[y]
            self.size[y] = 0
        else:
            self.par[x] = y
            self.size[y] += self.size[x]
            self.size[x] = 0
            if self.rank[x] == self.rank[y]:
                self.rank[y] += 1

n, m = map(int,input().split())
edges = []
for i in range(m):
    a,b,_ = map(int,input().split())
    edges.append([a-1,b-1])

uf = UnionFind(n)
point = n*(n-1)//2
ans = []

while edges:
    ans.append(point)
    a, b = edges.pop()
    pa = uf.find(a)
    pb = uf.find(b)
    uf.union(a,b)

score = 0
for i in range(n):
    if uf.size[i] != 0:
        score += 1

print(score)

Measured memory_bytes: 102528
#include <bits/stdc++.h>
#define FOR(i,a,b) for (int i = (a); i < (b); ++i)
#define rep(i,n) for (int i = (0); i < (n); ++i)
#define rng(x) (x).begin(),(x).end()
#define rrng(x) (x).rbegin(),(x).rend()
#define debug(x) cout << #x << " = " << (x) << endl;
#define fi first
#define se second
#define pb push_back
#define eb emplace_back
#define sz(x) (int)(x).size()
#define bn(x) ((1<<x)-1)
#define dup(x,y) (((x)+(y)-1)/(y))
#define uni(x) x.erase(unique(rng(x)),x.end())
#define SP << " " <<
using namespace std;
using ll = long long;
using vi = vector<int>;
using vvi = vector<vi>;
using vll = vector<ll>;
using pii = pair<int, int>;
using tiii = tuple<int, int, int>;
const int mod = 1000000007;
const double EPS = 1e-9;
const int INF = 1<<30;
const ll INFLL = 1LL<<60;
//cout << fixed << setprecision(10);

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; }

int main () {
  int n, y;
  cin >> n >> y;
  // a + b + c = n
  // 10000*a + 5000*b + 1000*c = y
  for (int a = 0; a <= n; ++a) {
    for (int b = 0; a + b <= n; ++b)  {
      int c = n - a - b;
      int total = 10000*a + 5000*b + 1000*c;
      if (total == y) {
        cout << a SP b SP c << endl;
        return 0;
      }
    }
  }
  cout << -1 SP -1 SP -1 << endl;
  return 0;
}

Measured memory_bytes: 256
## tasks https://atcoder.jp/contests/dp/tasks
## TODO

def solution():
    # result array for calculation for optimal cost for every stone
    res = [[-1 for x in range(3)] for y in range(n)]
    res[0] = a[0]
    for i in range(1,n):
        # going through the all days
        for j in range(3):
            # calucaltion for every possible actions, but skipping the same activity
            # picked current activity is j, let us get options from non j and get max of them
            # it will be a cost for j
            # do it for every

            #searching for best previous happines
            bph = -1
            for o in range(3):
                #skipping the same activity
                if o==j:
                    continue
                bph = max(bph, res[i-1][o])
            #print("res = {} a = {}".format(res, a))
            #print("i={} j={}".format(i, j))
            res[i][j] = max(res[i][j], bph + a[i][j])

    return max(res[-1][0],res[-1][1],res[-1][2])


if __name__ == "__main__":
    n = int(input())

    a = [[None for x in range(3)] for y in range(n)]

    for i in range(n):
        row = list(map(int, input().split(" ")))
        for j in range(3):
            a[i][j] = row[j]

    #print("n = {} a = {}".format(n, a))
    if n == 1:
        print(max(a[0][0],a[0][1],a[0][2]))
    else:
        print(solution())


Measured memory_bytes: 45616
#include <bits/stdc++.h>
#define REP(i, n) for(int i = 0; i < n; i++)
#define REPR(i, n) for(int i = n; i >= 0; i--)
#define FOR(i, m, n) for (int i = m; i < n; i++)
#define INF 2e9
#define ALL(v) v.begin(), v.end()
using namespace std;
typedef long long ll;
typedef vector<ll> vll;
typedef vector<vll> vvll;


int main()
{
    ll a, b;
    cin >> a >> b;
    if (b % a) {
        cout << b - a << endl;
    } else {
        cout << a + b << endl;
    }
    return 0;
}

Measured memory_bytes: 256
"""
1. The initial character of S is an uppercase A.
2. There is exactly one occurrence of C between the third character from the beginning and the second to last character (inclusive).
3. All letters except the A and C mentioned above are lowercase.
"""
import collections
def a(r): return r[0] == 'A'
def b(r): return collections.Counter(r[2:-1])['C'] == 1
def c(r): return all([l in set(['A', 'C']) or l.lower() ==l for l in r ])
def f(r):  return a(r) and b(r) and c(r)

print 'AC' if f(raw_input()) else 'WA'

Measured memory_bytes: 85932
#include <stdio.h>

int main(void) {
    int K, S;
    scanf("%d %d", &K, &S);

    int x, y, z;
    int result = 0;
    for (x = 0; x <= K; x++) {
        for (y = 0; y <=K; y++) {
            z = S - x - y;
            if (z >= 0 && z <= K) {
                result++;
            }
        }
    }
    
    printf("%d\n", result);

    return 0;
}

Measured memory_bytes: 128
#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
int main(){
    ios_base::sync_with_stdio(false);
    cin.tie(0);cout.tie(0);
    ll n,k,i,j,x,ans=0;
    cin>>n>>k;
    map<ll,vector<ll> >m1;
    for(i=1;i<=k;i++){
        cin>>x;
        while(x--){
            cin>>j;
            m1[j].push_back(i);
        }
    }
    for(i=1;i<=n;i++){
        if(m1[i].size()==0)
            ans+=1;
    }
    cout<<ans<<endl;
    return 0;
}


Measured memory_bytes: 3644
#include<iostream>
#include<string>

using namespace std;
void   LCS(string s1,string s2,int dp[][3001]){
	
    //memset(dp,0,sizeof dp);
	int a=s1.length();
	int b=s2.length();
	for(int i=0;i<=a;i++){
			dp[i][0]=0;
	}

for(int i=0;i<=b;i++){
			dp[0][i]=0;
	}
	for(int i=1;i<=a;i++){
		for(int j=1;j<=b;j++){
        
			if(s1[i-1]==s2[j-1]){
				dp[i][j]=1+dp[i-1][j-1];
				
			}
		
		else{
			dp[i][j]=max(dp[i-1][j],dp[i][j-1]);
		}
	}
    }
 //return dp[a][b];  
}
int main() {
     string s1,s2;
     cin>>s1>>s2;
     int dp[3001][3001];
   
    int a=s1.length();
	int b=s2.length();
   
    
    LCS(s1,s2,dp);
    char ans [dp[a][b]];
    int x=a;
    int y=b;
    int i=dp[a][b]-1;
    while(i>=0){
        if(s1[x-1]==s2[y-1]){
            ans [i]=s1[x-1];
            x--;
            y--;
            i--;
        }
        else{
            if(dp[x-1][y]>=dp[x][y-1]){
                    x--;
            }
            else{
                y--;
            }
        }
    }
    for(int j=0;j<dp[a][b];j++){
        cout<<ans[j];
    }
   // cout<<LCS(s1,s2,dp);
	return 0;
}

Measured memory_bytes: 35456
A = int(input())
B = int(input())
C = int(input())
X = int(input())
count=0
sum=0
for a in range(0, A+1):
    for b in range(0, B+1):
        for c in range(0, C+1):
            sum = 500*a + 100*b +50*c
            if X == sum:
                count+=1
            sum=0
print(count) 

Measured memory_bytes: 3064
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> P;
typedef pair<int,P> P1;
typedef pair<P,P> P2;
#define pu push
#define pb push_back
#define mp make_pair
#define eps 1e-7
#define INF 1000000000
#define mod 1000000007
#define fi first
#define sc second
#define rep(i,x) for(int i=0;i<x;i++)
#define repn(i,x) for(int i=1;i<=x;i++)
#define SORT(x) sort(x.begin(),x.end())
#define ERASE(x) x.erase(unique(x.begin(),x.end()),x.end())
#define POSL(x,v) (lower_bound(x.begin(),x.end(),v)-x.begin())
#define POSR(x,v) (upper_bound(x.begin(),x.end(),v)-x.begin())
int main(){
	int n; cin>>n;
	if(n == 1) puts("Hello World");
	else{
		int a,b; cin>>a>>b;
		cout<<a+b<<endl;
	}

}

Measured memory_bytes: 256
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

class Program {
    static void Main(string[] args) {
        string[] input = Console.ReadLine().Split(' ');

        StringBuilder s = new StringBuilder();
        s.Append(input[1]);
        s.Append(input[0]);
        Console.WriteLine(s);
    }
}


Measured memory_bytes: 11092
#include <stdio.h>


void Hoshina_check(char str[],int num){
  int i;

  if(str[num+1] != 'o')return;
  if(str[num+2] != 's')return;
  if(str[num+3] != 'h')return;
  if(str[num+4] != 'i')return;
  if(str[num+5] != 'n')return;
  if(str[num+6] != 'o')return;

  str[num+6]='a';
  return;
}

int main(void){
  char str[1001];
  int n_i;
  int n;
  int i;
  int c;

  scanf("%d",&n);

  for(n_i=0;n_i<n;n_i++){

    for(i=0;i<1001;i++){
      str[i]=0;
    }

    i=0;
    if(n_i==0)c=getchar();
    c=getchar();
    while(c != '\n'){
      str[i]=c;
      i++;
      c=getchar();
    }

    i=0;
    while(str[i] != '\n'){
      if(str[i] == 'H'){
        Hoshina_check(str,i);
      }
      i++;
    }
    printf("%s\n",str);

  }
  return 0;
}

Measured memory_bytes: 584
import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		int y = sc.nextInt();
		for(int i = 0 ; i <= n ; i++) {
			for(int j = 0 ; i + j <= n ; j++) {
				int k = n - (i + j);
				if(k >= 0 && 10000 * i + 5000 * j + 1000 * k == y) {
					System.out.println(i + " " + j + " " + k);
					return;
				}
			}
		}
		System.out.println(-1 + " " + -1 + " " + -1);
	}
}


Measured memory_bytes: 21972
#include <stdio.h>
#include <math.h>

int main() {
  int n,i,j,l;
  double s[10020];
  double a2,m,a;
  
  while(1) {
    m=0;
    a2 = 0;
    scanf("%d" , &n);
    if(n == 0) {
      break;
    }

    for(i=0;i<n;i++) {
      scanf("%lf", &s[i]);
	m=m+s[i];
	//printf("m[%d] = %lf\n",i ,m);
    }
    m=m/n;

    for(i=0; i<n; i++) {
      a2 =a2+(s[i]-m)*(s[i]-m);
    }
    a2 = a2/n;

    printf("%.11lf\n",sqrt(a2));

  }

  return 0;

}


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

int main() {
  int a,b;
  cin >> a >> b;
  int count = 0;
  for(int i = a; i <= b; i++) {
    bool f = true;
    string n = to_string(i);
    for(int j = 0; j < n.size(); j++) {
      if(n[j] != n[n.size()-j-1]) {
        f = false;
        break;
      }
    }
    if(f)
      count++;
  }
  cout << count << endl;
}


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

#define endl "\n"
#define INF (int) 9e18
#define HELL (int) (1e9 + 7)
#define int long long
#define double long double
#define uint unsigned long long
#define deb(x) cerr << #x << " => " << x << "\n"
#define deba(x) cerr << #x << "\n"; for (auto a : x) cerr << a << " "; cerr << "\n";
#define debm(x) cerr << #x << "\n"; for (auto a : x){for(auto b : a) cerr << b << " "; cerr << "\n";}
#define getMat(x, n, m, val) vector<vector<int>> x(n, vector<int> (m, val))
#define fastio ios_base :: sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr);
#define pout cout << fixed << setprecision(10)

int fastpow(int a, int b, int m) {
    int res = 1; a %= m; while (b > 0) {
        if (b & 1) res = (res * a) % m; a = (a * a) % m; b >>= 1;
    } return res;
}

int32_t main() { fastio;
    
    string s; cin >> s;
    if (s[2] == s[3] and s[4] == s[5]) {
    	cout << "Yes" << endl;
    } else {
    	cout << "No" << endl;
    }

    return 0;
}

Measured memory_bytes: 256
#include<iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <math.h>
#include <functional>
#include <iomanip>
#include <bitset>
#include <numeric>
#include <queue>
#include <map>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
const long long mod = 1000000007;
typedef long long ll;
typedef pair<int, double> P;
const vector<int> di = {-1, 0, 1, 0};
const vector<int> dj = { 0, 1, 0, -1 };



int main() {

	int n, k, x, y;
	cin >> n >> k >> x >> y;

	if (n > k) {
		int ans = x * k + y * (n - k);
		cout << ans << endl;
	}
	else {
		int ans = x * n;
		cout << ans << endl;
	}

	
	
	return 0;
}

Measured memory_bytes: 256
#include<cmath>
#include<string>
#include<fstream>
#include<iostream>
#define MAX(a,b) (a>b? a:b)
#define MIN(a,b) (a>b? b:a)
#define rep(i,n) for(int i=0;i<(int)(n);i++)

using namespace std;

int main(){
	int a, b;
	cin >> a >> b;
	if(a%2==0||b%2==0) cout << "Even" << endl;
	else cout << "Odd " << endl;
	return 0;
}

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

ll total_price(std::vector<pair<ll,int> > v, int m)
{
  int tmp = 0;
  ll price = 0;
  int cur = 0;
  while(tmp < m)
  {
    if(tmp+v[cur].second < m)
    {
      tmp += v[cur].second;
      price += v[cur].first * v[cur].second;
    } else {
      int last = m-tmp;
      price += v[cur].first * last;
      return price;
    }
    cur++;
  }
  return -1;
}

int main()
{
  int n,m;
  cin >> n >> m;
  std::vector<pair<ll,int> > v;
  for(int i=0; i<n; i++)
  {
    ll price;
    int stock;
    cin >> price >> stock;
    v.push_back(make_pair(price,stock));
  }
  sort(v.begin(),v.end());
  cout << total_price(v,m) << endl;
  return 0;
}


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

using namespace std;

// when range [l, r), has property notP~P, want first P.
// when return r, means not found.
template <typename T>
T bs_first(T l, T r, function<bool (T)> f) {
    assert(l < r);
    T mid;
    while (l != r) {
        mid = l + (r-l)/2;
        if (f(mid)) {
            r = mid;
        }else {
            l = mid + 1;
        }
    }
    return r;
}

void solve() {
    int n; cin >> n;
    vector<int> a(n);
    for (auto& x: a) {
        cin >> x;
    }
    int res = bs_first<int>(1, n+1, [&](int k){
            if (k == 1) {
                for (int i = 1; i < n; i++) {
                    if (a[i-1] >= a[i]) return false;
                }
                return true;
            }
            vector<pair<int, int>> stk;
            for (int i = 1; i < n; i++) {
                if (a[i-1] >= a[i]) {
                    int pos = a[i];
                    while (!stk.empty() && stk.back().first > pos) {
                        stk.pop_back();
                    }
                    while (!stk.empty() && stk.back().first == pos && stk.back().second == k) {
                        pos--;
                        stk.pop_back();
                    }
                    if (pos == 0) return false;
                    if (!stk.empty() && stk.back().first == pos) {
                        stk.back().second++;
                    } else stk.emplace_back(pos, 2);
                }
            }
            return true;
            });
    cout << res;
}

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    solve();
    cout << endl;
}


Measured memory_bytes: 1024
n, k=gets.split.map(&:to_i)
a=gets.split.map(&:to_i)

freq=Array.new(n+1){0}
a.each{|e| freq[e]+=1}
li=[]
freq.each{|e| li.push(e)}
li=li.sort
tot=0
while li.size>k
  tot+=li.shift
end
puts tot

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

int main(){
  int N; cin >> N;
  string S; cin >> S;
  
  for(int i=0; i<S.size(); i++){
    int tmp=S[i]-'A';
    tmp=(tmp+N)%26;
    S[i]=(char)('A'+tmp);
  }
  cout << S << endl;
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
int n,a[55],ans;
int main()
{
	//freopen(".in","r",stdin);
	//freopen(".out","w",stdout);
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	cin>>n;
	for (int i=1;i<=n;++i) cin>>a[i];
	for (int i=1;i<=n;++i)
		for (int j=i+1;j<=n;++j)
			ans+=a[i]*a[j];
	cout<<ans;
	return 0;
}

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

using ll = long long;
using ld = long double;

int main()
{
    int n,m,c,ans=0;
    cin>>n>>m>>c;
    vector<int> a(m),b(m);
    for(int i = 0; i < m; i++)
    {
        cin>>b[i];
    }
    for(int ii = 0; ii < n; ii++)
    {
        int tmp=c;
        for(int i = 0; i < m; i++)
        {
            cin>>a[i];
            tmp+=a[i]*b[i];
        }
        if(tmp>0)ans++;
    }
    
    cout<<ans<<endl;
    return 0;
}

Measured memory_bytes: 256
#include<iostream>
#include <bits/stdc++.h>
#include<stdio.h>
#include <stdlib.h>
#include<string>
#include<queue>
#include<algorithm>
#include<math.h>

using namespace std;
int main(){
	int a,b;
	char op;
	while(1){
		cin>>a>>op>>b;
		if(op=='?'){
			return 0;
		}else if(op=='+'){
			cout<<a+b<<endl;
		}else if(op=='-'){
			cout<<a-b<<endl;
		}else if(op=='*'){
			cout<<a*b<<endl;
		}else if(op=='/'){
			cout<<a/b<<endl;
		}
	}
	
}


Measured memory_bytes: 3104
N = int(input())
H = list(map(int,input().split()))

dp = [0]*N
for i in range(N):
  if i == 0:
    dp[i] = 0
  elif i == 1:
    dp[i] = abs(H[i]-H[i-1])
  else:
    dp[i] = min((dp[i-1]+abs(H[i]-H[i-1])),dp[i-2]+abs(H[i]-H[i-2]))
print(dp[-1])

Measured memory_bytes: 52208
#include<bits/stdc++.h>
//ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
//clock_t start=clock();clock_t end=clock();cout<<(double)(end-start)/CLOCKS_PER_SEC<<endl;
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef unsigned int ui;
typedef pair<int,int> pii;
typedef pair<pii,int> ppii;
typedef pair<int,pii> pipi;
typedef pair<ll,ll> pll;
typedef pair<pll,ll> ppll;
typedef pair<ll,pll> plpl;
typedef pair<pii,pii> P;
typedef tuple<ll,ll,ll> tl;
typedef pair<double,double> pdd;
typedef vector<vector<ll>> mat;
ll mod=1000000007;
ll mod2=998244353;
ll mod3=1000003;
ll mod4=998244853;
ll mod5=1000000009;
ll inf=1LL<<62;
double pi=3.141592653589793238462643383279L;
double eps=1e-14;
#define rep(i,m,n) for(ll i=m;i<n;i++)
#define rrep(i,n,m) for(ll i=n;i>=m;i--)
#define srep(itr,st) for(auto itr=st.begin();itr!=st.end();itr++)
#define mrep(itr,mp) for(auto itr=mp.begin();itr!=mp.end();itr++)
#define Max(a,b) a=max(a,b)
#define Min(a,b) a=min(a,b)
typedef pair<pii,pii> e;
int dh[4]={1,-1,0,0};
int dw[4]={0,0,1,-1};
int ddh[8]={-1,-1,-1,0,0,1,1,1};
int ddw[8]={-1,0,1,-1,1,-1,0,1};
struct custom_hash {
    static uint64_t splitmix64(uint64_t x) {
        // http://xorshift.di.unimi.it/splitmix64.c
        x += 0x9e3779b97f4a7c15;
        x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
        x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
        return x ^ (x >> 31);
    }

    size_t operator()(uint64_t x) const {
        static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count();
        return splitmix64(x + FIXED_RANDOM);
    }
};
#define umh unordered_map<ll,ll,custom_hash>
ll gcd(ll a,ll b){
    if(a<b)swap(a,b);
    if(b==0)return a;
    if(a%b==0)return b;
    return gcd(b,a%b);
}
ll lcm(ll a,ll b){
    ll c=gcd(a,b);
    return a*b/c;
}
ll Pow(ll n,ll k){
    ll ret=1;
    ll now=n;
    while(k>0){
        if(k&1)ret*=now;
        now*=now;
        k/=2;
    }
    return ret;
}
ll beki(ll n,ll k,ll md){
  ll ret=1;
  ll now=n;
    now%=md;
  while(k>0){
    if(k%2==1){
      ret*=now;
      ret%=md;
    }
    now*=now;
    now%=md;
    k/=2;
  }
  return ret;
}
ll gyaku(ll n,ll md){
  return beki(n,md-2,md);
}
ll popcount(ll n){
    ll ret=0;
    ll u=n;
    while(u>0){
        ret+=u%2;
        u/=2;
        
    }
    return ret;
}

int main(){
    ios::sync_with_stdio(false);cin.tie(0);
    ll n;cin>>n;
    ll k;cin>>k;
    ll ri=0,le=0;
    rep(i,0,k){
        le+=i;
        ri+=n-i;
    }
    ll ans=0;
    rep(i,k,n+2){
        ans+=ri-le+1;
        ans%=mod;
        le+=i;
        ri+=n-i;
    }
    cout<<ans<<endl;
}


Measured memory_bytes: 3636
import sys
sys.setrecursionlimit(10 ** 6)
input = sys.stdin.readline
N, M = map(int, input().split())
G = [[] for i in range(N)]
for i in range(M):
    a, b = map(int, input().split())
    a, b = a - 1, b - 1
    G[a].append(b)
    G[b].append(a)

col = [-1] * N


def dfs(u, p, k):
    if col[u] != -1:
        if col[u] == k:
            return
        else:
            print(N * (N - 1) // 2 - M)
            exit()
    col[u] = k
    for w in G[u]:
        if w == p:
            continue
        dfs(w, u, k ^ 1)


dfs(0, -1, 0)
n, m = 0, 0
for i in range(N):
    if col[i] == 1:
        n += 1
    else:
        m += 1

print(n * m - M)

Measured memory_bytes: 235612
#include <bits/stdc++.h>
#define all(x) (x).begin(), (x).end()
typedef long long ll;
#define MOD 1000000007
using namespace std;
int main() {
    int a, b, c;
    cin >> a >> b >> c;
    if(a == b && a != c) {
        cout << "Yes" << endl;
        return 0;
    }
    if(a == c && b != c) {
        cout << "Yes" << endl;
        return 0;
    }
    if(b == c && a != c) {
        cout << "Yes" << endl;
        return 0;
    }
    cout << "No" << endl;
}

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

int main() {
    int a,b,c,x;
    cin>>a>>b>>c>>x;
    int ans = 0;
    for (int i=0; i<=a; i++) {
        for (int j=0; j<=b; j++) {
            for (int k=0; k<=c; k++) {
                if (500*i + 100*j + 50*k == x) ans++;
            }
        }
    }
    cout << ans << endl;
    return 0;
}

Measured memory_bytes: 256
// Sky's the limit :)
#include<bits/stdc++.h>
using namespace std;
#define int long long

signed main()
{
    ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    
    //int t = 1; cin >> t; while(t--)
    {
        string s;
        cin >> s;

        int q, f = 1;
        cin >> q;
        deque<char> dq;
        while(q--) {
            int x;
            cin >> x;
            if(x == 1)
                f = !f;
            else {
                int l;
                char c;
                cin >> l >> c;
                l--;

                if(l == f)
                    s += c;
                else
                    dq.push_front(c);
            }
        }

        string t;
        while(!dq.empty()) {
            char c = dq.front();
            dq.pop_front();
            t += c;
        }

        s = t + s;
        if(f == 1)
            cout << s;
        else {
            reverse(s.begin(), s.end());
            cout << s << '\n';
        }

    }
    
    cerr << "Time elapsed : " << 1.0 * clock() / CLOCKS_PER_SEC << " sec \n";
    return 0;
}



Measured memory_bytes: 1304
#include<bits/stdc++.h>
using namespace std;
int main(){
  int n,x; cin>>n;
  bool f=true;
  for(int i=0;i<n;i++)
  {
    cin>>x;
    if(x%2==0){
      if(x%3!=0&&x%5!=0)
        f=false;
    }
  }
  if(f)
    cout<<"APPROVED";
  else
    cout<<"DENIED";
}





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

typedef pair<int, int> P;

int n;
int a[101010];
vector<P> b;

int main()
{
  cin >> n;
  for (int i = 0; i < n; i++){
    cin >> a[i];
    b.push_back(P(a[i], i));
  }

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

  int cnt = 0;
  for (int i = 0; i < b.size(); i++){
    if (b[i].second % 2 != i % 2) cnt++;
  }

  cout << cnt / 2 << endl;
}



Measured memory_bytes: 1656
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for(int i = 0; i < (n); i++)
typedef long long ll;
const ll M = 1e9 + 7;

int main(void){
  int n, m;
  scanf("%d %d", &n, &m);
  set<int> as, bs;
  rep(i, n) {int a; scanf("%d", &a); as.insert(a - 1);}
  rep(i, m) {int b; scanf("%d", &b); bs.insert(b - 1);}
  if (as.size() != n || bs.size() != m){printf("%d", 0); return 0;}

  ll ans = 1, x = 0, y = 0, res = 0;
  for (int i = n * m - 1; i >= 0; i--){
    bool fa = as.count(i), fb = bs.count(i);
    if (fa && fb) res += x + y, ++x, ++y;
    else if (fa) (ans *= y) %= M, res += y - 1, ++x;
    else if (fb) (ans *= x) %= M, res += x - 1, ++y;
    else (ans *= res) %= M, res--;
  }

  printf("%lld\n", ans);

  return 0;
}


Measured memory_bytes: 3852
#include<bits/stdc++.h>
using namespace std;
#define fs first
#define sc second
#define pb push_back
#define mp make_pair
#define eb emplace_back
#define ALL(A) A.begin(),A.end()
#define RALL(A) A.rbegin(),A.rend()
typedef long long ll;
typedef pair<ll,ll> P;
const ll mod=1000000007;
const ll LINF=1LL<<60;
const int INF=1<<30;
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }

int main(){
	int n,m;cin>>n>>m;
	vector<ll> a(n);
	for(int i = 0; i < n; i++) {
		cin>>a[i];
	}
	vector<bool> b(n,true);
	for(int i = 0; i < m; i++) {
		int x,y;cin>>x>>y;
		if (a[x-1] >= a[y-1]){
			b[y-1] = false;
		}
		if (a[x-1] <= a[y-1]){
			b[x-1] = false;
		}
	}
	int ans = 0;
	for(int i = 0; i < n; i++) {
		ans += b[i];
	}
	cout << ans << endl;
	return 0;
}

Measured memory_bytes: 4000
a,b,q=map(int,input().split())
s=[0]*a
t=[0]*b
x=[0]*q
for i in range(a):
  s[i]=int(input())
for i in range(b):
  t[i]=int(input())
import bisect
for i in range(q):
  x=int(input())
  ids=bisect.bisect_left(s,x)
  idt=bisect.bisect_left(t,x)
  mi=14857363987569617
  if ids < a:
    if idt < b:
      mi=min(mi,min([abs(s[ids]-x)+abs(s[ids]-t[idt]),
                    abs(t[idt]-x)+abs(t[idt]-s[ids])]))
    if idt > 0:
      mi=min(mi,min([abs(s[ids]-x)+abs(s[ids]-t[idt-1]),
                    abs(t[idt-1]-x)+abs(t[idt-1]-s[ids])]))
  if ids > 0:
    if idt < b:
      mi=min(mi,min([abs(s[ids-1]-x)+abs(s[ids-1]-t[idt]),
                    abs(t[idt]-x)+abs(t[idt]-s[ids-1])]))
    if idt > 0:
      mi=min(mi,min([abs(s[ids-1]-x)+abs(s[ids-1]-t[idt-1]),
                    abs(t[idt-1]-x)+abs(t[idt-1]-s[ids-1])]))
  print(mi)

Measured memory_bytes: 13924
n, x, y = gets.chomp.split(" ").map(&:to_i)
x -= 1
y -= 1
ans = Array.new(n, 0)
INF = 100000000

def push(v, d)
    return if @dist[v] != INF
    @que.push(v)
    @dist[v] = d
end

n.times do |i|
    @dist = Array.new(n, INF)
    @que = []
    push(i, 0)
    while !@que.empty? do
        v = @que.shift 
        d = @dist[v]
        push(v-1, d+1) if v-1 >= 0
        push(v+1, d+1) if v+1 < n
        push(y, d+1) if v == x
        push(x, d+1) if v == y
    end
    @dist.each do |v|
        ans[v] += 1
    end
end
ans.map!{|i| i/2}
1.upto(n){|i| puts ans[i]}

Measured memory_bytes: 26776
/******************************************
* NICK : nikhilsinghh59 *
******************************************/
#include<bits/stdc++.h>
using namespace std;
#define ll long long int 
#define watch(x) cout << (#x) << " is " << (x) << endl
#define endl "\n"

#define PB push_back
#define F first
#define S second
#define all(c) (c).begin(),(c).end()
const int M = 1e9+7;

int main()
{   
    // #ifndef ONLINE_JUDGE
    // freopen("input.txt", "r", stdin);
    // freopen("output.txt", "w", stdout);
    // #endif

    int n;
    cin >> n;

    ll ar[n];
    map<ll , ll>mp;
    for(int i=0 ; i<n ; i++) {
        cin >> ar[i];

        mp[ar[i]]++;
    }

    map<ll , ll>ct;
    ll sum=0;
    for(auto it : mp) {
        ct[it.first] = (it.second*(it.second-1))/2;
        sum+=ct[it.first];
    }

    for(int i=0 ; i<n ; i++) {
        ll ans = sum - ct[ar[i]] + ((mp[ar[i]]-1)*(mp[ar[i]] - 2))/2;
        cout << ans << endl;
    }
    
    return 0;
}

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

using namespace std;

typedef long long ll;

const double eps = 0.00001;
const int MOD = 1000000007;
const int INF = 2147480555;
const ll LINF = 9223372000000000555;
const int N = 1550555;
const int M = 525;

void solve() {
    string s;
    cin >> s;

    int sum = 0;
    for (int i = 0; i < s.size(); i++){
        sum += s[i] - '0';
    }

    cout << (sum%9 == 0 ? "Yes" : "No");

}


int main() {
    ios_base::sync_with_stdio(false);
#ifdef dddxxz
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#else

#endif

    int T = 1;
    //cin >> T;

    while (T--) {
        solve();
    }

    return 0;
}

Measured memory_bytes: 3764
N, K= map(int, input().split())
h = list(map(int, input().split()))

for i in range(K):
  h.append(float('inf'))

dp = [0]
for i in range(N+K):
  dp.append(float('inf'))

for i in range(N):
  for j in range(1,K+1):
    
    dp[i+j] = min(dp[i+j],dp[i] + abs(h[i]-h[i+j]))
    #print(dp)

print(dp[N-1])

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

int main(){
    int n, b, f, r, v;
    int room[5][4][11] ={};
    
    cin >>n;
    for(int a=1; a<=n; a++){
        cin >> b >> f >> r >> v;
        room[b][f][r] += v;
    }
    
    for(int i=1; i<=4; i++){
        for(int j=1; j<=3; j++){
            for(int k=1; k<=10; k++){
                cout << " " << room[i][j][k];
            }
            cout << endl;
        }
        if( i != 4){
            cout << "####################" << endl;
        }
        
    }
    return 0;
}


Measured memory_bytes: 3072
# -*- coding: utf-8 -*-
"""
Created on Sun Jan 20 20:50:54 2019

@author: Yamazaki Kenichi
"""

s = int(input())

a = [s]

ans = -1
counta = 1
while ans ==-1:
    if a[-1]%2==0:
        a.append(a[-1]//2)
    else:
        a.append(a[-1]*3+1)
    counta +=1
    ans = counta if a.count(a[-1])>=2 else ans
       
print(ans)

Measured memory_bytes: 3064
n = int(input())
AB = [tuple(map(int,input().split())) for _ in range(n)]
all_b = sum([b for _,b in AB])
AB = sorted([a + b for a,b in AB],reverse=True)
ans = 0
for i in range(n):
    if i % 2 == 0:
        ans += AB[i]
print(ans - all_b)

Measured memory_bytes: 21580
#include <bits/stdc++.h>  // clang-format off
using Int = long long;
#define REP_(i, a_, b_, a, b, ...) for (Int i = (a), lim##i = (b); i < lim##i; i++)
#define REP(i, ...) REP_(i, __VA_ARGS__, __VA_ARGS__, 0, __VA_ARGS__)
struct SetupIO { SetupIO() { std::cin.tie(nullptr), std::ios::sync_with_stdio(false), std::cout << std::fixed << std::setprecision(13); } } setup_io;
#ifndef dump
#define dump(...)
#endif  // clang-format on

/**
 *    author:  knshnb
 *    created: Sun Apr 26 21:18:52 JST 2020
 **/

template <class T, bool directed = true> struct Dijkstra {
    struct Edge {
        int to;
        T cost;
    };
    std::vector<std::vector<Edge>> g;
    Dijkstra(int n) : g(n) {}
    void add_edge(int u, int v, T cost) {
        g[u].push_back({v, cost});
        if (!directed) g[v].push_back({u, cost});
    }
    std::vector<T> run(int s) {
        std::vector<T> dist(g.size(), std::numeric_limits<T>::max() / 2);
        // {d, v}
        std::priority_queue<std::pair<T, int>, std::vector<std::pair<T, int>>, std::greater<std::pair<T, int>>> q;
        q.push({0, s});
        while (!q.empty()) {
            std::pair<T, int> p = q.top();
            q.pop();
            int v = p.second;
            if (dist[v] <= p.first) continue;
            dist[v] = p.first;
            for (const Edge& e : g[v]) {
                if (dist[e.to] <= p.first + e.cost) continue;  // 定数倍枝刈り
                q.emplace(p.first + e.cost, e.to);
            }
        }
        return dist;
    }
};

signed main() {
    Int n, m, S;
    std::cin >> n >> m >> S;
    const Int K = 5001;
    auto enc = [&](Int v, Int j) {
        assert(j < K);
        return K * v + j;
    };
    Dijkstra<Int> dj(n * K);
    REP(_, m) {
        Int u, v, a, b;
        std::cin >> u >> v >> a >> b;
        u--, v--;
        REP(j, a, K) {
            dj.add_edge(enc(u, j), enc(v, j - a), b);
            dj.add_edge(enc(v, j), enc(u, j - a), b);
        }
    }
    REP(i, n) {
        Int c, d;
        std::cin >> c >> d;
        REP(j, K) {
            if (j + c >= K) break;
            dj.add_edge(enc(i, j), enc(i, j + c), d);
        }
    }
    auto d = dj.run(enc(0, std::min(S, K - 1)));
    REP(i, 1, n) {
        std::cout << *min_element(d.begin() + enc(i, 0), d.begin() + enc(i + 1, 0)) << "\n";
    }
}


Measured memory_bytes: 48664