akhauriyash/Code-Regression huggingface dataset:
#include <iostream>
#include <string>
#include <vector>
#include <stdio.h>
#include <algorithm>
#define _USE_MATH_DEFINES
#include <cmath>
#include <numeric>
#include <iomanip>
#include <deque>
#include <tuple>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <unordered_map>
#include <sstream>
#include <cstring>

#define rep(i, x) for (int i = 0; i < (int)(x); i++)
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define all(a) (a).begin(), (a).end()
#define F first
#define S second
#define x first
#define y second
#define pb push_back
#define pf push_front

using ll = long long;
using namespace std;

ll M = 1000000007;
typedef pair<int, int> P;

typedef pair<int, int> P;

bool ps1(const P &forall_a_in_P, const P &forall_b_in_P)
{ //first昇順
    return forall_a_in_P.first < forall_b_in_P.first;
}
bool ps2(const P &forall_a_in_P, const P &forall_b_in_P)
{ //first降順
    return forall_a_in_P.first > forall_b_in_P.first;
}
bool ps3(const P &forall_a_in_P, const P &forall_b_in_P)
{ //second昇順
    return forall_a_in_P.second < forall_b_in_P.second;
}
bool ps4(const P &forall_a_in_P, const P &forall_b_in_P)
{ //second降順
    return forall_a_in_P.second > forall_b_in_P.second;
}
bool IsPrime(int num)
{
    if (num < 2) return false;
    else if (num == 2) return true;
    else if (num % 2 == 0) return false; // 偶数はあらかじめ除く

    double sqrtNum = sqrt(num);
    for (int i = 3; i <= sqrtNum; i += 2)
    {
        if (num % i == 0)
        {
            // 素数ではない
            return false;
        }
    }

    // 素数である
    return true;
}
int main()
{
    int x;
    cin>>x;
    while(1){
        if(IsPrime(x)){
            cout<<x<<endl;
            break;
        }else{
            x++;
        }
    }
    
}

Measured memory_bytes: 256
import re

S = input()

def main():
    m = re.match(r'^A[a-z](?P<mid>[a-zC]+)[a-z]$', S)
    if m is None:
        ans = "WA"
    else:
        mid = m.group("mid")
        if mid.count("C") == 1:
            ans = "AC"
        else:
            ans = "WA"
    print(ans)

if __name__ == "__main__":
    main()

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

int main() {
  string s,t;
  cin>>s>>t;
  
  int i,n=s.size(),m=t.size();
  for(i=0;i<m+n;i++){
    if(i%2==0)
      cout<<s.at(i/2);
    else cout<<t.at((i-1)/2);
  }
    
}

Measured memory_bytes: 256
#include <cstdio>
#include <climits>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <queue>
#include <deque>
#include <stack>
#include <map>
#include <list>
#include <set>
#include <bitset>
#include <iterator>
#define rep(i, n) for(long long i = 0; i < (long long)(n); i++)
#define debug(x)  cout << #x << " = " << (x) << endl;
using namespace std;
typedef long long int LL;
typedef unsigned long long int ULL;

int main() {
    int n;
    cin >> n;
    vector<int> d(n);
    rep(i, n) {
        cin >> d[i];
    }
    sort(d.begin(), d.end());
    int mid_left = d[n / 2 - 1];
    int mid_right = d[n / 2];
    if (mid_left == mid_right) {
        cout << 0 << endl;
    } else {
        cout << mid_right - mid_left << endl;
    }
    return 0;
}


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

typedef long long llong;
// -2.1e-9 <= int <= 2.1e9

int main()
{
    string S;
    cin >> S;
    for (size_t i = 0; i < S.size(); i++) {
        S[i] = 'x';
    }
    cout << S << endl;
}


Measured memory_bytes: 256
def run_length_compress(S):
    res = [[S[0], 1]]
    for c in S[1:]:
        if c == res[-1][0]:
            res[-1][1] += 1
        else:
            res.append([c, 1])
    return res


N, K = map(int, input().split())
S = input()
cs = run_length_compress(S)
M = len(cs)

ans = 0
tot = 0
right = 0
zeros = 0
for left in range(M):
    while right < M and zeros + (cs[right][0] == "0") <= K:
        if cs[right][0] == "0":
            zeros += 1
        tot += cs[right][1]
        right += 1

    ans = max(ans, tot)

    if left == right:
        right += 1
    else:
        if cs[left][0] == "0":
            zeros -= 1
        tot -= cs[left][1]
print(ans)



Measured memory_bytes: 17796
#include        "algorithm"
#include        "iostream"
#include        "numeric"
#include        "iomanip"
#include        "cstring"
#include        "math.h"
#include        "bitset"
#include        "string"
#include        "vector"
#include        "ctime"
#include        "queue"
#include        "stack"
#include        "map"
#include        "set"

#include        "ext/pb_ds/assoc_container.hpp" // Common file
#include        "ext/pb_ds/tree_policy.hpp" // Including tree_order_statistics_node_update
#include        "ext/pb_ds/detail/standard_policies.hpp"

using namespace std;
using namespace __gnu_pbds;


#define          f first
#define          lgn 25
#define          endl '\n'
#define          sc second
#define          N (int)2e5+5
#define          pb push_back
#define          mod 1000000007
#define          ld long double
#define          vi vector<int>
#define          eb emplace_back
#define          vpii vector<pii>
#define          mii map<int,int>
#define          int long long 
#define          pii pair<int,int>
#define          pq priority_queue
#define          BLOCK (int)sqrt(N)
#define          test(x) while(x--)
#define          all(x) begin(x),end(x)
#define          allr(x) rbegin(x),rend(x)
#define          fo(i,a,n) for(int i=a;i<n;i++)
#define          rfo(i,n,a) for(int i=n;i>=a;i--)
#define          FAST ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define          time() cerr << "Time : " << (double)clock() / (double)CLOCKS_PER_SEC << "s\n"
#define          PI acos(-1.0)
#define 		 bug(...)       __f (#__VA_ARGS__, __VA_ARGS__)

typedef tree< int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update > 
OS ;

template <typename Arg1>
void __f (const char* name, Arg1&& arg1) { cout << name << " : " << arg1 << endl; }
template <typename Arg1, typename... Args>
void __f (const char* names, Arg1&& arg1, Args&&... args)
{
     const char* comma = strchr (names + 1, ',');
     cout.write (names, comma - names) << " : " << arg1 << " | "; __f (comma + 1, args...);
}

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

const int inf = 0x3f3f3f3f;
const int INF = 0x3f3f3f3f3f3f3f3f;

int n,m,k,q;
string s;
vi adj[N];
int vis[N],par[N],a[N];

void go()
{
	cin >> s >> n;

	string aage , piche;

	int flip = 0;

	while( n-- )
	{
		int x; 
		cin >> x;

		if( x == 1 )
		{
			flip ^= 1;
		}
		else
		{
			int y;
			char ch;
			cin >> y >> ch;

			if( flip )
			{
				( y == 1 ) ? piche.pb(ch) : aage.pb(ch);
			}
			else
			{
				( y == 1 ) ? aage.pb(ch) : piche.pb(ch);
			}
		}
	}

	reverse(all(aage));
	// reverse( all( piche) );
	string ans = aage + s + piche;

	if( flip ) reverse( all ( ans ) );

	cout << ans << endl;
}

