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

vector<int> adj[100005];
int deg[100005], dp[100005];

int solve(int u){
    if(dp[u] != -1) return dp[u];
    int res = 0;
    for(int v : adj[u])
        res = max(res, solve(v));
    return dp[u] = res + 1;
}

int main(){
    memset(dp, -1, sizeof dp);
    int n, m; scanf("%d %d", &n, &m);
    for(int i = 0; i < m; i++){
        int u, v; scanf("%d %d", &u, &v);
        adj[u].push_back(v);
        deg[v]++;
    }
    
    int res = 0;
    for(int i = 1; i <= n; i++)
        if(deg[i] == 0){
            //cout << i << endl;
            res = max(res, solve(i));
        }
    cout << res-1 << endl;
	return 0;
}


Measured memory_bytes: 11136
import sys
input = sys.stdin.readline

n = int(input())
cnt = 0
decrement = 0
profit = []
loss = []

for _ in range(n):
    s = input().rstrip()
    min_p = 0
    end_p = 0
    point = 0
    for c in s:
        if c == '(':
            point += 1
        elif c == ')':
            point -= 1
            if point < min_p:
                min_p = point
    else:
        end_p = point
    if 0 <= min_p:
        cnt += end_p
    else:
        if min_p == end_p:
            decrement += end_p
        else:
            if 0 <= end_p:
                profit.append((min_p, end_p))
            else:
                loss.append((min_p, end_p))



profit.sort()
while profit:
    min_p, end_p = profit.pop()
    if cnt+min_p < 0:
        print('No')
        exit()
    cnt += end_p


loss.sort(reverse=True)
while loss:
    min_p, end_p = loss.pop()
    if cnt+min_p < 0:
        print('No')
        exit()
    cnt += end_p

print('Yes' if cnt+decrement == 0 else 'No')


Measured memory_bytes: 23496
#include <stdio.h>

int main(void){
	long debt = 100000;
	double interest = 0.05;
	int n, i;

	scanf("%d", &n);
	for(i = 1; i <= n; i++){
		debt = debt * (1 + 0.05);
		if(debt % 1000 > 0){
			debt = (debt - (debt % 1000)) + 1000;
		}
	}

	printf("%ld\n", debt);
	return 0;
}

Measured memory_bytes: 592
/**
 *    author:  otera    
**/
#include<iostream>
#include<string>
#include<cstdio>
#include<cstring>
#include<vector>
#include<cmath>
#include<algorithm>
#include<functional>
#include<iomanip>
#include<queue>
#include<deque>
#include<ciso646>
#include<random>
#include<map>
#include<set>
#include<complex>
#include<bitset>
#include<stack>
#include<unordered_map>
#include<utility>
#include<cassert>
using namespace std;

//#define int long long
typedef long long ll;
typedef unsigned long long ul;
typedef unsigned int ui;
typedef long double ld;
const int inf=1e9+7;
const ll INF=1LL<<60 ;
const ll mod=1e9+7 ;
#define rep(i,n) for(int i=0;i<n;i++)
#define per(i,n) for(int i=n-1;i>=0;i--)
#define Rep(i,sta,n) for(int i=sta;i<n;i++)
#define rep1(i,n) for(int i=1;i<=n;i++)
#define per1(i,n) for(int i=n;i>=1;i--)
#define Rep1(i,sta,n) for(int i=sta;i<=n;i++)
typedef complex<ld> Point;
const ld eps = 1e-8;
const ld pi = acos(-1.0);
typedef pair<int, int> P;
typedef pair<ld, ld> LDP;
typedef pair<ll, ll> LP;
#define fr first
#define sc second
#define all(c) c.begin(),c.end()
#define pb push_back
#define debug(x)  cerr << #x << " = " << (x) << endl;
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; }

void solve() {
	string s; cin >> s;
    int n = (int)s.size();
    int N = 0, W = 0, S = 0, E = 0;
    rep(i, n) {
        if(s[i] == 'N') {
            N = 1;
        } else if(s[i] == 'W') {
            W = 1;
        } else if(s[i] == 'S') {
            S = 1;
        } else {
            E = 1;
        }
    }
    if(N ^ S) {
        cout << "No" << endl;
        return;
    }
    if(W ^ E) {
        cout << "No" << endl;
        return;
    }
    cout << "Yes" << endl;
}

signed main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	//cout << fixed << setprecision(10);
	//int t; cin >> t; rep(i, t)solve();
	solve();
    return 0;
}

Measured memory_bytes: 256
require 'pp'
$DEBUG ? define_method(:dpp){|e|pp e} : define_method(:dpp){|e|}

# Nと空白区切の入力値を数値の配列で返却する
def gets_n_and_i_list()
  return gets.chomp.to_i, gets.chomp.split(" ").map(&:to_i)
end
N, alist = gets_n_and_i_list

count = 0
alist.each_with_index do |a, i|
  if alist[a - 1] == i + 1
    count += 1
  end
end
puts count / 2

Measured memory_bytes: 21396
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
typedef map<int,int> mii;
typedef vector<int>vi;
#define pb push_back
#define sc(n) scanf("%d",&n)
#define lsc(n) scanf("%lld",&n)
#define all(x) x.begin(),x.end()
#define rep(i,n) for(i=0LL;i<n;i++)
#define rep1(i,n) for(i=1;i<=n;i++)
#define rev(i,n) for(i=n-1;i>=0;i--)
#define rev1(i,n) for(i=n;i>0;i--)
#define in insert
#define IT ::iterator
#define pr(n) printf("%d\n",n)
#define lpr(n) printf("%lld\n",n);

int main()
{
  int a,b,c;
  cin >> a >> b >> c;
  swap(a,b);
  swap(a,c);
  cout << a << " " << b << " " << c << endl;
}


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

int main() {
  int N, K;
  cin >> N >> K;
  int ans = 0;
  for (int i = 0; i < N; ++i) {
    int x;
    cin >> x;
    ans += min(x, K - x) * 2;
  }
  cout << ans << endl;
}


Measured memory_bytes: 256
#include <bits/stdc++.h>
#define rep(i, n) for (rint i = 1; i <= (int)(n); i ++)
#define re0(i, n) for (rint i = 0; i < (int) n; i ++)
#define travel(i, u) for (rint i = head[u]; i; i = e[i].nxt)
#define rint register int
using namespace std;

typedef long long lo;

template<typename tp> inline void read(tp &x) {
    x = 0; char c = getchar(); int f = 0;
    for (; c < '0' || c > '9'; f |= c == '-', c = getchar());
    for (; c >= '0' && c <= '9'; x = (x << 3) + (x << 1) + c - '0', c = getchar());
    if (f) x = -x;
}

int main(void) {
	string str;
	cin >> str;
	string pre = " ";
	string cur = "";
	int ans = 0;
	for (int i = 0; i < str.length(); i++) {
		cur += str[i];
		if (cur != pre) {
			pre = cur;
			cur = "";
			++ans;
		}
	}
	cout << ans << "\n";
}


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

#define watch(x) cerr << (#x) << ": " << (x) << endl
#define int long long



int32_t main() {

    string s; cin >> s;
    bool good = true;
    for(int i = 0; i < 3; ++i) {
        if(s[i] == s[i+1]) good = false;
    }
    if(good) cout << "Good\n";
    else cout << "Bad\n";
    return 0;
}