int32_t main()
{
	//INP(); 
	FAST;     
	int t=1; 
	// cin>>t;
	test(t) go();
}

 

Measured memory_bytes: 6636
import itertools

D, G = map(int, input().split())
P = [0 for _ in range(D)]
C = [0 for _ in range(D)]
for i in range(D):
    p, c = map(int, input().split())
    P[i] = p
    C[i] = c

ans = 10 ** 10
for mask in itertools.product((0, 1), repeat=D):
    myscore = 0
    soloved_num = 0
    incomplete_index = 0
    for i, j in enumerate(mask):
        if j == 0:
            incomplete_index = i
        else:
            myscore += P[i] * (i + 1) * 100 + C[i]
            soloved_num += P[i]
    if myscore < G:
        for n in range(P[incomplete_index]):
            myscore += (incomplete_index + 1) * 100
            soloved_num += 1
            if myscore >= G:
                break
    if myscore >= G:
        ans = min(ans, soloved_num)

print(ans)


Measured memory_bytes: 3188
w, h, n = input().split()
lx ,ly ,hx ,hy = 0, 0, int(w), int(h)
for i in range(int(n)):
    S = list(map(int, input().split()))
    if S[2]==1 and lx<S[0]:
        lx = S[0]
    elif S[2]==2 and hx>S[0]:
        hx = S[0]
    elif S[2]==3 and ly<S[1]:
        ly = S[1]
    elif S[2]==4 and hy>S[1]:
        hy = S[1]
ans = int(hx-lx)*int(hy-ly) 
if hx-lx<=0 or hy-ly<=0:
    print(0)
else: print(ans)



Measured memory_bytes: 3064
N,P=map(int,input().split())
S=input()
s=[0 for i in range(N+1)]
a=[0 for i in range(N+1)]
a[0]=(10**0)%P
for i in range(N):
    A=a[i]
    a[i+1]=(A*(10%P))%P
    s[i+1]=(s[i]+A*int(S[N-i-1]))%P
d=[0 for i in range(P)]
for i in range(len(s)):
    d[s[i]]+=1
ans=0
if P!=2 and P!=5:
    for D in d:
        ans+=D*(D-1)//2
    print(ans)
elif P==2:
    for i in range(N):
        if int(S[N-1-i])%2==0:
            ans+=(N-i)
    print(ans)
else:
    for i in range(N):
        if int(S[N-1-i])%5==0:
            ans+=(N-i)
    print(ans)

Measured memory_bytes: 56568
N = int(input())
x_list = list(map(int, input().split()))
x_max = max(x_list)

min_ans = 10 ** 7
for i in range(1, x_max+1):
    ans = 0
    for x in x_list:
        ans += (i - x) ** 2
    min_ans = min(min_ans, ans)
    
print(min_ans)

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

int main(){
  ll K, S;
  cin >> K >> S;
  ll ans = 0;
  
  for(int i = 0; i <= K; i++){
    for(int j = 0; j <= K; j++){
      if(i + j <= S && i + j >= S - K) ans++;
    }
  }
  
  cout << ans << endl;

}

Measured memory_bytes: 256
use std::io::*;
use std::str::FromStr;

fn main() {
    let n: usize = read();
    let k: usize = read();
    if k == 1 {
        println!("0");
    } else {
        let nokori = n - k;
        println!("{}", nokori);
    }
}

fn read<T: FromStr>() -> T {
    let stdin = stdin();
    let stdin = stdin.lock();
    let token: String = stdin
        .bytes()
        .map(|c| c.unwrap() as char)
        .skip_while(|c| c.is_whitespace())
        .take_while(|c| !c.is_whitespace())
        .collect();
    token.parse().ok().unwrap()
}


Measured memory_bytes: 4352
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>

using namespace std;

int main()
{
    long int count = 0;
    long int N;
    cin >> N;
    
    vector<long int> A(N+1);
    vector<long int> B(N);
    
    for(int i=0; i<N+1; i++){
        long int a;
        cin >> a;
        A[i] = a;
    }
    
    for(int i=0; i<N; i++){
        long int b;
        cin >> b;
        B[i] = b;
    }
    
    long int C = 0;
    for(int i=0; i<N; i++){
        if(B[i] + C > A[i]){
            count += A[i];
            C = min((B[i]+C - A[i]),B[i]);
        }
        else{
            count += B[i]+C;
            C = 0;
        }
    }
    count += min(A[N],C);
    
    cout << count << endl;
    return 0;
}


Measured memory_bytes: 1792
_S = list(input())
T = list(input())

N = len(_S)
M = len(T)

candidates = []

for i in range(N - M + 1):
    if _S[i] == '?' or _S[i] == T[0]:
        S = _S.copy()
        flg = True
        for j in range(M):
            if S[i + j] == '?':
                S[i + j] = T[j]
            elif S[i + j] != T[j]:
                flg = False
                break
        if flg:
            for k in range(N):
                if S[k] == '?':
                    S[k] = 'a'
            candidates.append(''.join(S))

if len(candidates) == 0:
    print('UNRESTORABLE')
else:
    print(min(candidates))


Measured memory_bytes: 9088
class Dice:
    def __init__(self, d):
        self.dice = d
        self.top = d[0]
        self.north = d[4]
        self.west = d[3]
        self.south = d[1]
        self.east = d[2]
        self.bottom = d[5]

    def move(self, d):
        if d == "N":
            self.move_north()
        elif d == "W":
            self.move_west()
        elif d == "S":
            self.move_south()
        elif d == "E":
            self.move_east()

    def move_north(self):
        self.top, self.south, self.bottom, self.north \
            = self.south, self.bottom, self.north, self.top

    def move_south(self):
        self.top, self.south, self.bottom, self.north \
            = self.north, self.top, self.south, self.bottom

    def move_east(self):
        self.top, self.east, self.bottom, self.west \
            = self.west, self.top, self.east, self.bottom

    def move_west(self):
        self.top, self.east, self.bottom, self.west \
            = self.east, self.bottom, self.west, self.top

if __name__ == "__main__":
    import re

    nums = [int(n) for n in re.split(r"\s+", input().strip())]
    dice = Dice(nums)

    for d in list(input().strip()):
        dice.move(d)

    print(dice.top)



Measured memory_bytes: 6572
import java.util.Scanner;

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

        String thisContest = sc.next();

        if (thisContest.equals("ABC")) {
            System.out.println("ARC");
        } else {
            System.out.println("ABC");
        }
    }
}

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

using namespace std;

const int INF = 1 << 30;