Measured memory_bytes: 256
using System;
using System.Linq;

namespace aizu
{
    class OnlineJudge
    {
        static void Main()
        {
            Func<int> input = () => Enumerable.Range(0, 10).Select(p => int.Parse(Console.ReadLine())).OrderByDescending(p => p).Take(3).Sum();
            Console.WriteLine("{0} {1}", input(), input());
        }
    }
}

Measured memory_bytes: 6428
#include<cstdio>
#include<cmath>
#include<cstdlib>
#include<string>
#include<algorithm>
#include <bits/stdc++.h>
using namespace std;
int main(void){
	int a,b,c,x,y,ans1,ans2,ans3,ans;
	scanf("%d%d%d%d%d",&a,&b,&c,&x,&y);
	ans1=a*x+b*y;
	if(x>y){
		ans2=c*y*2+a*(x-y);
		ans3=c*x*2;
	}else if(x<y){
		ans2=c*x*2+b*(y-x);
		ans3=c*y*2;
	}else{
		ans2=c*x*2;
		ans3=ans2;
	}
	ans=min(ans1,min(ans2,ans3));
	printf("%d\n",ans);
	return 0;
}

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

#define rep(i, a, b) for(int i = (int)a; i < (int)b; i++)
#define repm(i, a, b) for(int i = (int)a; i > (int)b; i--)
#define all(x) x.begin(),x.end()
#define pb push_back
#define mp make_pair
#define fi first
#define se second
typedef long long ll;
typedef long double ld;
typedef pair<ll,ll> P;
typedef pair<int,int> Pi;


int main(){
  string s;
  cin >> s;
  bool ok = true;
  
  if((s.at(2) != s.at(3)) || (s.at(4) != s.at(5)))
    ok = false;
  
  cout << ((ok)? "Yes" : "No") << endl;
}

Measured memory_bytes: 256
#include<bits/stdc++.h>
#define ll long long int
#define M 1000000007
#define mod 998244353
#define mp(x,y) make_pair(x,y)
#define pb(x) push_back(x)
#define pi pair<ll,ll>
using namespace std;
const ll N=500010;


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

    string s;
    cin>>s;
    ll n=s.length();
    for(ll i=0;i<n;++i){cout<<"x";}

    return (0);
}


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

int dis(int x, int y) {
  if(x-y > 0)   return x-y;
  else          return y-x;
}

int main() {
  int a, b, c, d;
  cin >> a >> b >> c >> d;
  
  if(dis(a, c) <= d || (dis(a, b) <= d && dis(b, c) <=d))
    cout << "Yes" << endl;
  else
    cout << "No" << endl;
    
}

Measured memory_bytes: 256
#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/rope>
using namespace __gnu_pbds;
using namespace __gnu_cxx;
#ifndef print
#define print(...)
#define trace(...)
#define endl '\n'
#endif
#define pb push_back
#define fi first
#define se second
#define int long long
typedef long long ll;
typedef long double f80;
#define double long double
#define pii pair<int,int>
#define pll pair<ll,ll>
#define sz(x) ((long long)x.size())
#define fr(a,b,c) for(int a=b; a<=c; a++)
#define rep(a,b,c) for(int a=b; a<c; a++)
#define trav(a,x) for(auto &a:x)
#define all(con) con.begin(),con.end()
const ll infl = 0x3f3f3f3f3f3f3f3fLL;
const int infi = 0x3f3f3f3f;
//const int mod = 998244353;
const int mod = 1000000007;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> oset;
auto clk = clock();
mt19937_64 rang(chrono::high_resolution_clock::now().time_since_epoch().count());
int rng(int lim) { uniform_int_distribution<int> uid(0, lim - 1); return uid(rang); }
int powm(int a, int b) { int res = 1; while (b) { if (b & 1) res = (res * a) % mod; a = (a * a) % mod; b >>= 1; } return res; }

int32_t main() {
	ios::sync_with_stdio(false); cin.tie(NULL); cout.precision(10); srand(chrono::high_resolution_clock::now().time_since_epoch().count());
	string s; cin >> s;
	for (int i = 0; i < s.size(); ++i) {
		if (s[i] == '?') {
			s[i] = 'D';
		} 
	}
	cout << s << '\n';
}

Measured memory_bytes: 3748
/*

Single Source Shortest Path I(https://onlinejudge.u-aizu.ac.jp/problems/GRL_1_A)

*/
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <climits>
#include <cmath>
#include <complex>
#include <map>
#include <set>
#include <vector>
#include <stack>
#include <queue>
#include <bitset>
#include <algorithm>
#include <numeric>
#include <functional>
#include <cassert>
#include <iomanip>
using namespace std;

#define Rep(b, e, i) for(int i = b; i <= e; i++)
#define Repr(e, b, i) for(int i = e; i >= b; i--)
#define rep(n, i) Rep(0, n-1, i)
#define repr(n, i) Repr(n-1, 0, i)
#define all(v) (v).begin(), (v).end()
#define pb(v) push_back(v)
#define uniq(v) (v).erase(unique(all(v)),(v).end())
#define bitcnt(x) __builtin_popcount(x)
#define fst first
#define snd second
#define Pqaz(T) priority_queue<T,vector<T>,greater<T>>
#define Pqza(T) priority_queue<T>
#define put(x) cout << x;
#define putsp(x) cout << x << ' ';
#define putln(x) cout << x << endl;
#define ENJYU std::ios::sync_with_stdio(false);std::cin.tie(0);

typedef long long ll;
typedef pair<ll, ll> P;
typedef pair<int, int> intP;
typedef complex<double> comp;
typedef vector <int> vec;
typedef vector <ll> vecll;
typedef vector <double> vecd;
typedef vector <vec> mat;
typedef vector <vecll> matll;
typedef vector <vecll> matd;

//vector の中身を出力
template <class T>ostream &operator<<(ostream &o,const vector<T>&v)
{o<<"{";for(int i=0;i<(int)v.size();i++)o<<(i>0?", ":"")<<v[i];o<<"}";return o;}


//最大の頂点数
const int MAX_V = 100010;
const ll INF = 1e18;

struct edge{ll to, cost;};

//V := number of nodes
//G := adj
int V;
vector<edge> G[MAX_V];
ll d[MAX_V];

void dijkstra(int s){
	priority_queue<P, vector<P>, greater<P> > que;
	fill(d, d+V, INF);
	d[s] = 0;
	que.push(P(0, s));

	while (!que.empty()) {
		P p = que.top(); que.pop();
		int v = p.second;
		if (d[v] < p.first) continue;
		rep((int)G[v].size(), i) {
			edge e = G[v][i];
			if (d[e.to] > d[v] + e.cost) {
				d[e.to] = d[v] + e.cost;
				que.push(P(d[e.to], e.to));
			}
		}
	}
}

void solve(void){
	int E, r;
	cin >> V >> E >> r;

	while(E--)
	{
		ll s, t, d;
		cin >> s >> t >> d;
		G[s].pb((edge{t, d}));
	}

	dijkstra(r);

	rep(V, i)
	{
		if (d[i] == INF)
		{
			cout << "INF" << endl;
			continue;
		}
		cout << d[i] << endl;
	}


}

int main(void){
	solve();
	//cout << "yui(*-v・)yui" << endl;
	return 0;
}