int main() {
  int a, b, c, d;
  cin >> a >> b >> c >> d;
  cout << abs(a - c) + abs(b - d) - __gcd(abs(c - a), abs(b - d)) << endl;
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define f(i, x, n) for(int i = x; i < (int)(n); ++i)
#define mod 1000000007
#define speed ios::sync_with_stdio(0); cin.tie(0);
int const N = 2 * 1e5+10;
vector<int> v;
vector<int> v1;
int a[N] , t[N] , answer[N];
int main(){
	speed
	ll a , b , k;
	cin >> a >> b >> k;
	f(i,0,k){
		if(i%2 == 0){
			if(a%2==1)a--;
			b+=a/2;
			a/=2;
		}else {
			if(b%2==1)b--;
			a+=b/2;
			b/=2;
		}
	}
	cout << a << " " << b << endl;
	return 0;
}

Measured memory_bytes: 384
// #define _GLIBCXX_DEBUG // for STL debug (optional)
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <string>
#include <cstring>
#include <deque>
#include <list>
#include <queue>
#include <stack>
#include <vector>
#include <utility>
#include <algorithm>
#include <map>
#include <set>
#include <complex>
#include <cmath>
#include <limits>
#include <cfloat>
#include <climits>
#include <ctime>
#include <cassert>
#include <numeric>
#include <fstream>
#include <functional>
#include <bitset>
using namespace std;
using ll = long long int;
using int64 = long long int;
 
template<typename T> void chmax(T &a, T b) {a = max(a, b);}
template<typename T> void chmin(T &a, T b) {a = min(a, b);}
template<typename T> void chadd(T &a, T b) {a = a + b;}
 
int dx[] = {0, 0, 1, -1};
int dy[] = {1, -1, 0, 0};
const int INF = 1LL << 29;
const ll LONGINF = 1LL << 60;
const ll MOD = 1000000007LL;
 
int main() {
    int N, M; cin >> N >> M;

    vector<int> p1, p0;
    for(int i=M; i>=1; i--) (i % 2 ? p1 : p0).emplace_back(i);

    vector< pair<int, int> > ans;
    int ptr = 0, ma = 0;
    for(auto p : p1) {
        ans.emplace_back(ptr, ptr + p);
        chmax(ma, ptr + p);
        ptr++;
    }
    ptr = ma + 1;
    for(auto p : p0) {
        ans.emplace_back(ptr, ptr + p);
        chmax(ma, ptr + p);
        ptr++;
    }

    for(auto e : ans) {
        printf("%d %d\n", e.first + 1, e.second + 1);
    }
    return 0;
}


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


using namespace std;

#define M 1000000007
#define pb push_back
#define f first
#define s second
#define rep(i, st, ed) for(int i=st; i<ed; i++)
#define repn(i, st, ed) for(int i=st; i<=ed; i++)
#define repb(i, ed, st) for(int i=ed; i>=st; i--)
#define all(v) v.begin(),v.end()
typedef long long ll;
typedef unsigned long long int llt;
typedef long double ld;

const long double PI  =3.141592653589793238463;
const int N = 1010;
const ll INF = 1LL << 60;

template<class T> void chmax(T &a,T b) { if (a<b) a=b;}
template<class T> void chmin(T &a,T b) { if (a>b) a=b;}



// Main Code
map<char, vector<int>> mp;
set<string> v;
int main()
{
	string s;
	int k;
	cin>>s;
	cin>>k;
	rep(i, 0, s.length())
		mp[s[i]].pb(i);
	int kz = k+1;
	for(auto x: mp)
	{
		char ch = x.f;
		vector<int> pos = x.s;
		bool fir = true;
		for(auto p: pos)
		{
			string ss = "";
			int kk = k+1;
			rep(i, p, s.length())
			{
				ss += s[i];
				v.insert(ss);
				kk--;
				if(kk == 0) break;
			}
		}
		kz--;
		if(kz == 0) break;
	}
	for(auto x: v)
	{
		k--;
		if(k == 0){ 
			cout<<x<<endl;
			break;
		}
	}
}


Measured memory_bytes: 768
#include <bits/stdc++.h>
using namespace std;
int main(){
  //体力の高いモンスターの順にソート
  //k番目の要素から体力を足し算して出力
  int N ,K;
  cin >>N>>K;
  int h[200010];
  for (int i= 0;i<N; ++i)cin>>h[i];
  sort (h, h+N,greater<int>());

  long sum =0;
  for (int i = K;i<N;++i)sum += h[i];
  cout<<sum<<endl;


}


Measured memory_bytes: 1024
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define all(v) v.begin(), v.end()
#define int long long
//#define _GLIBCXX_DEBUG

signed main() {
  int a, b, x; cin >> a >> b >> x;

  int ans = (a%x + b-a)/x;
  if(a%x == 0) ans++;

  cout << ans << endl;

}

Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
#define FOR(i,k,n) for(int i = (int)(k); i < (int)(n); i++)
#define REP(i,n) FOR(i,0,n)
#define ALL(a) a.begin(), a.end()
#define MS(m,v) memset(m,v,sizeof(m))
typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
typedef vector<string> vs;
typedef pair<int, int> pii;
const ll MOD = 998244353;
template<class T> T &chmin(T &a, const T &b) { return a = min(a, b); }
template<class T> T &chmax(T &a, const T &b) { return a = max(a, b); }
template<class T>
istream& operator >> (istream& is, vector<T>& v)
{
	for (auto &i : v) is >> i;
	return is;
}
template<class T>
ostream& operator<<(ostream& os, vector<T>& v)
{
	const string delimiter = "\n";
	REP(i, v.size())
	{
		os << v[i];
		if (i != v.size() - 1) os << delimiter;
	}
	return os;
}
/*--------------------template--------------------*/

int n;
string s;
vi can_r, can_b;
ll dp[4444][4444];

ll solve(int p, int cntr)
{
	if (dp[p][cntr] >= 0) return dp[p][cntr];
	if (p == n * 2) return 1;
	ll res = 0;
	int r = can_r[p] - cntr;
	int b = can_b[p] - (p - cntr);
	if (r > 0) res += solve(p + 1, cntr + 1);
	if (b > 0) res += solve(p + 1, cntr);
	return dp[p][cntr] = res % MOD;
}

int main()
{
	cin.sync_with_stdio(false); cout << fixed << setprecision(10);
	cin >> s;
	n = s.size();
	MS(dp, -1);
	can_r.resize(n * 2);
	can_b.resize(n * 2);
	can_r[0] = s[0] - '0';
	REP(i, n - 1) can_r[i + 1] = can_r[i] + (s[i + 1] - '0');
	REP(i, n) can_b[i] = 2 * (i + 1) - can_r[i];
	FOR(i, n, n * 2)
	{
		can_r[i] = can_r[i - 1];
		can_b[i] = can_b[i - 1];
	}
	cout << solve(0, 0) << endl;
	return 0;
}

Measured memory_bytes: 154880
#include <bits/stdc++.h>
#define PI 3.14159265359
typedef long long int ll;
typedef unsigned long long int ull;
using namespace std;
typedef vector<pair<ll,ll> > vpii;
typedef vector<ll> vi;
const ll INF=1e13+7;
void input(ll *a,ll n){
    for(ll i=0;i<n;i++)cin>>a[i];
}
ll lcm(ll a,ll b){return (a*b)/__gcd(a,b);}
bool isprime(ll n){
    if(n==1)return false;
    if(n==2 || n==3)return true;
    for(ll i=2;i<=sqrt(n);i++){
        if(n%i==0)return false;
    }
    return true;
}
//ll x[100001]={0};
ll a[100001]={0};
ll b[100001]={0};

//bool c[1000001]={0};
//vector<pair<ll,ll> > adj[100001];
//vector<ll> adj[200001];
//vpii edges;
//bool visited[200001]={0};
//bool color[200001]={0};
//ll dist[100001]={0};
ll binary_expon(ll a,ll b){
    ll res=1;
    while(b){
        if(b%2)res=(res*a)%INF;
        a=(a*a)%INF;
        b/=2;
    }
    return res%INF;
}

void sieve(ll n,bool *a)
{
    for (ll p=2; p*p<=n; p++){
        if (!a[p]){
            for (ll i=p*p;i<=n;i+=p)
                a[i]=true;
        }
    }
}

ll fact(ll n){
    ll res=1;
    for(ll i=1;i<=n;i++)res*=i;
    return res;
}

double distance(ll x1,ll y1,ll x2,ll y2){
    return (sqrt((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1)));
}

/*void tree_dfs(ll curr,ll prev){
    for(auto u:adj[curr]){
        if(u!=prev){
            tree_dfs(u,curr);
        }
    }
}*/

/*void dfs(ll x,ll y){
    if(x<0 || y<0 || x>=n || y>=m)return;
    //if(grid[x][y]!=color)return;
    if(visited[x][y] || Map[x][y]=='#'){
        //f=true;
        return;
    }
    visited[x][y]=true;
    Map[x][y]='#';
    //if(px!=(x+1) || py!=y)
        dfs(x+1,y);
    //if(px!=(x-1) || py!=y)
        dfs(x-1,y);
    //if(px!=(x) || py!=y+1)
        dfs(x,y+1);
    //if(px!=(x) || py!=y-1)
        dfs(x,y-1);
}*/

/*bool isVowel(char c){
    return(c=='a'||c=='e'||c=='i'||c=='o'||c=='u');
}*/
//ll grid[1001][1001]={0};
/*void dfs(ll s){
    if(visited[s])return;
    visited[s]=true;
    for(auto u:adj[s]){
        dfs(u);
    }
}*/
//set<pair<ll,ll> >edges;
/*vector<ll> z(string s) {
    ll n=s.length();
    vector<ll> z(n);
    ll x=0,y=0;
    for(ll i=1;i<n;i++) {
        z[i]=max(0ll,min(z[i-x],y-i+1));
        while(i+z[i]<n&&s[z[i]]==s[i+z[i]]){
            x = i; y = i+z[i]; z[i]++;
        }
     }
    return z;
}*/

ll dp[100001][3]={0};

void solve(){
    ll n;
    cin>>n;

    for(ll i=0;i<n;i++){
        ll a,b,c;
        cin>>a>>b>>c;
        if(i==0){
            dp[i][0]=a;
            dp[i][1]=b;
            dp[i][2]=c;
        }
        else{
            dp[i][0]=a+max(dp[i-1][1],dp[i-1][2]);
            dp[i][1]=b+max(dp[i-1][0],dp[i-1][2]);
            dp[i][2]=c+max(dp[i-1][0],dp[i-1][1]);
        }
    }

    cout<<max(dp[n-1][0],max(dp[n-1][1],dp[n-1][2]));
}

int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    #ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    #endif
    //sieve(1000001,c);
    ll t=1;
    //cin>>t;
    for(ll i=0;i<t;i++){
        solve();
    }
}