Measured memory_bytes: 19384
#include <cmath>
#include <iostream>
#include <vector>
#include <queue>
#include <map>
#include <set>
#include <algorithm>
#include <utility>
#include <iomanip>

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

using namespace std;

typedef pair<int, int> P;

const int INF = 1e15;
const int MOD = 1e9+7;

void solve(vector<bool>& t, vector<int>& d, vector<int>& ans, int i){
    if(i > 12){
        vector<int> s;
        for(int j = 0; j < 24; j++){
            if(t[j]){
                s.push_back(j);
            }
        }
        int minT = INF;
        for(int j = 0; j < (int)s.size()-1; j++){
            for(int k = j+1; k < (int)s.size(); k++){
                int time = s[k] - s[j];
                time = min(time, 24 - time);
                if(time < minT){
                    minT = time;
                }
            }
        }
        ans.push_back(minT);
        return ;
    }

    if(d[i] == 0){
        solve(t, d, ans, i+1);
    }else{
        t[i] = true;
        solve(t, d, ans, i+1);
        t[i] = false;
        t[24-i] = true;
        solve(t, d, ans, i+1);
        t[24-i] = false;
    }
}

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

    vector<int> d(13);
    d[0] = 1;
    rep(i, n){
        int x;
        cin >> x;
        d[x]++;
    }

    vector<bool> t(24, false);
    for(int i = 1; i <= 12; i++){
        if(d[i] == 2){
            t[i] = true;
            t[24-i] = true;
            d[i] = 0;
        }
        if(d[i] > 2){
            cout << 0 << endl;
            return 0;
        }
    }

    if(d[0] == 1){
        t[0] = true;
    }

    if(d[0] > 1){
        cout << 0 << endl;
        return 0;
    }

    vector<int> ans;
    solve(t, d, ans, 1);

    cout << *max_element(ans.begin(), ans.end()) << endl;
    return 0;
}


Measured memory_bytes: 384
s = int(input())
a = []
a.append(s)

def f(n):
    if n % 2 == 0:
        return int(n/2)
    else:
        return 3*n + 1

m = s
c = 2
while f(m) not in a:
    a.append(f(m))
    m = f(m)
    c += 1

print(c)

Measured memory_bytes: 3060
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<cstdio>
#include<cmath>
#include<vector>
using namespace std;
#define ll long long
#define db double
#define For(i,a,b) for(int i=(a);i<=(b);++i)
#define Rep(i,a,b) for(int i=(a);i>=(b);--i)
const int maxn=100+7,W=90;
const ll mod=1e9+7;
ll Td,n,m,ans,sum,f[maxn];
 
char cc;ll ff;
template<typename T>void read(T& aa) {
    aa=0;ff=1; cc=getchar();
    while(cc!='-'&&(cc<'0'||cc>'9')) cc=getchar();
    if(cc=='-') ff=-1,cc=getchar();
    while(cc>='0'&&cc<='9') aa=aa*10+cc-'0',cc=getchar();
    aa*=ff;
}
 
struct Node{
    ll x,y;
    Node(){}
    Node(ll x,ll y):x(x),y(y){}
}o;
vector<Node> G[maxn];
 
int main() {
    ll x,y,s; f[0]=f[1]=1;
    For(i,2,W) f[i]=f[i-1]+f[i-2];
    G[1].push_back(Node(1,2)); G[1].push_back(Node(1,3));
    G[1].push_back(Node(1,4));
    For(i,1,W-3) {
        s=G[i].size();
        For(j,0,s-1) {
            o=G[i][j]; x=o.y; y=o.x+x;
            while(y<=f[i+3]+f[i]) {
                G[i+1].push_back(Node(x,y));
                y+=x;
            }
        }
    }
     
    read(Td);
    while(Td--) {
        read(n); read(m);
        if(n>m) swap(n,m);
        for(ans=1;f[ans+1]<=n&&f[ans+2]<=m;++ans) ;
        printf("%lld ",ans);
        if(ans==1) {
            printf("%lld\n",n*m%mod);
            continue;
        }
        s=G[ans-1].size(); sum=0;
        For(i,0,s-1) {
            o=G[ans-1][i]; x=o.x; y=o.y;
            if(y<=n) sum+=(m-x)/y%mod;
            if(y<=m) sum+=(n-x)/y%mod;
            sum%=mod;
        }
        printf("%lld\n",sum);
    }
    return 0;
}

Measured memory_bytes: 4096
#include<iostream>
 #include<stdio.h>
 #include<string>
 #include<math.h>
 #include<iomanip>
 #include<algorithm>
 #include<string.h>
 #include<cctype>
 #include<map>
 #include<set>
 #include<vector>
 #include<sstream>
 #include<stack>
 #include<queue>
 
 using namespace std;
 
 void swap(int &a,int &b)
 {
   int temp;
   temp=a;
   a=b;
   b=temp;     
 }
 
 int main()
 {
   int a,b;
   while(cin>>a>>b&&(a||b))
   {
     if(a<=b) cout<<a<<" "<<b<<endl; 
     else
     {
       swap(a,b);
       cout<<a<<" "<<b<<endl;
     }                        
   }
   //while(1);
   return 0;
 }
 
 
 

Measured memory_bytes: 2992
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;

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

    vector<ll> a(n);
    for(int i = 0; i < n; i++){
        cin >> a[i];
    }

    ll ans = 0;
    for(int i = 0; i < n-1; i++){
        ans += a[i]/2;
        a[i+1] += a[i+1]>0 ? a[i]&1 : 0;
    }
    ans += a[n-1]/2;
    cout << ans << endl;
    return 0;
}


Measured memory_bytes: 1024
#include <iostream>
#include <vector>
#include <map>
#include <unordered_map>
#include <queue>
#include <set>
#include <algorithm>
#include <string>
#include <math.h>
#include <limits.h>
#include <stack>
#include <complex>
#include <stdlib.h>
#include <stdio.h>
#include <functional>
#include <cfloat>
#include <math.h>
#include <numeric>
#include <string.h>
#include <sys/time.h>
#include <random>


#define fs first
#define sc second

using namespace std;

typedef long long ll;
typedef pair<ll, ll> P;

int main(){
    ll n, m, v, p;
    cin >> n >> m >> v >> p;
    vector<ll> a(n, 0);
    for(int i = 0; i < n; i++){
        cin >> a[i];
        a[i] = (ll)(1e9+7) - a[i];
    }
    sort(a.begin(), a.end());

    ll res = p;
    ll cum = a[p-1];
    for(int i = p; i < n; i++){
        ll y = p-1 + (n - i - 1);
        ll target = a[i] - m;
        if(a[p-1] < a[i] - m || cum - target * (n-y-1) < (v - y - 1) * m){
            break;
        }
        else{
            res++;
            cum += a[i];
        }
    }

    cout << res << endl;


    return 0;
}


Measured memory_bytes: 1024
n = int(raw_input())
values = [ int(raw_input()) for i in range(n) ]
place = { x: i for i, x in enumerate(sorted(values)) }
count = 0
for i, x in enumerate(values):
    if i % 2 != place[x] % 2:
        count += 1
result = count // 2 + count % 2
print(result)


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

int main(){
  string s;
  int n,a,b,i,j;
  cin >> a >> b;
  n = b-a;
  j=0;
  for(i=0;i<=n;i++){
    j+=i;
  }
  cout << j-b << endl;
  return 0;
}


Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef vector<LL> VI;
typedef vector<VI> VVI;
typedef pair<LL, LL> PII;
typedef vector<string> VS;
inline LL toInt(string s) {LL v; istringstream sin(s);sin>>v;return v;}
template<class T> inline string toString(T x) {ostringstream sout;sout<<x;return sout.str();}
#define PB push_back
#define MP make_pair
#define SZ(a) LL((a).size())
#define EACH(x,c) for(auto x: (c))
#define EXIST(s,e) ((s).find(e)!=(s).end())
#define REVERSE(c) reverse((c).begin(), (c).end())
#define SORT(c) sort((c).begin(),(c).end())
#define RSORT(c) sort((c).rbegin(),(c).rend())
#define STABLE_SORT(c) stable_sort((c).begin(),(c).end())
#define STABLE_RSORT(c) stable_sort((c).rbegin(),(c).rend())
#define UNIQUE(c) SORT(c); (c).erase(unique((c).begin(), (c).end()), (c).end());
#define FOR(i,a,b) for(LL i=(a);i<(b);++i)
#define REP(i,n)  FOR(i,0,n)
const double INF = 1e7;
const double EPS = 1e-10;
const double PI  = acos(-1.0);
#define dump(x)  cerr << #x << " = " << (x) << endl;


int main() {
	LL s;
	cin >> s;

	set<LL> val;
	LL prev;
	LL n;
	for (n = 1; n <= 1000000; n++) {
		LL v;
		if (n == 1) {
			v = s;
		} else if (prev % 2 == 0) {
			v = prev / 2;
		} else {
			v = 3 * prev + 1;
		}
		if (val.count(v) > 0) {
			break;
		} else {
			val.insert(v);
			prev = v;
		}
	}

	cout << n << endl;
	
    return 0;
}


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

int main()
{
    string s;
    vector<int> count(26);
    
    while(cin >> s){
        for(char c : s){
            if(c >= 'a' && c <= 'z') count[c - 'a']++;
            else if(c >= 'A' && c <= 'Z') count[c - 'A']++;
        }
    }
    for(int i=0; i<26; ++i){
        cout << (char)('a' + i) << " : " << count[i] << endl;
    }
    return 0;
}


Measured memory_bytes: 3176
import java.util.Scanner;

class Main {
	public static void main(String[] args) {
		Scanner scan = new Scanner(System.in);
		
		int n;
		int a, b;
		int cardA, cardB;
		
		while(true){
			n = scan.nextInt();
			if(n == 0)break;
			cardA = 0;
			cardB = 0;
			
			
			for(int i = 0; i < n; i++){
				a = scan.nextInt();
				b = scan.nextInt();
				
				if(a > b)cardA += a + b;
				else if(a < b)cardB += a + b;
				else{
					cardA += a;
					cardB += b;
				}
			}
			System.out.println(cardA + " " + cardB);
		}
	}
}

Measured memory_bytes: 56276
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <algorithm>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <regex>
#include <numeric>
using namespace std;

/*--- Macro ---*/
#define rep(i, n) for(int i=0; i<(int)n; i++)
#define rep1(i, n) for(int i=1; i<(int)n; i++)
#define mp make_pair
#define pb push_back
#define all(a) a.begin(), a.end()

/*---  Type ---*/
typedef long long ll;

/*--- Constants ---*/
template <typename T> const T INF = numeric_limits<T>::max() / 10;
const int MOD = (int)1e9 + 7;

// Floating
const double EPS = 1e-11;
#define EQ0(x) (abs(x) < EPS)
#define EQ(x, y) (abs(a-b) < EPS)

// Util
int max(int a, int b, int c) {return max(a, max(b, c));}
int min(int a, int b, int c) {return min(a, min(b, c));}
ll max(ll x, ll y) {return x > y ? x : y;}
ll min(ll x, ll y) {return x < y ? x : y;}
ll max(ll a, ll b, ll c) {return max(a, max(b, c));}
ll min(ll a, ll b, ll c) {return min(a, min(b, c));}

// -------------------------------------------------------------------------------------



int main() {
  int n;
  cin >> n;
  vector<int> a(n+2);
  a[0] = 0, a[n+1] = 0;
  rep(i, n) {
    cin >> a[i+1];
  }

  // 全て訪れた時のコスト和
  ll _sum = 0;
  rep(i, n+1) {
    _sum += abs(a[i+1] - a[i]);
  }

  // i+1番目は訪れない
  rep1(i, n+1) {
    if(a[i-1] <= a[i] && a[i] <= a[i+1]) {
      cout << _sum << endl;
    } else {
      ll cost = _sum - (abs(a[i] - a[i-1]) + abs(a[i+1] - a[i])) + (abs(a[i+1] - a[i-1]));
      cout << cost << endl;
    }
  }


  return 0;
}

Measured memory_bytes: 1664
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

class Main {
	public static void main(String args[]) throws IOException {
		BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
		Solver solver = new Solver();
		solver.init();
		solver.readHead(in.readLine());
		// for (; solver.hasNext();) {
		solver.readBody(in.readLine());
		// }
		// solver.solve();
	}
}

class Solver {
	int N;
	int cnt;

	public void init() {
		N = 0;
		cnt = 0;
	}

	public void readHead(String str) {
		String[] strArr = str.split(" ");
		N = Integer.parseInt(strArr[0]);
		// System.out.println(N);
	}

	public boolean hasNext() {
		return cnt < N;
	}

	public void readBody(String str) {
		// System.out.println(str);
		String[] strArr = str.split(" ");
		for (int i = 0; i < N; i++) {
			if ("Y".equals(strArr[i])) {
				System.out.println("Four");
				return;
			}
		}
		System.out.println("Three");
		cnt++;
	}

	public void solve() {

	}
}


Measured memory_bytes: 21204
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<ctime>
using namespace std;
#define REP(i,st,ed) for(int i=st,i##end=ed;i<=i##end;++i)
#define DREP(i,st,ed) for(int i=st,i##end=ed;i>=i##end;--i)
#define Runtime 1.0*clock()/CLOCKS_PER_SEC
int read(){
	int x=0,f=1;
	char c=getchar();
	while((c<'0')||(c>'9')){if(c=='-')f=-1;c=getchar();}
	while((c>='0')&&(c<='9'))x=(x<<1)+(x<<3)+(c^48),c=getchar();
	return x*f;
}
const int maxn=2005;
int n,m,eu[maxn],ev[maxn];
int Begin[maxn],Next[maxn<<1],to[maxn<<1],e;
int sum=0,ans=1e9;
void add_edge(int u,int v){
	to[++e]=v,Next[e]=Begin[u],Begin[u]=e;
}
void dfs(int u,int fa,int dep){
	++sum;
	if(dep==m)return;
	for(int i=Begin[u];i;i=Next[i])
		if(to[i]!=fa)dfs(to[i],u,dep+1);
}
int main(){
	int u,v;
	n=read(),m=read();
	REP(i,1,n-1){
		u=read(),v=read();
		add_edge(u,v),add_edge(v,u);
		eu[i]=u,ev[i]=v;
	}
	if(m&1){
		m>>=1;
		REP(i,1,n-1){
			sum=0;
			dfs(eu[i],ev[i],0);
			dfs(ev[i],eu[i],0);
			ans=min(ans,n-sum);
		}
	}else{
		m>>=1;
		REP(i,1,n){
			sum=0;
			dfs(i,0,0);
			ans=min(ans,n-sum);
		}
	}
	printf("%d\n",ans);
	return 0;
}