Measured memory_bytes: 6040
#include <bits/stdc++.h>
#define ll long long
#define ull unsigned ll
#define pii pair<ll, ll> 
#define pb push_back
#define mod (1e9+7)
#define debug(x) cerr<< #x << " is " <<x<<endl;
#define debug2(x) cerr<<#x<< " is\n"; for (auto &i:x) {cout<<i<<' '; } cout<<endl;
#define f first
#define s second
using namespace std;

const int inf = 1e9+5;



int main(){
    ios::sync_with_stdio(false); cin.tie(0);
    int n, W; cin>>n>>W;
    vector<ll> dp(W+1);
    for (int i=0; i<n; i++){
    	ll w, v; cin>>w>>v;
    	vector<ll> ndp(W+1);
    	for (int j=0; j<=W; j++){
    		if (j>=w) ndp[j]=max(dp[j],dp[j-w]+v);
    		else ndp[j]=dp[j];
    	}
    	dp=ndp;
    }
    cout<<*max_element(dp.begin(), dp.end())<<endl;

}

Measured memory_bytes: 1804
# coding: utf-8
N = [int(s) for s in input().split(' ')]

is1 = False
is9 = False
is7 = False
is4 = False

for num in N:
    if num == 1:
        is1 = True
    elif num == 9:
        is9 = True
    elif num == 7:
        is7 = True
    elif num == 4:
        is4 = True
if is1 and is9 and is7 and is4:
    print("YES")
else:
    print("NO")
    

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

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

#define ALL(a)  (a).begin(),(a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define EB emplace_back
#define MP make_pair
#define SZ(a) int((a).size())
#define EACH(i,c) for(typeof((c).begin()) i=(c).begin(); i!=(c).end(); ++i)
#define EXIST(s,e) ((s).find(e)!=(s).end())
#define SORT(c) sort((c).begin(),(c).end())

#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n)  FOR(i,0,n)

#define FF first
#define SS second
template<class S, class T>
istream& operator>>(istream& is, pair<S,T>& p){
  return is >> p.FF >> p.SS;
}

const double EPS = 1e-10;
const double PI  = acos(-1.0);
const LL MOD = 1e9+7;

LL gcd(LL x, LL y){
  if(x == y) return x;
  if(x < y) swap(x,y);
  if( y == 0) return -x;
  return 2*y * (x/y) + gcd(y,x%y);
}

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

  LL N, X; cin >> N >> X;
  cout << N+gcd(N-X,X) << endl;
  
  return 0;
}


Measured memory_bytes: 256
class Tree:
    __slots__ = ['id', 'p', 'left', 'right']
    def __init__(self, id):
        self.id = id
        self.left = -1
        self.right = -1
    def setc(self, c):
        if self.left == -1:
            self.left = c
        else:
            self.right = c
    def __str__(self):
        return f"node {self.id}: left = {self.left}, right = {self.right}"
n = int(input())
tree = tuple(Tree(i) for i in range(n + 1))
pretree = list(map(int, input().split()))
m = {a:i for i, a in enumerate(pretree)}
inotree = list(map(int, input().split()))
root = pretree[0]
def treeSet(A, id):
    if A == []:
        return -1
    elif len(A) == 1:
        return A[0]
    min_A_index = A.index(id)
    global tree
    if 0 < min_A_index:
        min_A = min(A[:min_A_index], key=lambda x: m[x])
        left = treeSet(A[:min_A_index], min_A)
        tree[id].left = left
    if 0 < len(A) - min_A_index - 1:
        min_A = min(A[min_A_index + 1:], key=lambda x: m[x])
        right = treeSet(A[min_A_index + 1:], min_A)
        tree[id].right = right
    return id