Measured memory_bytes: 384
from collections import deque

S = input()
S = deque(S)
Q = int(input())
Query1 = 0
flag = False
for i in range(Q):
    Queryi = input().split()
    if Queryi[0] == '1':
        Query1 += 1
        flag = not flag
    else:
        if (Queryi[1] == '1') ^ flag:
            S.appendleft(Queryi[2])
        else:
            S.append(Queryi[2])

if Query1 % 2 != 0:
    S.reverse()

ans = ''.join(S)
print(ans)

Measured memory_bytes: 8436
#include<bits/stdc++.h>
#define pa pair<int,int>
#define pb push_back
#define se second
#define fi first
using namespace std;
const int N=2005;
bool vis[N],co[N];
int p[N],sz[N];
vector<pa> to[N];
int n,m,ans,rm[N];
vector<int> e[N];
pa get(int x){
	if (p[x]==x) return pa(x,0);
	pa pr=get(p[x]);
	pr.se^=co[x];
	return pr;
}
vector<pa> solve(int x){
	vis[x]=1;
	vector<pa> cur;
	for (int i=0;i<to[x].size();i++)
		if (rm[to[x][i].fi]!=x)
			cur.push_back(to[x][i]);
	for (int i=0;i<e[x].size();i++){
		int to=e[x][i];
		if (vis[to]) continue;
		vector<pa> tmp=solve(to);
		if (tmp.size()==1) ans++;
		else if (tmp.size()){
			ans+=2;
			map<int,int> mp;
			for (int j=0;j<tmp.size();j++){
				pa x=tmp[j];
				pa tt=get(x.fi);
				int c=tt.se^x.se;
				mp[tt.fi]|=(1<<c);
			}
			int fl=0;
			map<int,int>::iterator it;
			for (it=mp.begin();it!=mp.end();it++)
				if (it->second==3){
					fl=1; break;
				}
			if (!fl){
				it=mp.begin();
				pa x=*it,y=*(++it);
				if (sz[x.fi]>sz[y.fi]) swap(x,y);
				p[x.fi]=y.fi; sz[y.fi]+=sz[x.fi];
				co[x.fi]=((x.se!=y.se)^1);
			}
		}
		for (int j=0;j<tmp.size();j++)
			if (rm[tmp[j].first]!=x)
				cur.push_back(tmp[j]);
	}
	return cur;
}
int fa[N],T;
int IN[N],OUT[N];
bool isfa(int x,int y){
	return IN[x]<=IN[y]&&IN[y]<=OUT[x];
}
int lca(int x,int y){
	for (;!isfa(x,y);x=fa[x]);
	return x;
}
void dfs1(int x,int y){
	fa[x]=y; IN[x]=++T;
	for (int i=0;i<e[x].size();i++)
		if (e[x][i]!=y) dfs1(e[x][i],x);
	OUT[x]=++T;
}
int ga[N],gb[N];
int main(){
	scanf("%d%d",&n,&m);
	for (int i=1;i<n;i++){
		int x,y;
		scanf("%d%d",&x,&y);
		e[x].pb(y);	e[y].pb(x);
	}
	dfs1(1,0);
	for (int i=1;i<=m;i++){
		scanf("%d%d",&ga[i],&gb[i]);
		to[ga[i]].pb(pa(i,0));
		to[gb[i]].pb(pa(i,1));
		rm[i]=lca(ga[i],gb[i]);
	}
	for (int i=1;i<=m;i++)
		p[i]=i,sz[i]=1;
	solve(1);
	printf("%d\n",ans);
	for (int i=1;i<=m;i++){
		pa t=get(i);
		if (t.se==0) printf("%d %d\n",ga[i],gb[i]);
		else printf("%d %d\n",gb[i],ga[i]);
	}
}

Measured memory_bytes: 896
# cf17-final
def main():
    N, *D = map(int, open(0).read().split())
    D.append(0)
    D.sort()
    for i in range(1, N + 1):
        if i & 1 == 0:
            D[i] = 24 - D[i]
    D.sort()
    ans = min(j - i for i, j in zip(D, D[1:]))
    print(ans)


if __name__ == "__main__":
    main()

Measured memory_bytes: 3064
a = list(map(int, input().split(" ")))
A=a[0]
B=a[1]

if A+B>=A-B and A+B>=A*B:
	print(A+B)
elif A-B>=A+B and A-B>=A*B:
	print(A-B)
else:
	print(A*B)


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

long long T;
long long N, A[1 << 18];
char C[1 << 18];

vector<long long> Gauss(vector<long long> F) {
	sort(F.begin(), F.end());
	reverse(F.begin(), F.end());

	for (int i = 59; i >= 0; i--) {
		int cl = F.size();
		for (int j = 0; j < F.size(); j++) {
			if (F[j] < (1LL << (i + 1))) { cl = j; break; }
		}
		if (cl == F.size() || (F[cl] / (1LL << i)) % 2LL == 0) continue;
		for (int j = 0; j < F.size(); j++) {
			if (j == cl) continue;
			if ((F[j] / (1LL << i)) % 2LL != 0) F[j] ^= F[cl];
		}

		sort(F.begin(), F.end());
		reverse(F.begin(), F.end());
	}
	while (F.size() >= 1 && F[F.size() - 1] == 0) F.pop_back();
	return F;
}

int main() {
	cin >> T;
	for (int i = 1; i <= T; i++) {
		cin >> N;
		for (int j = 1; j <= N; j++) cin >> A[j];
		for (int j = 1; j <= N; j++) cin >> C[j];

		vector<long long> V; bool flag = false;
		for (int j = N; j >= 1; j--) {
			if (C[j] == '0') {
				V.push_back(A[j]);
				V = Gauss(V);
			}
			else {
				vector<long long> V2 = V;
				V2.push_back(A[j]);
				V2 = Gauss(V2);
				if (V.size() != V2.size()) flag = true;
			}
		}

		if (flag == true) cout << "1" << endl;
		else cout << "0" << endl;
	}
	return 0;
}

Measured memory_bytes: 3584
n,w=map(int,input().split())
items=[]
dp=[0 for i in range(w+1)]
for i in range(n):
    wi,vi=map(int,input().split())
    for j in range(w-wi,-1,-1):
        dp[j+wi]=max(dp[j+wi],dp[j]+vi)
print(max(dp))


Measured memory_bytes: 44912
#include <algorithm>
#include <cassert>
#include <cstring>
#include <iostream>

using namespace std;

#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define TRACE(x) cout << #x << " = " << x << endl
#define _ << " _ " <<

typedef long long llint;

const int mod = 1e9 + 7;
const int MAX = 5050;

inline int add(int a, int b) {
  return a+b >= mod ? a+b-mod : a+b;
}

inline int sub(int a, int b) {
  return a >= b ? a-b : a-b+mod;
}

inline int mul(int a, int b) {
  return llint(a)*b % mod;
}

int c[MAX][MAX];
int g[MAX][MAX];

int f(int A, int B, int C) {
  if (B % 2) return 0;

  int kb = B / 2;
  int ans = 0;
  REP(ka, A + 1) {
    int kc = A - ka;
    for (int kd = 0; kc + kd*3 <= C; ++kd) {
      int ways = 1;
      ways = mul(ways, c[ka+kb+kc+kd][ka]);
      ways = mul(ways, c[kb+kc+kd][kb]);
      ways = mul(ways, c[kc+kd][kc]);
      ways = mul(ways, g[kb][C-kc-kd*3]);
      ans = add(ans, ways);
    }
  }
  return ans;
}

int main(void) {
  REP(i, MAX) {
    c[i][0] = 1;
    FOR(j, 1, i+1)
      c[i][j] = add(c[i-1][j], c[i-1][j-1]);
  }

  REP(i, MAX) {
    REP(j, MAX) {
      if (j == 0) { g[i][j] = 1; continue; }
      if (i == 0) continue;
      g[i][j] = add(g[i][j-1], g[i-1][j]);
    }
  }
  
  int N, A, B, C;
  scanf("%d %d %d %d", &N, &A, &B, &C);
  printf("%d\n", f(A, B, C));
  return 0;
}


Measured memory_bytes: 167808
#include<bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
int main(){
  long double a,b,h,w;
  cin >> a >> b >> h >> w;
  long double k=h*60+w;
  long double ax,ay,bx,by;
  long double pi=3.141592653589793;
  ax=a*sin(pi*k/360);
  bx=b*sin(pi*w/30);
  by=b*cos(pi*w/30);
  ay=a*cos(pi*k/360);
  cout << fixed << setprecision(10);
  cout << sqrt((ax-bx)*(ax-bx)+(ay-by)*(ay-by)) << endl;
}


Measured memory_bytes: 4164
#include<stdio.h>
int main(void)
{
	int z1,m1,s1,e1,z2,m2,s2,e2,s,t;
	scanf("%d %d %d %d",&z1,&m1,&s1,&e1);
	scanf("%d %d %d %d",&z2,&m2,&s2,&e2);
	s=z1+m1+s1+e1;
	t=z2+m2+s2+e2;
	if(s>t) {
		printf("%d\n",s);
	}
	else if(s<t) {
		printf("%d\n",t);
	}
	else {
		printf("%d\n",s);
	}
	return 0;
}

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

#define ll long long

using namespace std;

//memset(dp, 0, sizeof(dp));

void work(int T) {
	ll n;
	cin >> n;
	vector<int> e(n);
	for(int i = 0; i < n; i++) {
		cin >> e[i];
	}
	int sum = 0;
	for(auto &i : e) {
		sum ^= i;
	}
	for(int i = 0; i < n; i++) {
		cout << (sum ^ e[i]) << " ";
	}
	cout << endl;
	
	return;
}

int main() {
	ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.precision(10);
    cout << fixed;
	// int t;
	// for(cin >> t; t > 0; t--) {
		// work(t);
	// }
	work(0);
	return 0;
};


Measured memory_bytes: 3900
//#pragma GCC optimize("Ofast")
//#pragma GCC optimize ("unroll-loops")
//#pragma GCC target("avx,avx2,fma")
#include<bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
#define mod 1000000007
#define fi first
#define se second
#define pii pair<int,int>
#define int long long
#define endl '\n'
#define pll pair<long long,long long>
#define LONGLONG_MAX 100000000000000
using namespace std;
template <typename T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
long long power(long long a,long long b){
    long long ans=1;
    while(b>0){
        if(b&1){ans=(ans*a)%mod;}
                a=(a*a)%mod;
                b>>=1;
        }
    return ans;
        
}
pii dp[405][408];
bool b[405][405];
pii fun(int i,int j,int a[]){
    if(i==j)return {0,a[i]};
    if(b[i][j])return dp[i][j];
    b[i][j]=true;
    int ans=LLONG_MAX,y=0;
    for(int k=i;k<j;k++){
        if(ans>fun(i,k,a).fi+fun(k+1,j,a).fi+fun(i,k,a).se+fun(k+1,j,a).se){
            ans=fun(i,k,a).fi+fun(k+1,j,a).fi+fun(i,k,a).se+fun(k+1,j,a).se;
            y=fun(i,k,a).se+fun(k+1,j,a).se;
        }
        //cout<<y<<" ";
    }
    //cout<<y<<endl;
    return dp[i][j]={ans,y};
}
int32_t main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    memset(b,false,sizeof(b));
    int n;
    cin>>n;
    int a[n];
    for(int i=0;i<n;i++){
        cin>>a[i];
    }
    cout<<fun(0,n-1,a).fi;
    return 0;
}

Measured memory_bytes: 2816
#include <bits/stdc++.h>
using namespace std;
typedef long long lint;
typedef pair<int, int> pi;

int n, a[100005];

int main(){
	scanf("%d",&n);
	for(int i=0; i<n; i++) scanf("%d",&a[i]);
	int ans = 0;
	for(int i=0; i<n; ){
		if(i == n-1){
			ans++;
			break;
		}
		int e = i+1;
		bool up = (a[i] <= a[i+1]);
		bool dn = (a[i] >= a[i+1]);
		while(e+1 <= n && (up || dn)){
			up &= (a[e] <= a[e+1]);
			dn &= (a[e] >= a[e+1]);
			e++;
		}
		i = e;
		ans++;
	}
	cout << ans;
}


Measured memory_bytes: 640
#include <bits/stdc++.h>
using namespace std;
#include <math.h>
#include <iomanip>
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; }

const int mod=1000000007;

int main() {
  int64_t N,A,B;
  cin>>N>>A>>B;
  if(A%2==B%2){cout<<abs(A-B)/2<<endl;}
  else{
    int64_t d=1+A-1+(B-A-1)/2;
    int64_t u=1+N-B+(B-A-1)/2;
    cout<<min(d,u)<<endl;
  }
  return 0;
}

Measured memory_bytes: 3592
import java.util.*;
import static java.lang.Integer.*;
//import static java.lang.Long.*;
//import static java.lang.Math.*;
import static java.lang.System.*;

public class Main {
	public static void main(String[] args) {
		int j;
		Scanner sc = new Scanner(in);
		int n = parseInt(sc.next());
		int i = parseInt(sc.next());
		sc.close();
		out.println(n-i+1);
	}
}


Measured memory_bytes: 22996
import java.io.IOException;
import java.util.Scanner;

public class Main {

	public static void main(String[] args) throws IOException {
		new Main().run();
	}

	private void run() throws IOException {
		Scanner scanner = new Scanner(System.in);
		while (true) {
			int n = scanner.nextInt();
			if (n == 0)
				break;
			int m = scanner.nextInt();
			slove(n, m);
		}

	}

	private void slove(int n, int m) {
		if (n > m) {
			int tmp = m;
			m = n;
			n = tmp;
		}
		int step = 1;
		while (true) {
			if (m % n == 0)
				break;
			int tmp2 = n;
			n = m % n;
			m = tmp2;
			step++;
		}
		System.out.println(n + " " + step);

	}
}

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

int ask(int v){ //席vについて尋ねる
  
  cout << v << endl;
  fflush(stdout);
  string s; cin >> s;
  if( s == "Vacant"){ exit(0);}
  if( s == "Male"){ return 1;}
  else{ return 2;}
  
}