treeSet(inotree, root)
ans = []
def postorder_tree_walk(id):
    if tree[id].left != -1:
        postorder_tree_walk(tree[id].left)
    if tree[id].right != -1:
        postorder_tree_walk(tree[id].right)
    global ans
    ans.append(id)
postorder_tree_walk(root)
print(*ans)



Measured memory_bytes: 5624
#include <iostream>
#include <algorithm>
#include <complex>
#include <utility>
#include <vector>
#include <string>
#include <queue>
#include <tuple>
#include <cmath>
#include <bitset>
#include <cctype>
#include <set>
#include <map>
#include <numeric>
#include <functional>
#define _overload3(_1,_2,_3,name,...) name
#define _rep(i,n) repi(i,0,n)
#define repi(i,a,b) for(ll i=ll(a);i<ll(b);++i)
#define rep(...) _overload3(__VA_ARGS__,repi,_rep,)(__VA_ARGS__)
#define all(x) (x).begin(),(x).end()
#define PRINT(V) cout << V << "\n"
#define SORT(V) sort((V).begin(),(V).end())
#define RSORT(V) sort((V).rbegin(), (V).rend())
using namespace std;
using ll = long long;
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; }
inline void Yes(bool condition){ if(condition) PRINT("Yes"); else PRINT("No"); }
template<class itr> void cins(itr first,itr last){
    for (auto i = first;i != last;i++){
        cin >> (*i);
    }
}
template<class itr> void array_output(itr start,itr goal){
    string ans = "",k = " ";
    for (auto i = start;i != goal;i++) ans += to_string(*i)+k;
    if (!ans.empty()) ans.pop_back();
    PRINT(ans);
}
ll gcd(ll a, ll b) {
    return a ? gcd(b%a,a) : b;
}
const ll INF = 1e18;
const ll MOD = 1000000007;
typedef pair<ll,ll> P;
const ll MAX = 20000;
constexpr ll nx[8] = {1,0,-1,0,-1,-1,1,1};
constexpr ll ny[8] = {0,1,0,-1,-1,1,-1,1};
int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    ll n;
    cin >> n;
    vector<ll> a(n),b(n,0);
    cins(all(a));
    rep(i,1,n){
        ll k = (a[i]-a[i-1])-(a[0]-a[n-1]);
        if (k%n){
            PRINT("NO");
            return 0;
        }
        b[i] = k/n;
    }
    ll cnt = a[0];
    rep(i,n-1){
        cnt += (n-i)*b[i+1];
    }
    if (cnt%(n*(n+1)/2)){
        PRINT("NO");
        return 0;
    }
    cnt /= n*(n+1)/2;
    rep(i,n){
        b[i] = cnt-b[i];
        if (b[i] < 0){
            PRINT("NO");
            return 0;
        }
    }
    PRINT("YES");
}

Measured memory_bytes: 1792
# include <iostream>
# include <algorithm>
# include <vector>
# include <string>
# include <set>
# include <map>
# include <cmath>
# include <iomanip>
# include <functional>
# include <utility>
# include <stack>
# include <queue>
# include <list>
# include <unordered_map>
constexpr int MOD = 1000000000 + 7;
constexpr int INF = 2000000000;
using namespace std;
using LL = long long;

int main() {
	LL a, b;
	cin >> a >> b;
	LL ans = 1LL << 60;
	for (int B1 = -1; B1 <= 1; B1 += 2) {
		for (int B2 = -1; B2 <= 1; B2 += 2) {
			LL nx = B1 * a;
			LL ny = B2 * b;
			LL cost = (B1 < 0 ? 1 : 0) + (B2 < 0 ? 1 : 0);
			if (nx <= ny) ans = min(ans, cost + (ny - nx));
		}
	}
	cout << ans << endl;
}

Measured memory_bytes: 256
#include<ios>
int main(){
    int iAA,iAB,iAC,iBA,iBB,iBC,iCA,iCB,iCC;
    scanf("%d%d%d%d%d%d%d%d%d",&iAA,&iAB,&iAC,&iBA,&iBB,&iBC,&iCA,&iCB,&iCC);
    if(iAA+iBB==iAB+iBA && iBB+iCC == iBC+iCB && iAA +iCC == iCA+iAC)
        printf("Yes\n");
    else printf("No\n");
    return 0;
}


Measured memory_bytes: 128
#include<stdio.h>

long long pow2(long long x){
	long long r=1;
	while(x!=0){r*=2;x--;}
	return r;
}

long long bc(long long x){
	long long r=0;
	while(x!=0){r+=(x%2);x/=2;}
	return r;
}

long long calc(long long d,long long x[]){
	long long r = 0,i,m;
	m = x[1];d/=2;
	for(i = 2;i <= 15;i++){
		if(d%2){
			if(m < x[i]){m = x[i];}
			else{r+=(m+1-x[i]);m++;}
		}
		else if(d != 0 && m < x[i]){return 1145141919810;}
		d/=2;
	}
	return r;
}

int main(){
	long long n,k,a[16],i,m=1145141919810,x;
	scanf("%lld%lld",&n,&k);
	for(i = 1;i <= n;i++){scanf("%lld",&a[i]);}
	for(i = 1;i < pow2(n);i+=2){
		if(bc(i) != k){continue;}
		x = calc(i,a);
		if(x < m){m = x;}
	}
	printf("%lld\n",m);
	return 0;
}

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

int main(){
  ll n,a,b;
  std::cin >> n>>a>>b;
  ll ans=n/(a+b)*a;
  ll temp =n-n/(a+b)*(a+b);
  // std::cout << temp << '\n';



  std::cout << ans+min(a,temp)<< '\n';

  return 0;
}


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

typedef long long int ll;
#define INF 1999999999
#define MODA 1000000007

int main()
{
    // freopen("input.txt", "r", stdin);
    // freopen("output.txt", "w", stdout);
    ios::sync_with_stdio(false);
    cin.tie(0);
    int n;
    cin >> n;
    vector<int> v(n);
    for(int i = 0; i < n; i++)
    {
        cin >> v[i];
    }

    vector<int>even;
    for(int i = 0; i < n; i++)
    {
        if(v[i] % 2 == 0)
        {
            even.push_back(v[i]);
        }
    }
     int count = 0;
    for(int i = 0; i < even.size(); i++)
    {
        if(even[i] % 3 == 0 || even[i] % 5 == 0)
        {
            count++;
        }
    }
    if(count  ==  even.size())
    {
        cout << "APPROVED" << endl;
    }
    else
    {
         cout << "DENIED" << endl;
    }
    return 0;
}


Measured memory_bytes: 256
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Scanner;