int main(){
  int N; cin >> N;
  int left = 0; int right = N-1; 
  
  vector<int> gender(N,0); //0は未定
  
  gender[0] = ask(0);
  gender[N-1] = ask(N-1);
  
  while(right-left > 1){
    
    int mid = (right+left)/2;
    
    gender[mid] = ask(mid);
    
    if( abs(gender[mid]-gender[left])!= abs(left-mid)%2){ right = mid;}
    //両端が同性でかつ間の長さが奇数長のときに、反対側に進む
    else{ left = mid;}
      
    
    
  }

  
  return 0;
}

Measured memory_bytes: 1236
#include<stdio.h>
  
#define MAX 500000
  
int Tree[MAX];  
int n;
  
void maxHeapify(int i){
  int largest ,exchange, l, r;
  l = getLeft(i);
  r = getRight(i);
    
  if((n+1 >l) && (Tree[l] > Tree[i])) largest = l;
  
  else largest = i;
    
  if((n+1 > r) && (Tree[r] > Tree[largest])) largest = r;
  
  if (largest != i){
  
    exchange = Tree[i];
    Tree[i] = Tree[largest];
    Tree[largest] = exchange;
    maxHeapify(largest);
  
  } 
}
  
void buildMaxHeap(){
  int i;
  for (i=(n/2); i+1>1; i--){
    maxHeapify(i);
     }
}
   
int getLeft(int i){
  return (2*i);
}
  
int getRight(int i){
  return (2*i + 1);
}
int getParent(int i){
  return i/2;
}
 
  
main()
{
  int i;
  scanf("%d",&n);
  
  for(i=1; i<=n; i++){
  
    scanf("%d",&Tree[i]);
  
  }
  
  buildMaxHeap();
  
  for(i=1; i<=n; i++){
      
    printf(" %d",Tree[i]);
  
  }
  
  printf("\n");
  
  return 0;
}


Measured memory_bytes: 3632
#include <iostream>
using namespace std;
static const int INFTY=(1<<21);

int main() {
  std::ios::sync_with_stdio(false);
  cout.tie(0);
  cin.tie(0);
  int n;
  cin>>n;
  int p[n+1];
  for (int i=1; i<=n; i++) {
    cin>>p[i-1]>>p[i];
  }
  int m[n+1][n+1];
  for (int i=1; i<=n; i++) {
    m[i][i]=0;
  }
  for (int l=2; l<=n; l++) {
    for (int i=1; i<=n-l+1; i++) {
      int j=i+l-1;
      m[i][j]=INFTY;
      for (int k=i; k<=j-1; k++) {
        m[i][j]=min(m[i][j],m[i][k]+m[k+1][j]+p[i-1]*p[k]*p[j]);
      }
    }
  }
  cout<<m[1][n]<<endl;
  return 0;
}



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

int main() {
  string s;
  cin >> s;
  vector<char> c;
  for(int i = 0; i < s.size(); i++){
    if(s.at(i) == '0'){
      c.push_back('0');
    }
    if(s.at(i) == '1'){
      c.push_back('1');
    }
    if(s.at(i) == 'B'){
      if(c.empty() == false){
        c.pop_back();
      }
    }
  }

  for(int i = 0; i < c.size(); i++){
    cout << c.at(i);
  }
}

Measured memory_bytes: 256
#include <iostream>
#include <algorithm>
using namespace std;
typedef long long ll;


int main(void){
  ll n,k,i,j,a[2003],me[2003]={},mv[2003]={},ans=0;
  const int mod=1e9+7;
  
  cin >> n >> k;
  for (i=0;i<n;i++) cin >> a[i];
  
  for (i=0;i<n-1;i++) for (j=i+1;j<n;j++){
    if (a[i]>a[j]) me[i]++;
  }
  
  for (i=0;i<n;i++) for (j=0;j<n;j++){
    if (i==j) continue;
    if (a[i]>a[j]) mv[i]++;
  }
  
  for (i=0;i<n;i++){
    ans=(ans+k*(k-1)/2%mod*mv[i]%mod+me[i]*k%mod)%mod;
  }
  
  cout << ans << endl;
  
  return 0;
}

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

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

void fastIO()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
}

vector<long long> arr(200005);
vector<bool> visited(200005);
vector<long long> dis(200005);
vector<long long> disc(200005);

long long n, k;

void bfs(long long v, long long n){
    visited[v] = true;

    if(!visited[arr[v]]){
        dis[arr[v]]=dis[v]+1;
        disc[dis[arr[v]]] = arr[v];
        if(dis[arr[v]] == k){
            cout << arr[v] << '\n';
            return;
        }
        bfs(arr[v], n);
    }
    else{
        //cout << dis[arr[v]] << " " << dis[v] << '\n';
        k-=dis[arr[v]];
        long long dist = dis[v]-dis[arr[v]]+1;
        cout << disc[k%dist+dis[arr[v]]] << '\n';
        return;
    }

}

int main()
{
    //textIO();
    fastIO();

    cin >> n >> k;

    for(int i=1;i<=n;i++){
        cin >> arr[i];
    }
    disc[0] = 1;
    bfs(1, n);


    return 0;
}


Measured memory_bytes: 12908
#include<stdio.h>
int main(){
    int x[100],a[100],t[2020],n,m,i;
    for(i=0;i<2020;i++){
        t[i]=0;
    }
    scanf("%d",&n);
    for(i=0;i<n;i++){
        scanf("%d",&x[i]);
        t[x[i]]=1;
    }
    scanf("%d",&m);
    for(i=0;i<m;i++){
        scanf("%d",&a[i]);
        if(t[x[a[i]-1]+1]==0 && x[a[i]-1]<2019){
            t[x[a[i]-1]+1]=1;
            t[x[a[i]-1]]=0;
            x[a[i]-1]+=1;
        }
    }
    for(i=0;i<n;i++){
        printf("%d\n",x[i]);
    }
}



Measured memory_bytes: 2104
#include <bits/stdc++.h>
using namespace std;
using ll=long long;
using Graph=vector<vector<int>>;
#define MOD 1000000007
#define INF 1000000000000

int main(){
  int H,W,K;
  cin>>H>>W>>K;
  vector<vector<ll>> dp(H+1,vector<ll>(W,0));
  dp.at(0).at(0)=1;
  ll number[8]={1,1,2,3,5,8,13,21};
  for(int i=1;i<=H;i++){
    if(W==1){
      dp.at(i).at(0)=dp.at(i-1).at(0);
    }else{
      dp.at(i).at(0)+=dp.at(i-1).at(0)*number[W-1];
      dp.at(i).at(0)+=dp.at(i-1).at(1)*number[W-2];
      dp.at(i).at(0)%=MOD;
      for(int j=1;j<W-1;j++){
        dp.at(i).at(j)+=dp.at(i-1).at(j)*number[j]*number[W-j-1];
        dp.at(i).at(j)+=dp.at(i-1).at(j-1)*number[j-1]*number[W-j-1];
        dp.at(i).at(j)+=dp.at(i-1).at(j+1)*number[j]*number[W-j-2];
        dp.at(i).at(j)%=MOD;
      }
      dp.at(i).at(W-1)+=dp.at(i-1).at(W-1)*number[W-1];
      dp.at(i).at(W-1)+=dp.at(i-1).at(W-2)*number[W-2];
      dp.at(i).at(W-1)%=MOD;
    }
  }
  cout<<dp.at(H).at(K-1)<<endl;
}


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

int main()
{
    std::vector<int> A;
    int N;
    std::cin >> N;
    for(int i=0; i<N; i++){
        int temp;
        std::cin >> temp;
        A.push_back(temp);
    }

    std::vector<int> sortedA = A;
    std::sort(sortedA.begin(), sortedA.end());
    std::reverse(sortedA.begin(), sortedA.end());

    // for(auto e : A) std::cout << e << " ";
    // std::cout << std::endl;
    // for(auto e : sortedA) std::cout << e << " ";
    // std::cout << std::endl;

    for(int i=0; i<N; i++){
        if(A[i] == sortedA[0]){
            std::cout << sortedA[1] << std::endl;
        }else{
            std::cout << sortedA[0] << std::endl;
        }

    }

    return 0;
}

Measured memory_bytes: 3316
N = int(input())
location = []
sorted_location = []
score = []

for i in range(N):
	a, b = input().split()
	location.append(a)
	score.append(int(b))

#	print('location:' + str(location))
#	print('score:' + str(score))

sorted_location = location[:]
sorted_location.sort()
#print(location)
#print(sorted_location)

a = [] # 掲載順
for i in range(N):
	# 対象の地名、スコアを格納
	l = location[i]
	s = score[i]

#	print('l:' + l)
#	print('s:' + str(s))

	# 地名のスコアを格納
	score_num = [j for j, x in enumerate(location) if x == l]
	score_list = []
	for j in score_num:
		score_list.append(score[j])
	score_list.sort(reverse=True)

#	print('score_list:' + str(score_list))
	
	# 対象の場所の1番最初の順番
	loc1 = sorted_location.index(l) + 1
#	print('loc1:' + str(loc1))

	# 対象の地名の中で、対象のスコアが何番目か
	loc2 = score_list.index(s)
#	print('loc2:' + str(loc2))

	# 対象の掲載順を格納
	a.append(loc1 + loc2)

# 掲載順を出力
for i in range(N):
	print(a.index(i+1) + 1)


Measured memory_bytes: 3064
#include <bits/stdc++.h>
using namespace std;
#include <math.h>
#include <iomanip>
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; }
const int INF=1001001001;
vector<pair<int64_t,int64_t>>pppp(int64_t x){
  vector<pair<int64_t,int64_t>>p;
  for(int64_t i=2;i*i<=x;i++){
    int cnt=0;
    if(x%i==0){
    while(x%i==0){cnt++;x/=i;}
    p.push_back(make_pair(i,cnt));
    }
  }
  if(x!=1){p.push_back(make_pair(x,1));}
  return p;
}
 
int main() {
 string s;
 cin>>s;
 map<char,int>c;
 c['a'],c['b'],c['c'];
 for(int i=0;i<s.size();i++){
  c[s[i]]++;
 }
 int MAX=0,MIN=9999999;
 for(auto d:c){
   char a=d.first;
   int b=d.second;
   MAX=max(b,MAX);
   MIN=min(b,MIN);
 }
 if(MAX-MIN<=1){cout<<"YES"<<endl;}
 else{cout<<"NO"<<endl;}
  return 0;
}

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

int bubblesort(vector<int> &A, int N)
{
    int cnt = 0;

    bool flag = true;
    while (flag)
    {
        flag = false;
        for (int j = N - 1; j > 0; j--)
        {
            if (A.at(j) < A.at(j - 1))
            {
                swap(A.at(j), A.at(j - 1));
                cnt++;
                flag = true;
            }
        }
    }
    return cnt;
}
int main()
{
    int N;
    cin >> N;

    vector<int> A(N);
    int n;
    for (int i = 0; i < N; i++)
    {
        cin >> A.at(i);
    }

    int cnt = bubblesort(A, N);

    for (int i = 0; i < A.size() - 1; i++)
    {
        cout << *(A.begin() + i) << ' ';
    }
    cout << A.back() << endl;

    cout << cnt << endl;
}


Measured memory_bytes: 3096
import java.util.*;
import java.io.*;
import java.lang.*;

public class Main {
    static final int INF = 1000000000;     
    
    public static void main(String[] args) throws Exception {                
        Scanner input = new Scanner(System.in);
        int n, q;
        n = input.nextInt();
        q = input.nextInt(); 
        Queue<Process> queue = new ArrayDeque<Process>();
        for (int i = 0; i < n; ++i) {
            String name = input.next();
            int time = input.nextInt();
            Process element = new Process(name, time);
            queue.add(element);
        }
        ArrayList<Process> res = new ArrayList<Process>();
        int time = 0;
        while (!queue.isEmpty()) {
            Process element = queue.poll();
            if (element.getTime() <= q) {
                time += element.getTime();
                res.add(new Process(element.getName(), time));
            } else {
                time += q;
                element.setTime(element.getTime() - q);
                queue.add(element);
            }
        }
        for (Process element : res) {
            System.out.println(element.getName() + " " + element.getTime());
        }
    }                
}

class Process {
    String name;
    int time;
    public Process() {
        name = "";
        time = 0;
    }
    public Process(String name, int time) {
        this.name = name;
        this.time = time;
    }        

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getTime() {
        return time;
    }

    public void setTime(int time) {
        this.time = time;
    }
}

Measured memory_bytes: 68756
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define mp make_pair
#define pb push_back
#define ff first
#define ss second
#define N 200005
ll mod=998244353;
#define fast ios_base::sync_with_stdio(false);cin.tie(NULL)
#define all(x) x.begin(),x.end()
#define lb lower_bound
ll powermodm(ll x,ll n,ll M){ll result=1;while(n>0){if(n % 2 ==1)result=(result * x)%M;x=(x*x)%M;n=n/2;}return result;}
ll power(ll _a,ll _b){ll _r=1;while(_b){if(_b%2==1)_r=(_r*_a);_b/=2;_a=(_a*_a);}return _r;}
int main()
{
    fast;
    ll n;
    cin>>n;
    string s;
    cin>>s;
    int c1=0,c2=0;
    for(int i=0;i<n;i++)
    {
        if(s[i]=='R')
        {
            c1++;
        }
        else
        {
            c2++;
        }
    }
    if(c1>c2)
    {
        cout<<"Yes\n";
    }
    else
    {
        cout<<"No\n";
    }
    return 0;
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
#define FOR(i, a, b) for(int i = a; i < b; i++)
#define REP(i, n) FOR(i, 0, n)
#define INF 999999999
typedef long long ll;
using namespace std;
int main() {
    int A, B;
    string S;
    cin >> A >> B;
    cin >> S;
    bool flag = true;
    if(S[A] != '-') flag = false;
    REP(i, A) {
        if(S[i] == '-') flag = false;
    }
    FOR(i, A+1, A+B+1) {
        if(S[i] == '-') flag = false;
    }
    if(flag) puts("Yes");
    else puts("No");
}

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

int main()
{
	int a1, a2, a3;
	cin >> a1 >> a2 >> a3;

	if (a1 + a2 + a3 >= 22)
	{
		cout << "bust" << endl;
	}
	else
	{
		cout << "win" << endl;
	}
	return 0;
}

Measured memory_bytes: 256