class Main {
    public static int MAX = 301;
    public static void main(String[] args) throws IOException {
        Scanner sc = new Scanner(new BufferedReader(new InputStreamReader(System.in)));
        int n = sc.nextInt();
        int[] cnt = new int[4];
        for(int i = 0; i < n; i++) {
            cnt[sc.nextInt()]++;
        }
        double p[][][] = new double[MAX][MAX][MAX];
        double e[][][] = new double[MAX][MAX][MAX];
        p[cnt[1]][cnt[2]][cnt[3]] = 1;
        for(int c = n; c >= 0; c--) {
            for(int b = n; b >= 0; b--) {
                for(int a = n; a >= 0; a--) {
                    if(a == 0 && b == 0 && c == 0) {
                        continue;
                    }
                    if(a + b + c > n) {
                        continue;
                    }
                    double pWaste = (double) (n - (a + b + c)) / (n * 1.0);
                    double eWaste = pWaste / (1 - pWaste) + 1;
                    e[a][b][c] += eWaste * p[a][b][c];
                    if(a != 0) {
                        double pGo = (double) a / ((a + b + c) * 1.0);
                        p[a - 1][b][c] += p[a][b][c] * pGo;
                        e[a - 1][b][c] += e[a][b][c] * pGo;
                    }
                    if(b != 0) {
                        double pGo = (double) b / ((a + b + c) * 1.0);
                        p[a + 1][b - 1][c] += p[a][b][c] * pGo;
                        e[a + 1][b - 1][c] += e[a][b][c] * pGo;
                    }
                    if(c != 0) {
                        double pGo = (double) c / ((a + b + c) * 1.0);
                        p[a][b + 1][c - 1] += p[a][b][c] * pGo;
                        e[a][b + 1][c - 1] += e[a][b][c] * pGo;
                    }
                }
            }
        }
        System.out.println(e[0][0][0]);
    }
}


Measured memory_bytes: 593640
h,w=map(int,input().split())
s=[list(input()) for i in range(h)]
a=[[0]*w for j in range(h)]
b=[[0]*w for j in range(h)]
for i in range(h):
    k=0
    rep=0
    while k<w:
        cnt=0
        if s[i][k]==".":
            j=k
            while k<w and s[i][k]==".":
                k+=1
                cnt+=1
            for x in range(j,k):
                a[i][x]=cnt
        else:
            k+=1
for i in range(w):
    k=0
    while k<h:
        cnt=0
        if s[k][i]==".":
            j=k
            while k<h and s[k][i]==".":
                k+=1
                cnt+=1
            for x in range(j,k):
                b[x][i]=cnt
        else:
            k+=1
ans=0
for i in range(h):
    for j in range(w):
        ans=max(ans,a[i][j]+b[i][j])
print(ans-1)

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

const int mod = 1000000007;

int main() {
  int n;
  cin >> n;
  vector<ll> a(n);
  rep(i,n) cin >> a[i];
  ll ans = 0;
  rep(i,60) {
    ll x = 0;
    rep(j,n) if (a[j]>>i&1) ++x;
    ll now = x*(n-x)%mod;
    rep(j,i) now = now*2%mod;
    ans += now;
    ans %= mod;
  }
  cout << ans << endl;
  return 0;
}

Measured memory_bytes: 2560
// https://atcoder.jp/contests/abc119/tasks/abc119_d

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;

#define REP(i,n)   for(int i=0; i<(int)(n); i++)
#define FOR(i,b,e) for(int i=(b); i<=(int)(e); i++)
#define DUMP(a, n) REP(_i, n) printf("%d%c", a[_i], _i + 1 == n ? '\n' : ' ')
#define DUMP2D(a, n, m) REP(_i, n) REP(_j, m) printf("%d%c", a[_i][_j], _j + 1 == m ? '\n' : ' '); puts("")

const int A_MAX = 1e5;
const int B_MAX = 1e5;
const int Q_MAX = 1e5;
const ll S_A_MAX = 1e10;
const ll T_B_MAX = 1e10;
const ll X_I_MAX = 1e10;
const ll INF = 1e12;

int A, B, Q;
ll s[A_MAX];
ll t[B_MAX];
ll x[Q_MAX];

void solve() {
  REP(i, Q) {
    int i1 = lower_bound(s, s + A, x[i]) - s;
    int i0 = i1 - 1;
    ll s0 = i0 >= 0 ? s[i0] : -INF;
    ll s1 = i1 < A ? s[i1] : INF;
    int j1 = lower_bound(t, t + B, x[i]) - t;
    int j0 = j1 - 1;
    ll t0 = j0 >= 0 ? t[j0] : -INF;
    ll t1 = j1 < B ? t[j1] : INF;
    // printf("x:%lld s:%lld_%lld t:%lld_%lld\n", x[i], s0, s1, t0, t1);
    ll ans = INF;
    ans = min(ans, x[i] - min(s0, t0));
    ans = min(ans, max(s1, t1) - x[i]);
    ans = min(ans, s1 - t0 + min(x[i] - t0, s1 - x[i]));
    ans = min(ans, t1 - s0 + min(x[i] - s0, t1 - x[i]));
    printf("%lld\n", ans);
  }
}

void input() {
  scanf("%d%d%d", &A, &B, &Q);
  REP(i, A) scanf("%lld", s + i);
  REP(i, B) scanf("%lld", t + i);
  REP(i, Q) scanf("%lld", x + i);
}

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


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

int main(){
	int n,q,s[10000],t[500];
	scanf("%d",&n);
	for (int i=0;i<n;i++)
		scanf("%d",s+i);
	scanf("%d",&q);
	for (int i=0;i<q;i++)
		scanf("%d",t+i);
	int count=0;
	for (int i=0;i<q;i++){
		for (int j=0;j<n;j++){
			if (s[j]==t[i]){
				count++;
				break;
			}
		}
	}
	printf("%d\n",count);
}	



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

int n, K;
vector<int> x, y, v, w;
long long ans = 9e18;

int main(){
//	cin.tie(0);
//	ios::sync_with_stdio(false);
	cin >> n >> K;
	for(int i=0; i<n; ++i){
		int s, t;
		cin >> s >> t;
		x.push_back(s);
		y.push_back(t);
	}
	v = x;
	w = y;
	sort(v.begin(), v.end());
	sort(w.begin(), w.end());
	for(int i=0; i<n; ++i)
		for(int j=i+1; j<n; ++j)
			for(int k=0; k<n; ++k)
				for(int l=k+1; l<n; ++l){
					int tmp = 0;
					for(int m=0; m<n; ++m)
						if(v[i] <= x[m] && x[m] <= v[j] && w[k] <= y[m] && y[m] <= w[l])
							++tmp;
					if(tmp >= K)
						ans = min(ans, (long long)(v[j] - v[i]) * (w[l] - w[k]));
				}
	cout << ans << "\n";
}

Measured memory_bytes: 256
#include <algorithm>
#include <cctype>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;

#define EPS 1e-10
#define INF 1000000000
#define mp make_pair
#define pb push_back

typedef vector<bool> vb;
typedef vector<vb> vvb;
typedef vector<int> vi;
typedef vector<vi> vii;
typedef pair<int,int> pii;
typedef long long ll;

int main() {
  int N, M;
  cin >> N >> M;
  vi routes(N, 0);
  for (int i = 0; i < M; i++) {
    int a, b;
    cin >> a >> b;
    routes[a-1]++;
    routes[b-1]++;
  }
  for (int i = 0; i < N; i++) {
    cout << routes[i] << endl;
  }
}

Measured memory_bytes: 256
import java.util.*;
import java.lang.*;

public class Main {
  
   public int dp(ArrayList<Integer>[] tree,int[] dp,int index) {
	   if(dp[index]!=-1) {
		   return dp[index];
	   }
	   Iterator<Integer> it=tree[index].iterator();
	   int max=0;
	   while(it.hasNext()) {
		   int v=it.next();
		   if(this.dp(tree, dp, v)+1>max) {
			   max=this.dp(tree, dp, v)+1;
		   }
	   }
	   dp[index]=max;
	   return max;
   }
	public static void main(String[] args) {
        // TODO Auto-generated method stub
        Scanner sc=new Scanner(System.in);
       int n=sc.nextInt();
       int m=sc.nextInt();
        ArrayList<Integer>[] tree=new ArrayList[n];
        for(int i=0;i<n;i++) {
        	tree[i]=new ArrayList<Integer>();
        }
        for(int i=0;i<m;i++) {
        	tree[sc.nextInt()-1].add(sc.nextInt()-1);
        }
        int[] dp=new int[n];
        for(int i=0;i<n;i++) {
        	dp[i]=-1;
        }
        Main mm=new Main();
        for(int i=0;i<n;i++) {
        mm.dp(tree, dp, i);}
        
        Arrays.sort(dp);
        System.out.println(dp[n-1]);
	}
}



Measured memory_bytes: 107524
#include<bits/stdc++.h>
typedef long long ll;
typedef unsigned long long ull;
using namespace std;
#define pb push_back
int dy[]={0, 0, 1, -1, 0};
int dx[]={1, -1, 0, 0, 0};
 
#define FOR(i,a,b) for (int i=(a);i<(b);i++)
#define RFOR(i,a,b) for (int i=(b)-1;i>=(a);i--)
#define REP(i,n) for (int i=0;i<(n);i++)
#define RREP(i,n) for (int i=(n)-1;i>=0;i--)
#define MAX_V 100
#define INF 1000000000

struct edge{int to, cap;
            int  rev;};

vector<edge> G[MAX_V];
bool used[MAX_V];

void add_edge(int from,int to,int cap){
    G[from].pb((edge){to,cap,G[to].size()});//?¬?????????§G[to]??????????????????????¶???????????????§?????????????????\?????¨???
    G[to].pb(edge{from,0,G[from].size() - 1});//???????????§G[from](ry....
}

int dfs(int v,int t,int f/*?????£??????????????????????°?????????????*/){
    if(v == t)return f;
    used[v] = true;
    for(int i = 0;i < G[v].size();i++){
        edge &e = G[v][i];
        if(!used[e.to] && e.cap > 0){
            int d = dfs(e.to,t,min(f,e.cap));
            if(d > 0){
                e.cap -= d;//????????????????????£???????????????
                G[e.to][e.rev].cap += d;//??????????????£???????????????
                return d;
            }
        }
    }
    return 0;
}

int max_flow(int s,int t){
    int flow = 0;
    while(1){
        fill(used,used+MAX_V,false);
        int f = dfs(s,t,INF);
        if(f == 0)return flow;
        flow += f;
    }
}

int main(){
    int V,E;
    int u,v,c;
    scanf("%d%d",&V,&E);
    REP(i,E){
        scanf("%d%d%d",&u,&v,&c);
        add_edge(u,v,c);
    }
    printf("%d\n",max_flow(0,V-1));
}

Measured memory_bytes: 3272
[A, B, C, X] = [int(input()) for i in range(4)]
number = 0
for a in range(A + 1):
  for b in range(B + 1):
    for c in range(C + 1):
      if a * 500 + b * 100 + c * 50 == X:
        number += 1
print(number)

Measured memory_bytes: 3060
#include <bits/stdc++.h>
using namespace std;
/*#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
template<typename T> using gpp_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
template<typename T, typename L> using gpp_map = tree<T, L, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
template<typename T> using gpp_multiset = tree<T, null_type, less_equal<T>, rb_tree_tag, tree_order_statistics_node_update>;*/
struct fast_ios { fast_ios(){ cin.tie(0); ios::sync_with_stdio(false); cout << fixed << setprecision(20); }; } fast_ios_;
#define FOR(i, begin, end) for(int i=(begin);i<(end);i++)
#define REP(i, n) FOR(i,0,n)
#define IFOR(i, begin, end) for(int i=(end)-1;i>=(begin);i--)
#define IREP(i, n) IFOR(i,0,n)
#define Sort(v) sort(v.begin(), v.end())
#define Reverse(v) reverse(v.begin(), v.end())
#define all(v) v.begin(),v.end()
#define SZ(v) ((int)v.size())
#define Lower_bound(v, x) distance(v.begin(), lower_bound(v.begin(), v.end(), x))
#define Upper_bound(v, x) distance(v.begin(), upper_bound(v.begin(), v.end(), x))
#define Max(a, b) a = max(a, b)
#define Min(a, b) a = min(a, b)
#define bit(n) (1LL<<(n))
#define bit_exist(x, n) ((x >> n) & 1)
#define debug(x) cout << #x << "=" << x << endl;
#define vdebug(v) { cout << #v << "=" << endl; REP(i_debug, v.size()){ cout << v[i_debug] << ","; } cout << endl; }
#define mdebug(m) { cout << #m << "=" << endl; REP(i_debug, m.size()){ REP(j_debug, m[i_debug].size()){ cout << m[i_debug][j_debug] << ","; } cout << endl;} }
#define Return(ans) { cout << (ans) << endl; return 0; }
#define pb push_back
#define f first
#define s second
#define int long long
#define INF 1000000000000000000
template<typename T> istream &operator>>(istream &is, vector<T> &v){ for (auto &x : v) is >> x; return is; }
template<typename T> ostream &operator<<(ostream &os, vector<T> &v){ for(int i = 0; i < v.size(); i++) { cout << v[i]; if(i != v.size() - 1) cout << endl; }; return os; }
template<typename T1, typename T2> ostream &operator<<(ostream &os, pair<T1, T2> p){ cout << '(' << p.first << ',' << p.second << ')'; return os; }
template<typename T> void Out(T x) { cout << x << endl; }
template<typename T1, typename T2> void Ans(bool f, T1 y, T2 n) { if(f) Out(y); else Out(n); }
 
using vec = vector<int>;
using mat = vector<vec>;
using Pii = pair<int, int>;
using PiP = pair<int, Pii>;
using PPi = pair<Pii, int>;
using bools = vector<bool>;
using pairs = vector<Pii>;
 
//int dx[4] = {1,0,-1,0};
//int dy[4] = {0,1,0,-1};
//char d[4] = {'D','R','U','L'};
 
const int mod = 1000000007;
//const int mod = 998244353;
//#define Add(x, y) x = (x + (y)) % mod
//#define Mult(x, y) x = (x * (y)) % mod

#include <bits/stdc++.h>
using namespace std;

signed main(){
    int N; cin >> N;
    vec A(N);
    vec check(N+1,0);
    REP(i,N){
        cin>>A[i];
        check[A[i]-1] ++;
    }
  	int ans = 0;
  	REP(i,N){
      ans += (check[i]*(check[i]-1))/2;
    }
  vec D(N+1,ans);
  REP(i,N+1){
    if(check[i]>0) D[i] -= check[i]-1; 
  }
  REP(i,N) Out(D[A[i]-1]);
}
  

Measured memory_bytes: 7296
n,m=map(int,raw_input().split())
s=raw_input().split()
a=[]
t=[0 for i in range(n)]
for i in range(n):
    a.append(int(s[i]))
sum=0
a=sorted(a)
for j in range(n-1,n-m-1,-1):
    sum=sum+a[j]
print sum

Measured memory_bytes: 2568
#include <iostream>
#include <string>
#include <cmath>
#include <map>
#include <algorithm>
#include <vector>
using namespace std;

int main()
{
	long long T[1000], A[1000];
	long long N,t, a, m, ans;
	cin >> N;
	for (int i = 0; i < N; i++) {
		cin >> T[i] >> A[i];
	}
	
	for (int j = 0; j < N - 1; j++) {
		if(T[j] % T[j + 1] != 0){
			t = T[j] / T[j + 1] + 1;
		}
		else {
			t = T[j] / T[j + 1];
		}
		if(A[j] % A[j + 1] != 0) {
			a = A[j] / A[j + 1] + 1;
		}
		else {
			a = A[j] / A[j + 1];
		}
		m = max(t, a);
		T[j + 1] = T[j + 1] * m;
		A[j + 1] = A[j + 1] * m;
	}
	ans = A[N - 1] + T[N - 1];
	cout << ans << endl;
	return 0;
}



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

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

pub fn read_vec2<T: std::str::FromStr>(n: usize) -> Vec<Vec<T>> {
    (0..n).map(|_| read_vec()).collect()
}

pub fn read_col<T: std::str::FromStr>(n: usize) -> Vec<T> {
    (0..n).map(|_| read()).collect()
}

fn main() {
    let nx: Vec<u64> = read_vec();
    let n = nx[0] as usize;
    let x = nx[1];
    let mut avec: Vec<u64> = read_vec();
    let mut ans: u64 = 0;
    if avec[0] > x {
        ans += avec[0] - x;
        avec[0] = x;
    }
    for i in 1..n {
        if avec[i - 1] + avec[i] > x {
            ans += avec[i - 1] + avec[i] - x;
            avec[i] = x - avec[i - 1];
        }
    }
    println!("{}", ans);
}


Measured memory_bytes: 8444
#include <iostream>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <sstream>
#include <functional>

using namespace std;

int main() {
    string s,t;

    cin >> s >> t;

    sort(s.begin(),s.end());
    sort(t.rbegin(),t.rend());
    

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


Measured memory_bytes: 256
#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
using namespace std;
signed main() {
  ios::sync_with_stdio(false); cin.tie(0);
  int n;
  cin >> n;
  vector<int> in(n);
  for (int i = 0; i < n; i++) {
    cin >> in[i];
  }
  long long mx = 2, mi = 2;
  for (int i = n - 1; i >= 0; i--) {
    mx = 1LL * (mx / in[i] + 1) * in[i] - 1;
    if (mi < in[i]) mi = in[i];
    if (mi > in[i]) mi = 1LL * ((mi + in[i] - 1) / in[i]) * in[i];
  }
  long long x = mx, y = mi;
  for (int i = 0; i < n; i++) {
    x = 1LL * (x / in[i]) * in[i];
    y = 1LL * (y / in[i]) * in[i];
  }
  if (x == 2 && y == 2) {
    cout << mi << " " << mx << endl;
  } else {
    cout << -1 << endl;
  }
  return 0;	
}

Measured memory_bytes: 640
#include <bits/stdc++.h>
#define ALL(a)  (a).begin(),(a).end()
#define sz(x) int(x.size())
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
typedef pair<long long, long long> Pll;
typedef vector<int> vi;
typedef vector<vector<int>> vvi;
typedef vector<long long> vll;
typedef vector<vector<long long>> vvll;
template <typename T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template <typename T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
const long long INF = 1LL << 60;
const int INT_INF = 1 << 30;
const double PI = acos(-1.0);
#define MOD 1000000007LL
#define endl "\n"

int main(){
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  srand((unsigned)time(NULL));
  ll N;
  cin >> N;
  string X;
  cin >> X;

  ll pc = 0;
  for(int i = 0; i < N; i++){
    if(X.at(i) == '1') pc++;
  }

  if(pc == 0){
    for(int i = 0; i < N; i++){
      cout << 1 << endl;
    }
    return 0;
  }
  if(pc == 1){
    if(X.at(N-1) == '0'){
      for(int i = 0; i < N-1; i++){
        if(X.at(i) == '0'){
          cout << 1 << endl;
        }
        else cout << 0 << endl;
      }
      cout << 2 << endl;
    }
    else{
      for(int i = 0; i < N-1; i++){
        cout << 2 << endl;
      }
      cout << 0 << endl;
    }
    return 0;
  }

  map<ll, ll> mp;
  mp[0] = 0;
  for(int i = 1; i <= 2 * 100000; i++){
    ll popc = __builtin_popcount(i);
    if(i % popc == 0) mp[i] = 1;
    else{
      mp[i] = 1 + mp[i % popc];
    }
  }
  ll ppc = pc-1, mpc = pc+1;
  vll one(N, -1), zero(N, -1);

  ll psum = 0, msum = 0;
  ll tmp = 1LL;
  for(int i = 0; i < N; i++){
    one.at(N-1-i) = tmp % ppc;
    tmp *= 2LL;
    tmp %= ppc;
    if(X.at(N-1-i) == '1') psum += one.at(N-1-i);
    psum = (psum + ppc) % ppc;
  }
  tmp = 1LL;
  for(int i = 0; i < N; i++){
    zero.at(N-1-i) = tmp % mpc;
    tmp *= 2LL;
    tmp %= mpc;
    if(X.at(N-1-i) == '1') msum += zero.at(N-1-i);
    msum = (msum + mpc) % mpc;
  }

  
  for(int i = 0; i < N; i++){
    if(X.at(i) == '1'){
      ll num = psum - one.at(i);
      num = (num + ppc) % ppc;
      cout << mp[num] + 1 << endl;
    }
    else{
      ll num = msum + zero.at(i);
      num = (num + mpc) % mpc;
      cout << mp[num] + 1 << endl;
    }
  }
}

Measured memory_bytes: 19184