akhauriyash/Code-Regression huggingface dataset:
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <vector>
#include <cstring>
#include <queue>
#include <functional>
#include <set>
#include <map>
#include <string>
#include <cmath>
#include <cassert>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
#define repd(i, n) for (int i = n; i >= 1; i--)
#define mod 1000000007
#define MOD 998244353
#define inf 1e20
typedef long long int ll;
typedef pair<ll, ll> p;


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

	string ans = "No";
	if (a + b >= c) {
		ans = "Yes";
	}
	cout << ans << endl;
	return 0;
}

//Ctrl+Aを使わない

Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
#define ll long long
ll mp[2][2002][2002];
int n, k;

ll imos(int c, int x, int y){
    int dx = x + k;
    int dy = y + k;
    x = max(0, min(2*k+1, x));
    y = max(0, min(2*k+1, y));
    dx = max(0, min(2*k+1, dx));
    dy = max(0, min(2*k+1, dy));
    return mp[c][y][x]-mp[c][y][dx]-mp[c][dy][x]+mp[c][dy][dx];
}
ll cnt(int x, int y){
    ll sum = 0;
    for(int i=0;i<4;i++){
        for(int j=0;j<4;j++){
            sum += imos((i+j)%2, x + k*(j-2), y + k*(i-2));
        }
    }
    return sum;
}
int main(){
    cin>>n>>k;
    for(int i=0;i<n;i++){
        int x, y;
        char c;
        cin>>x>>y>>c;
        mp[(c=='W'?1:0)][(y%(k*2))+1][(x%(k*2))+1]++;
    }
    for(int y = 2000; y >= 0; y--){
        for(int x = 2000; x >= 0; x--){
            mp[0][y][x] += mp[0][y][x+1];
            mp[1][y][x] += mp[1][y][x+1];
        }
    }
    for(int x=2000;x>=0;x--){
        for(int y=2000;y>=0;y--){
            mp[0][y][x] += mp[0][y+1][x];
            mp[1][y][x] += mp[1][y+1][x];
        }
    }
    ll ans = 0;
    for(int y=1;y<=k*2;y++){
        for(int x=1;x<=k*2;x++){
            ans = max(ans, cnt(x, y));
        }
    }
    cout<<ans<<endl;
    return 0;
}

Measured memory_bytes: 62976
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ABC138A
{
    class Program
    {
        static void Main(string[] args)
        {
            int a;
            string s;
            a = int.Parse(Console.ReadLine());
            s = Console.ReadLine();
            if(a>=3200)
            {
                Console.WriteLine(s);
            }
            else
            {
                Console.WriteLine("red");
            }
            Console.ReadKey();
                
                
        }
    }
}

Measured memory_bytes: 11092
void main(){
  import std.stdio, std.string, std.conv, std.algorithm;

  int n, k; rd(n, k);
  auto x=new long[](n), y=new long[](n);
  foreach(i; 0..n) rd(x[i], y[i]);

  long mn=5_000_000_000_000_000_000;
  foreach(i1; 0..n)foreach(i2; (i1+1)..n){
    foreach(j1; 0..n)foreach(j2; (j1+1)..n){
      auto x1=x[i1], x2=x[i2];
      auto y1=y[j1], y2=y[j2];
      if(x1>x2) swap(x1, x2);
      if(y1>y2) swap(y1, y2);
      if((x2-x1)*(y2-y1)>=mn) continue;
      int cnt=0;
      foreach(t; 0..n){
        if(x1<=x[t] && x[t]<=x2 && y1<=y[t] && y[t]<=y2) cnt++;
      }
      if(cnt>=k) mn=min(mn, (x2-x1)*(y2-y1));
    }
  }
  writeln(mn);
}

void rd(T...)(ref T x){
  import std.stdio, std.string, std.conv;
  auto l=readln.split;
  foreach(i, ref e; x){
    e=l[i].to!(typeof(e));
  }
}


Measured memory_bytes: 256
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int maxn = 100005;
int a[maxn];
int preffix[maxn], suffix[maxn];

int main()
{
  // freopen("in.txt", "r", stdin);
    ios_base::sync_with_stdio(false);
    cin.tie(0);

    int n;
    cin >> n;
    for(int i = 1; i <= n; i++){
        cin >> a[i];
        preffix[i] = __gcd(preffix[i-1], a[i]);
      //  cout << preffix[i] << endl;
    }
    suffix[n] = a[n];
    for(int i = n - 1; i >= 1; i--){
        suffix[i] = __gcd(suffix[i+1],a[i]);
      //  cout << suffix[i] << endl;
    }
    int ans = 0;
    for(int i = 1; i <= n; i++){
        int x = __gcd(preffix[i-1], suffix[i+1]);
       // cout << x << endl;
       ans = max(ans, x);
    }
    cout << ans << endl;
    return 0;
}



Measured memory_bytes: 1408
#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
#include <iomanip>

using namespace std;

struct union_find {
    std::vector<int> data;
    std::vector<int> rank;
    int num_union;

    union_find(int size) : data(size+1, 0), rank(size+1, 0), num_union(size) {
        init();
    }

    void init() {
        for (int i=1; i<data.size(); ++i) {
            data[i] = i;
            rank[i] = 0;
        }
    }

    int find_root(int x) {
        if (data[x] == x) {
            return x;
        } else {
            return data[x] = find_root(data[x]);
        }
    }

    void unite(int x, int y) {
        x = find_root(x);
        y = find_root(y);
        if (x == y) return;

        if (rank[x] < rank[y]) {
            data[x] = y;
        } else {
            data[y] = x;
            if (rank[x] == rank[y]) ++rank[x];
        }
        --num_union;
    }

    bool same(int x, int y) {
        return find_root(x) == find_root(y);
    }
};


int main(int argc, char** argv) {
    int n, m;
    cin >> n >> m;
    union_find uf(n);

    for (int i=0; i< m; ++i) {
        int x, y, z;
        cin >> x >> y >> z;
        uf.unite(x, y);
    }

    cout << uf.num_union << endl;
    return 0;
}

Measured memory_bytes: 1024
#Union Find

#xの根を求める
def find(x):
    if par[x] < 0:
        return x
    else:
        par[x] = find(par[x])
        return par[x]
#xとyの属する集合の併合
def unite(x,y):
    x = find(x)
    y = find(y)
    
    if x == y:
        return False
    else:
        #sizeの大きいほうがx
        if par[x] > par[y]:
            x,y = y,x
        par[x] += par[y]
        par[y] = x
        return True

#xとyが同じ集合に属するかの判定
def same(x,y):
    return find(x) == find(y)

#xが属する集合の個数
def size(x):
    return -par[find(x)]

#初期化
#根なら-size,子なら親の頂点

n,m = map(int,input().split())
par = [-1]*n
for i in range(m):
  a,b = map(int,input().split())
  unite(a-1,b-1)
  
ans = 0

for i in range(n):
  if par[i] < 0:
    ans += 1
print(ans-1)

Measured memory_bytes: 74560
#include <cmath>
#include <iostream>

using namespace std;

int main() {
  int N, T, A;
  cin >> N >> T >> A;

  int height;
  cin >> height;

  int ans = 1;
  double minDiff = abs(A - (T - height * 0.006));

  if (minDiff == 0) {
    cout << 1;
    return 0;
  }

  for (int i = 2; i <= N; i++) {
    cin >> height;
    double curDiff = abs(A - (T - height * 0.006));
    if (minDiff > curDiff) {
      ans = i;
      if (curDiff == 0) break;
      minDiff = curDiff;
    }
  }

  cout << ans;
  return 0;
}

Measured memory_bytes: 256
use proconio::{
   fastout, input,
   marker::{Bytes, Chars, Isize1, Usize1},
};

#[fastout]
fn main() {
   input! {
      mut n:usize
   }
   let table = String::from("zabcdefghijklmnopqrstuvwxy");
   let table = table.chars().collect::<Vec<_>>();
   let mut ans = vec![];
   while n > 0 {
      ans.push(table[n % 26]);
      n = (n + 25) / 26;
      if n == 0 {
         break;
      }
      n -= 1;
   }
   for i in ans.iter().rev() {
      print!("{}", i);
   }
   println!("");
}


Measured memory_bytes: 2172
#include <iostream>
#include <cstring>
using namespace std;
#define INF (1<<21)
int dp[101][2] , data[101][2];
bool al[101][2];
int n;
int dfs(int height,int bill){
	if(height >= n) return INF;
	else if(height == n-1 && data[height][bill] != 2) return 0;
	if(~dp[height][bill]) return dp[height][bill];
	
	if(al[height][bill]) return INF;
	else al[height][bill] = true;
	
	
	int ans = INF;
	
	if(data[height][bill] == 0 || (data[height][bill] == 1 && data[height+1][bill] != 1) ){
		ans = min( ans , dfs( height+0 , bill^1 ) + 1);
		ans = min( ans , dfs( height+1 , bill^1 ) + 1);
		ans = min( ans , dfs( height+2 , bill^1 ) + 1);
	}else if(data[height][bill] == 1){
		ans = min( ans , dfs( height+1 , bill   ) );
	}else if(data[height][bill] == 2){
		ans = min( ans , dfs( height-1 , bill   ) );
	}
	
	return dp[height][bill] = ans;
}

int main(){
	while(cin >> n , n){
		memset(dp,-1,sizeof(dp));
		memset(al,0,sizeof(al));
		for(int j = 0 ; j < 2 ; j++)
			for(int i = 0 ; i < n ; i++)
				cin >> data[i][j];
		int res = min( dfs(0,0) , dfs(0,1) );
		if(res == INF) cout << "NA" << endl;
		else cout << res << endl;
	}
}

Measured memory_bytes: 0
/*
                _____________   ______________       __
               / _________  /\ /_____   _____/\     / /\
              / /\       / /  \\    /  /\    \ \   / /  \
             / /  \_____/ /   / \__/  /  \____\/  / /   /
            / /   /    / /   /    /  /   /       / /   /
           / /   /    / /   /    /  /   /       / /   /
          / /   /    / /   /    /  /   /       / /   /
         / /___/____/ /   /    /  /   /       / /___/________
        /____________/   /    /__/   /       /______________/\
        \            \  /     \  \  /        \              \ \
         \____________\/       \__\/          \______________\/
           ___       ___               ___    ___________
          /  /\     /  /\             /  /\  /_______   /\
         /  /__\___/  /  \           /  /  \ \      /  /  \
        /____    ____/   /          /  /   /  \____/  /   /
        \   /   /\   \  /          /  /   /       /  /   /
         \_/   /  \___\/ ___      /  /   /       /  /   /
          /   /   /     /  /\    /  /   /       /  /   /
         /   /   /     /  /__\__/  /   /       /  /___/____
        /___/   /     /___________/   /       /___________/\
        \   \  /      \           \  /        \           \ \
         \___\/        \___________\/          \___________\/

          A FAN OF FIZZYDAVID

*/

#include <iostream>
#include <fstream>
#include <sstream>
#include <algorithm>
#include <cstdio>
#include <cassert>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <string>
#include <deque>
#include <queue>
#include <vector>
#include <list>
#include <map>
#include <set>
#include <bitset>
#include <stack>
#include <iomanip>
#include <utility>
#include <functional>
using namespace std;
//orz jzm
#define ll long long
#define ull unsigned ll
#define orz int
#define yjz main
#define fizzydavid return
#define ak 0
#define la ;
#define plusonesecond {ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);}
#define minusonesecond exit(0)
const double EPS = 1e-8;
const ll PR = 19260817;
const ll inv2 = 500000004;
const ll inv3 = 333333336;
const ll mod = 1000000007;
orz yjz() {
	char s[2][55];
	int n;
	cin>>n;
	cin>>s[0];
	cin>>s[1];
	ll ret=1;
	int last=-1;
	for(int i=0; i<n; i++) {
		if(s[0][i]!=s[1][i]) { //0
			if(last==-1) ret=ret*6%mod;
			else if(last==0) ret=ret*3%mod;
			else ret=ret*2%mod;
			last=0;
			i++;
		} else { //1
			if(last==-1) ret=ret*3%mod;
			else if(last==0) ret=ret*1%mod;
			else ret=ret*2%mod;
			last=1;
		}
	}
	cout<<ret;
	fizzydavid ak la
}

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

int main() {
  int A, B;
  int cnt =0;
  
  cin >> A >> B;
  
  for(int i=A; i <=B; i++){
    bool ans = (i/10000 == i%10) && (i/1000%10 == i/10%10);
    if(ans) cnt++;
  }
  cout << cnt << endl;
}


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

int a[100005],b[100005],c[100005];
int dpa[100005],dpb[100005],dpc[100005];

int main(){
	int n;
	cin>>n;
	for(int i=1;i<=n;++i){
		cin>>a[i]>>b[i]>>c[i];
	}	

	dpa[1]=a[1];
	dpb[1]=b[1];
	dpc[1]=c[1];
	for(int i=2;i<=n;++i){
		dpa[i]=max(dpb[i-1],dpc[i-1])+a[i];
		dpb[i]=max(dpa[i-1],dpc[i-1])+b[i];
		dpc[i]=max(dpa[i-1],dpb[i-1])+c[i];
	}
	cout<<max(dpa[n],max(dpb[n],dpc[n]))<<endl;
	return 0;
}


Measured memory_bytes: 2560
// Author: Tyler Wang

#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cctype>
#include <chrono>
#include <cmath>
#include <complex>
#include <cstdint>
#include <cstdlib>
#include <deque>
#include <functional>
#include <initializer_list>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <limits>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <type_traits>
#include <utility>
#include <vector>

using namespace std;

int main() {
  std::ios::sync_with_stdio(false);
  std::cin.tie(nullptr);
  std::cout.precision(12);
  std::cerr.precision(12);
  std::mt19937 rng((std::uint_fast32_t)std::chrono::high_resolution_clock::now()
                       .time_since_epoch()
                       .count());
  constexpr int W_MAX = (int)2e4 + 4;
  int n;
  cin >> n;
  vector<tuple<int, int, int>> wsv(n);
  for (int i = 0; i < n; ++i) {
    int &w = get<0>(wsv[i]), &s = get<1>(wsv[i]), &v = get<2>(wsv[i]);
    cin >> w >> s >> v;
  }
  // w + w1 <= s2
  // s1 < w + w2
  // w1 + s1 < w2 + s2
  sort(wsv.begin(), wsv.end(),
       [](const tuple<int, int, int>& t1, const tuple<int, int, int>& t2) {
         return get<0>(t1) + get<1>(t1) < get<0>(t2) + get<1>(t2);
       });
  vector<long long> dp(W_MAX);
  for (int i = 0; i < n; ++i) {
    int &w = get<0>(wsv[i]), &s = get<1>(wsv[i]), &v = get<2>(wsv[i]);
    for (int j = s; j >= 0; --j) {
      dp[j + w] = max(dp[j + w], dp[j] + v);
    }
  }
  cout << *max_element(dp.begin(), dp.end()) << '\n';
  return 0;
}

Measured memory_bytes: 384
#include<bits/stdc++.h>
using namespace std;
int main()
{
	int n,flag=0,count=0;
  cin>>n;
  string s;
  cin>>s;
  
  for(int i=0;i<n;i++)
  {
   if(s[i]=='A')
        flag=1;
    else if(s[i]=='B' && flag==1)
      flag=2;
     else if(s[i]=='C' && flag==2)
     {flag=0; count++;}
    else
      flag=0;
  }
  cout<<count<<endl;
 
  return 0;
}

Measured memory_bytes: 3592
s=input()
N=len(s)

a=[0]*(N+1)
for i in range(N):
    if (s[i]=='<'):
        a[i+1]=a[i]+1

for i in range(N-1,-1,-1):
    if (s[i]=='>'):
        a[i]=max(a[i],a[i+1]+1)
print(sum(a))

Measured memory_bytes: 23336
import collections
n = int(input())
a = list(map(int,input().split()))
aa  = collections.Counter(a)
aakey = list(aa.keys())     
aavalue = list(aa.values())     
ans = 0
for i in range(len(aavalue)):
         ans += (aavalue[i] * (aavalue[i] - 1))//2
for i in range(n):
         val = aa[a[i]]
         print(ans-(val-1))


Measured memory_bytes: 26900
#include <bits/stdc++.h>
#include <cxxabi.h>
using namespace std;
using ll=long long;
using vi=vector<int>;
using vvi=vector<vi>;
using pii=pair<int,int>;
#define rep(i,n) range(i,0,n)
#define range(i,a,n) for(int i=a;i<n;i++)
#define all(a) a.begin(),a.end()
#define LINF    ((ll)1ll<60)
#define INF     ((int)1<<30)
#define EPS     (1e-9)
#define MOD     (1000000007)
template<class S,class T>ostream& operator<<(ostream&os,pair<S,T>p){os<<"["<<p.first<<", "<<p.second<<"]";return os;};
template<class S>auto&operator<<(ostream&os,vector<S>t){bool b=1;for(auto s:t)os<<(exchange(b,0)||strlen(abi::__cxa_demangle(typeid(S).name(),0,0,0))>20?"":" ")<<s;return os<<endl;}

int main(){
	cin.tie(0);
   	ios::sync_with_stdio(false);
	
	int n;
	cin>>n;
	string s;
	cin>>s;
	int ans=0;
	range(i,1,n){
	    vi a(26,0);
	    for(auto c:s.substr(0,i))a[c-'a']|=1;
	    for(auto c:s.substr(i))a[c-'a']|=2;
	    ans=max(ans,(int)count(all(a),3));
	}
	cout<<ans<<endl;
	return 0;
}


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

int main(){
   string s;cin >> s;
   string dream   = "dream";
   string dreamer = "dreamer";
   string erase   = "erase";
   string eraser  = "eraser";


   reverse(s.begin(), s.end());
   reverse(dream.begin(), dream.end());
   reverse(dreamer.begin(), dreamer.end());
   reverse(erase.begin(), erase.end());
   reverse(eraser.begin(), eraser.end());

   string can = "YES";

   // for(int i = 0; i < s.end()+1; i++) {
   //    if()
   // }
   while (s.size() != 0) {
      string tmp = s;
      if (s.substr(0, 5) == dream || s.substr(0, 5) == erase) {
         s = s.substr(5);
      } else if (s.substr(0, 6) == eraser) {
         s = s.substr(6);
      } else if (s.substr(0, 7) == dreamer) {
         s = s.substr(7);
      }

      // ちぎれなかった場合
      if (tmp == s) {
         can = "NO";
         break;
      }

      // if (s.size() <= 7 && s.size() != 0 &&
      //    (s != dream && s != dreamer && s != erase && s != eraser)) {
      //       can = "NO";
      //       break;
      // }
   }

   cout << can << endl;
   return 0;
}


Measured memory_bytes: 3764
#include <bits/stdc++.h>
using namespace std;
#define reps(i, a, n) for (int i = (a); i < (n); ++i)
#define rep(i, n) reps(i, 0, n)
#define deps(i, a, n) for (int i = (a); i >= (n); --i)
#define dep(i, n) deps(i, n, 0)
#define inf 2147483647
#define int long long

int c[100005];
vector<int> ab[100005];
queue<int> qu;

signed main(void)
{
	int n, m, a, b; cin >> n >> m;
	reps (i, 1, m + 1)
	{
		cin >> a >> b;
		ab[a].push_back(b);
		ab[b].push_back(a);
	}
	qu.push(1);
	c[1] = 1;
	while (!qu.empty())
	{
		a = qu.front(); qu.pop();
		for(int nx: ab[a]) if(!c[nx]) qu.push(nx), c[nx] = a;
	}
	reps (i, 1, n + 1) if (!c[i]) {cout << "No" << endl; return 0;}
	cout << "Yes" << endl;
	reps (i, 2, n + 1) cout << c[i] << endl;
}


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

int classify(int x){
    if(x==1 || x==3 || x==5 || x==7 || x==8 || x==10 || x==12)return 1;
    else if(x==4 || x==6 || x==9 || x==11)return 2;
    else return 3;
}
int main(void){
    int x,y;
    int flag=0;
    cin >> x >> y;
    string ans = classify(x)==classify(y)?"Yes":"No";
    cout << ans << endl;
    return 0;
}

Measured memory_bytes: 256
#include <bits/stdc++.h>
#define fi first
#define se second
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rrep(i, n) for (int i = 1; i <= (n); ++i)
#define drep(i, n) for (int i = (n)-1; i >= 0; --i)
#define srep(i, s, t) for (int i = s; i < t; ++i)
#define rng(a) a.begin(), a.end()
#define rrng(a) a.rbegin(), a.rend()
#define maxs(x, y) (x = max(x, y))
#define mins(x, y) (x = min(x, y))
#define limit(x, l, r) max(l, min(x, r))
#define lims(x, l, r) (x = max(l, min(x, r)))
#define isin(x, l, r) ((l) <= (x) && (x) < (r))
#define pb push_back
#define eb emplace_back
#define sz(x) (int)(x).size()
#define pcnt __builtin_popcountll
#define uni(x) x.erase(unique(rng(x)), x.end())
#define show(x) cout << #x << " = " << x << endl;
#define PQ(T) priority_queue<T, v(T), greater<T>>
#define bn(x) ((1 << x) - 1)
#define dup(x, y) (((x) + (y)-1) / (y))
#define newline puts("")
#define v(T) vector<T>
#define vv(T) v(v(T))
using namespace std;
typedef long long int ll;
typedef unsigned uint;
typedef unsigned long long ull;
typedef pair<int, int> P;
typedef tuple<int, int, int> T;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<P> vp;
typedef vector<T> vt;

int main() { 
  int n,m;
  cin >> n >> m;
  vl a(n);
  rep(i,n)cin>>a[i];
  ll ans = 0;
  map<ll,ll> mp;
  ll sum = 0;
  mp[sum]++;
  rep(i,n){
    sum+=a[i];
    mp[sum%m]++;
  }
  for(auto it : mp){
    ans+=(it.se*(it.se-1))/2;
  }
  cout<<ans<<endl;
  return 0;
}

Measured memory_bytes: 4992
#include <bits/stdc++.h>
using namespace std;
int main(){
	int n;
	scanf("%d",&n);
	int a[100005],b[100005];
	for(int i=0;i<n+1;i++){
		scanf("%d",&a[i]);
	}
	long long s=0;
	for(int i=0;i<n;i++)
		scanf("%d",&b[i]);
	for(int i=0;i<n;i++){
		if(b[i]>=a[i]){
			s+=a[i];
			b[i]=b[i]-a[i];
			a[i]=0;
			if(b[i]<=a[i+1]){
				s+=b[i];
				a[i+1]-=b[i];
				b[i]=0;
			}
			else{
				s+=a[i+1];
				a[i+1]=0;
			}
		}
		else{
			s+=b[i];
		}
	}
	printf("%lld\n",s);				
	return 0;
}

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

typedef long long ll;
typedef vector<int> vi;

#define Forn(i, n) for(int i=1; i<=(n); i++)
#define forn(i, n) for(int i=0; i<(n); i++)
#define lid id<<1|1
#define rid (id<<1)+2
#define foreach(it, a) for(__typeof((a).begin()) it=(a).begin(); it!=(a).end(); it++)
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(), (x).end()
#define mid l+r>>1
#define lson lid, l, mid
#define rson rid, mid, r 
#define f first
#define s second
#define int long long

const int MAX=1e5+5;

int N, M, ptr, ret, a[MAX];

struct Seg
{
    int tag1[MAX*4], tag2[MAX*4];
    Seg()
    {
        memset(tag1, 0, sizeof(tag1));
        memset(tag2, 0, sizeof(tag2));
    }
    inline void update(int id, int l, int r, int a, int b, int k1, int b1)
    {
       if(r<=a||b<=l) return;
       if(a<=l&&r<=b)
       {
           tag1[id]+=k1;
           tag2[id]+=b1;
           return;
       }
       update(lson, a, b, k1, b1);
       update(rson, a, b, k1, b1);
    }
    inline void update(int l, int r, int k1, int b1)
    {
        if(l>=r) return;
        update(0, 0, ptr, l, r, k1, b1);
    }
    inline void print(int id, int l, int r, int &ret)
    {
        if(l+1==r)
        {
            if(l) ret=min(ret, tag1[id]*l+tag2[id]);
            return;
        }
        
        tag1[lid]+=tag1[id];
        tag2[lid]+=tag2[id];
        
        tag1[rid]+=tag1[id];
        tag2[rid]+=tag2[id];
        
        print(lson, ret);
        print(rson, ret);
    }
}S;

signed main()
{
    scanf("%lld %lld", &N, &M);
    forn(i, N)
        scanf("%lld", &a[i]);
    ptr=M+1;
    
    forn(i, N-1)
    {
        if(a[i]==a[i+1]) continue;
        if(a[i]<a[i+1])
        {
            S.update(1, a[i]+1, 0, a[i+1]-a[i]);
            S.update(a[i]+1, a[i+1]+1, -1, a[i+1]+1);
            S.update(a[i+1]+1, ptr, 0, a[i+1]-a[i]);
        }
        else
        {
            S.update(1, a[i+1]+1, -1, a[i+1]+1);
            S.update(a[i+1]+1, a[i]+1, 0, M-a[i]+a[i+1]);
            S.update(a[i]+1, ptr, -1, M+a[i+1]+1);
        }
    }
    ret=2e18;
    S.print(0, 0, ptr, ret);
    printf("%lld\n", ret);
    return 0;
}

Measured memory_bytes: 7296
C1=str(input())
C2=str(input())
C11=C1[0]
C12=C1[1]
C13=C1[2]
C21=C2[0]
C22=C2[1]
C23=C2[2]
if C11==C23 and C12==C22 and C13==C21:
    print('YES')
else:
    print('NO')

Measured memory_bytes: 3060
import sys
readline = sys.stdin.readline

class UnionFind:
  N=0
  parent=None
  size=None
  def __init__(self,N):
    self.N=N
    self.parent=[i for i in range(self.N)]
    self.size=[1]*self.N
    
  def root(self,x):
    while x!=self.parent[x]:
      self.parent[x]=self.parent[self.parent[x]]
      x=self.parent[x]
    return x
  
  def same(self,x,y):
    return self.root(x)==self.root(y)
  
  def unite(self,x,y):
    x=self.root(x)
    y=self.root(y)
    if x==y:
      return
    if self.size[x]>self.size[y]:
      # 大きい方にくっつける
      self.parent[y]=x
      self.size[x]+=self.size[y]
    else:
      self.parent[x]=y
      self.size[y]+=self.size[x]
      
  def get_group_size(self,x):
    return self.size[self.root(x)]
  
  def get_roots(self):
    r=set()
    for i in range(self.N):
      r.add(self.root(i))
    return r
  
  def show_parent(self):
    print(self.parent)
    
  def show_size(self):
    print(self.size)
    
N,M = map(int,readline().split())
bridges = [tuple(map(int,readline().split())) for i in range(M)]

# 順に崩落していく = 最後に崩落するものから順に建設されていくと考える
# 最初の不便さは(N * (N - 1)) // 2
# 新たにa,bの間に橋が繋がると、[aが属していた島々 * bが属していた島々]だけ不便さが減る
UF = UnionFind(N)
ans = [0] * M
num = (N * (N - 1)) // 2

for i in range(len(bridges) - 1, -1, -1):
  ans[i] = num
  a,b = bridges[i]
  a,b = a - 1,b - 1
  if not UF.same(a,b):
    num -= UF.get_group_size(a) * UF.get_group_size(b)
  UF.unite(a,b)

print(*ans, sep = "\n")

Measured memory_bytes: 32412
#include <iostream>
#include <cstring>
#include <stdlib.h>
#include <vector>
#include <algorithm>
#include <cmath>
#include <stack>
#include <queue>
#include <math.h>
#include <string>
#include <map>
#include <cstdlib>
using namespace std;

typedef long long ll;

int main()
{
    int n;
    cin >> n;
    vector<int> A(n);
    for(int i=0; i<n; i++){
        cin >> A[i];
    }
    sort(A.begin(), A.end(), greater<int> ());
    vector<int> flag;
    int cnt=0;
    for(int i=0; i<n-1; i++){
        if(A[i]==A[i+1]){
            cnt++;
            if(cnt==1 || cnt==3) flag.push_back(i);
        }else{
            cnt=0;
        }
    }
    
    if(flag.size()<=1) cout << 0;
    else cout << ll(A[flag[0]])*A[flag[1]];
    
    return 0;
}

Measured memory_bytes: 1024
#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
using namespace std;
int main() {
	string s;
	cin >> s;
	if (s[4] == s[5] && s[2] == s[3]) {
		cout << "Yes";
	}
	else {
		cout << "No";
	}
}


Measured memory_bytes: 256
#include <cstdio>
#include <iostream>
#include <vector>
#include <algorithm>
#include <bitset>
#include <cstring>
#include <set>
#include <map>
#include <unordered_map>

using namespace std;

vector<pair<int, int>> *arr;
long long dp[100002][100];
long long calc(int i, int w) {
    if (i == arr->size())
        return 0;
    long long &res = dp[w][i];
    if (res != -1)
        return res;
    res = 0;
    if ((*arr)[i].first <= w) {
        res = max(res, ((*arr)[i]).second + calc(i + 1, w - ((*arr)[i]).first));
    }
    res = max(res, calc(i + 1, w));

    return res;
}

int main() {
    ios_base::sync_with_stdio(0);
#ifdef IDE
    freopen("C:\\file.in", "r", stdin);
//    freopen("C:\\file.out", "w", stdout);
#endif

    int n, w;
    cin >> n >> w;
    vector<pair<int, int>> item(n);
    for (auto &r : item) {
        cin >> r.first >> r.second;
    }
    arr = &item;
    memset(dp, -1, sizeof dp);
    printf("%lld\n", calc(0, w));

    return true & false;
}



Measured memory_bytes: 78336
n, m = map(int, input().split())
ps = [list(input().split()) for _ in range(m)]
ac = [0]*n
wa = [0]*n
cnt = [0]*n
for i in range(m):
    x = int(ps[i][0]) - 1
    if ps[i][1] == "AC" and ac[x] == 0:
        ac[x] = 1
        wa[x] = cnt[x]
    elif ps[i][1] == "WA" and ac[x] == 0:
        cnt[x] += 1
print(sum(ac), sum(wa))

Measured memory_bytes: 32576
#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 = 2019;
struct mint{
    ll x;
    mint (ll x=0) : x((x%mod+mod)%mod){}//カッコの中で初期化している
    mint& operator+=(const mint a){//operatorは構造体同士の比較に用いられる
        if((x +=a.x)>=mod) x-=mod;
        return *this;//thisとは現在のオブジェクトのこと
    }
    mint& operator-=(const mint a){
        if((x +=mod-a.x)>=mod) x-=mod;
        return *this;
    }
    mint& operator*=(const mint a){
        (x*=a.x)%=mod;
        return *this;
    }
    mint operator+(const mint a){
        mint res(*this);
        return res+=a;
    }
    mint operator-(const mint a){
        mint res(*this);
        return res-=a;
    }
    mint operator*(const mint a){
        mint res(*this);
        return res*=a;
    }
    mint pow(ll t) const{
        if(!t) return 1;
        mint a=pow(t>>1);
        a*=a;
        if(t&1) a*=*this;
        return a;
    }
    mint inv() const{
        return pow(mod-2);//逆元
    }
    mint&operator/=(const mint a) {
        return (*this)*=a.inv();
    }
    mint&operator/(const mint a) const{
    mint res(*this);
    return res/=a;
    }
};
 
int main(void){
    string s;
    cin>>s;
    reverse(s.begin(), s.end());
    vector<ll> v(2019);
    mint now = 0;
    mint beki = 1;
    v[0]++;
    rep(i, s.size()){
        mint tmp = s[i]-'0';
        now += tmp*beki;
        v[now.x]++;
        beki *= 10;
    }
    ll ans = 0;
    rep(i, v.size()){
        ans += v[i]*(v[i]-1)/2;
    }
    cout<<ans<<endl;
    return 0;
}

Measured memory_bytes: 3752
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
using namespace std;
using ll = int64_t;
using P = pair<ll, ll>;
template <class T>
using V = vector<T>;

int head(int n) {
  string s = to_string(n);
  return s[0] - '0';
}
int tail(int n) {
  string s = to_string(n);
  return s[s.length() - 1] - '0';
}

int main() {
  string s;
  ll k;
  cin >> s;
  cin >> k;

  int l = s.length();
  rep(i, l) {
    if (k == 0) break;
    if (s[i] == 'a') continue;
    int r = 'z' - s[i] + 1;
    if (k >= r) {
      k -= r;
      s[i] = 'a';
    }
  }
  k %= 26;
  rep(i, k) {
    s[l - 1]++;
    if (s[l - 1] > 'z') s[l - 1] = 'a';
  }
  cout << s << endl;
}


Measured memory_bytes: 3720
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
#define pb push_back
#define mp make_pair
#define rep(i,n) for(int i=0;i<(n);++i)

int main(){
	cin.tie(0);
	ios::sync_with_stdio(false);
	vector<int> a(3);
	rep(i,3) cin >> a.at(i);
	sort(a.begin(),a.end());
	int ans=0;
	if(a.at(2)%2==a.at(0)%2){
		if(a.at(2)%2!=a.at(1)%2){
			ans++;
			a.at(0)++;
			a.at(2)++;
		}
	}
	else{
		if(a.at(0)%2==a.at(1)%2){
			ans++;
			a.at(0)++;
			a.at(1)++;
		}
		else{
			ans++;
			a.at(1)++;
			a.at(2)++;
		}
	}
	ans+=(a.at(2)-a.at(0))/2+(a.at(2)-a.at(1))/2;
	cout << ans << endl;
}

Measured memory_bytes: 256
#include <iostream>

using namespace std;

int abs(int x) {
    return x >= 0 ? x : -x;
}

int main() {
    int a, b, c, dist;

    cin >> a >> b >> c >> dist;

    if (abs(a - c) <= dist or abs(a - b) <= dist and abs(b - c) <= dist)
        cout << "Yes" << endl;
    else
        cout << "No" << endl;

    return 0;
}


Measured memory_bytes: 256
#include <bits/stdc++.h>
# define ll int64_t
#define rep(i,n) for(ll i=0;i<n;i++)
#define rrep(i,n) for(ll i=1;i<=n;i++)
# define ALL(x) (x).begin(), (x).end()
# define SZ(x) ((int)(x).size())
# define pb push_back
# define mod 1000000007

using namespace std;

bool compare_by_b(pair<string,ll> a,pair<string,ll> b){
    if(a.second != b.second) return a.second<b.second;
    else return a.first<b.first;
}

bool my_compare(pair<string,ll> a,pair<string,ll> b){
    
    if(a.first != b.first) return a.first<b.first;
    if(a.second != b.second) return a.second>b.second;
    else return true;
}

int main(){
    ll x;
    cin>>x;
    bool prime=true;
    while(prime){
        for(ll i=2;i<x;i++){
            if(x%i==0) {
                x++;
                i=2;
                continue;
            }
        }
        prime=false;
    }
    cout<<x<<endl;
    return 0;
}


Measured memory_bytes: 256
n=int(input())
t,a= map(int,input().split())
h = list(map(int,input().split()))
m=abs(t-h[0]*0.006-a)
ans=0
for i in range(n):
    if m >= abs(t-h[i]*0.006-a):
        m=abs(t-h[i]*0.006-a)
        ans=i+1
print(ans)



Measured memory_bytes: 3060
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ld long double
#define Phuong // <3
typedef pair<int,int> ii;

const int inf = 1e9+1;
int n,m;

vector<int> d,deg;
vector<vector<int>> g;

void DFS(int x)
{
    stack<int> st;
    st.push(x);
    while(!st.empty())
    {
        int u=st.top();
        st.pop();
        for(auto x:g[u])
        if (deg[x]==1)
        {
            d[x]=max(d[x],d[u]+1);
            st.push(x);
        } else
        {
            deg[x]--;
            d[x]=max(d[x],d[u]+1);
        }
    }
}

int main()
{
#ifndef ONLINE_JUDGE
    //freopen("input.txt","r",stdin);
#endif // ONLINE_JUDGE
    ios_base::sync_with_stdio(0);
    cin.tie(NULL);
    cin >> n >> m;
    g= vector<vector<int>> (n);
    deg = vector<int> (n,0);
    d = vector<int> (n,0);
    for(int i=0; i<m; ++i)
    {
        int u,v;
        cin >> u >> v;
        u--;
        v--;
        g[u].push_back(v);
        deg[v]++;
    }
    for(int i=0;i<n; ++i)
    if (deg[i]==0)
    {
        DFS(i);
    }
    int ans=0;
    for(auto x:d)
        ans=max(ans,x);
    cout << ans;
    return(0);
}


Measured memory_bytes: 6528
#include <bits/stdc++.h>
using namespace std;
#define Rep(i,n) for(int i=0;i<(int)(n);i++)
#define For(i,n1,n2) for(int i=(int)(n1);i<(int)(n2);i++)
#define REP(i,n) for(ll i=0;i<(ll)(n);i++)
#define RREP(i,n) for(ll i=((ll)(n)-1);i>=0;i--)
#define FOR(i,n1,n2) for(ll i=(ll)(n1);i<(ll)(n2);i++)
#define RFOR(i,n1,n2) for(ll i=((ll)(n1)-1);i>=(ll)(n2);i--)
#define all(a)  (a).begin(),(a).end()
#define SORT(a) sort((a).begin(),(a).end())
#define oorret 0
#define oor(x) [&](){try{x;} catch(const out_of_range& oor){return oorret;} return x;}()
#define IOS cin.tie(0),ios::sync_with_stdio(false)
typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll> P;
template<typename T1, typename T2> inline bool chmin(T1& a, T2 b) { if (a > b) { a = b; return 1; }return 0; }
template<typename T1, typename T2> inline bool chmax(T1& a, T2 b) { if (a < b) { a = b; return 1; }return 0; }
template<class Type>struct is_vector : std::false_type {};
template<class ValueType, class Alloc>struct is_vector<std::vector<ValueType, Alloc>> : std::true_type {};
template <typename T> inline ostream& operator << (ostream& out, const vector<T>& v) {
	if (v.empty())return out;
	constexpr bool is_vector_v = is_vector<T>::value;
	if (is_vector_v)for (auto itr = v.begin(); itr != v.end();)out << (*itr), out << ((++itr != v.end()) ? "\n" : "");
	else for (auto itr = v.begin(); itr != v.end();)out << (*itr), out << ((++itr != v.end()) ? " " : "");
	return out;
}
inline void put() {}
template<class T> inline void put(const T& first) { std::cout << first << "\n"; }
template<class T, class... N> inline void put(const T& first, const N& ... rest) { std::cout << first << " "; put(rest...); }
inline void putn() {}
template<class T, class... N> inline void putn(const T& first, const N& ... rest) { std::cout << first << "\n"; putn(rest...); }


int main() {
	ll n;
	cin >> n;
	put(n * (n - 1) / 2);
	return 0;
}

Measured memory_bytes: 256
/* while(!success_achieved())
      work_hard(); 

   code by:aman_ayush
  */
#include <bits/stdc++.h>
using namespace std;

#define ll long long
#define ldb long double 
#define db double
#define str string

#define fr first
#define se second
#define lb lower_bound 
#define ub upper_bound 
#define pb push_back
#define mp make_pair

#define pi pair<int,int>
#define pll pair<ll,ll>
#define pdb pair<db,db>
#define pldb pair<ldb,ldb>

#define vi vector<int>
#define vll vector<ll>
#define vdb vector<db>
#define vldb vector<ldb>
#define vpi vector< pi >
#define vpll vector< pll >

#define mi map<int,int>
#define mll map<ll,ll>
#define mdb map<db,db>
#define mldb map<ldb,ldb>


#define forr(i,a,b) for (int i = (a); i < (b); ++i)
#define rrof(i,a,b) for (int i = (b)-1; i >= (a); --i)

#define sz(x) (int)x.size()
#define all(x) begin(x), end(x)

const int MOD = 1e9+7;
const int MX = 2e5+5;
const ll inf =0x3f3f3f3f;
const ll linf=0x3f3f3f3f3f3f3f3f;
const ldb PI = 4*atan((ldb)1);

ll power(ll a,ll n)
{
 ll res=1;
 while(n)
{
 if(n%2)
  res*=a;
  a*=a;
  n/=2;
}
return res;
}

ll abs(ll a)
{
 return ((a<0)?(-1*a):(a));
}

int main()
{
    int n;
    cin>>n;
    cout<<(n*n*n);
    
    return 0;
}



Measured memory_bytes: 256
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
const ll MOD = 1e9+7;
const ll INF = 1e18;
#define rep(i,m,n) for(ll i = (m); i <= (n); i++)
#define zep(i,m,n) for(ll i = (m); i < (n); i++)
#define rrep(i,m,n) for(ll i = (m); i >= (n); i--)
#define print(x) cout << (x) << endl;
#define printa(x,m,n) for(int i = (m); i <= n; i++){cout << (x[i]) << " ";} cout<<endl;

int main(){
    cin.tie(0); ios::sync_with_stdio(false);
    
    ll n; cin >> n;
    ll l[n]; zep(i, 0, n)cin >> l[i];
    sort(l, l + n);
    
    ll ans = 0;
    zep(i, 0, n){
    	zep(j, 0, i){
    		zep(k, 0, j){
    			if(l[i] != l[j] && l[j] != l[k] && l[i] < l[j] + l[k])ans++;	
    		}
    	}
    }
    print(ans)
    
    return 0;
}

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

using namespace std;
typedef long long ll;

int a[200001];

int main(void) {
    int n;
    scanf("%d", &n);
    for(int i=0; i<n; i++) scanf("%d", a+i);
    if(a[0] != 0) return !printf("-1\n");

    ll ans = 0;
    for(int i=1; i<n; i++) {
        if(a[i] == a[i-1] + 1) ans++;
        else if(a[i] > i || a[i] - a[i-1] >= 2) return !printf("-1\n");
        else ans += a[i];
    }

    return !printf("%lld\n", ans);
}

Measured memory_bytes: 1024
import java.util.*;
    public class Main {
     
    	public static void main(String[] args) {
    		Scanner scan =new Scanner(System.in);
    		String s=scan.next();
    		
    		int len=s.length();
    		
    		int ans=3;
    		for(int i=0;i<len;++i) {
    			if(s.charAt(i)!=s.charAt(len-i-1)) {
    				--ans;

    				break;
    			}
    		}
    		
    		for(int i=0;i<(len-1)/2;++i) {
    			if(s.charAt(i)!=s.charAt((len-1)/2-i-1)) {
    				--ans;

    				break;
    			}
    		}
    		int j=0;
    		for(int i=(len+3)/2-1;i<len;++i) {
    			if(s.charAt(i)!=s.charAt(len-j-1)) {
    				--ans;

    				break;
    			}
    			++j;
    		}
    		
    		if(ans==3) {
    			System.out.print("Yes");
    		}else {
    			System.out.print("No");
    		}
    		
    }
    }
// end 

Measured memory_bytes: 23124
import sys

sys.setrecursionlimit(10 ** 9)


class UnionFind:
    def __init__(self, n):
        self.v = [-1 for _ in range(n)]  # 根(負): 連結頂点数 * (-1) / 子(正): 根の頂点番号(0-indexed)

    def find(self, x):  # xを含む木における根の頂点番号を返す
        if self.v[x] < 0:  # (負)は根
            return x
        else:  # 根の頂点番号
            self.v[x] = self.find(self.v[x])  # uniteでは, 旧根に属する頂点の根が旧根のままなので更新
            return self.v[x]

    def unite(self, x, y):  # 違う根に属していたらrankが低くなるように連結
        x = self.find(x)
        y = self.find(y)
        if x == y:
            return
        if -self.v[x] < -self.v[y]:  # size比較, 　(-1) * (連結頂点数 * (-1)), (正)同士の大小比較
            x, y = y, x  # 連結頂点数が少ない方をyにすると, findでの更新回数が減る？
        self.v[x] += self.v[y]  # 連結頂点数の和を取る, 連結頂点数 * (-1)
        self.v[y] = x  # 連結頂点数が少ないy(引数yの根の頂点番号)の根をx(引数xの根の頂点番号)にする

    def root(self, x):
        return self.v[x] < 0  # (負)は根

    def same(self, x, y):
        return self.find(x) == self.find(y)  # 同じ根に属するか

    def size(self, x):
        return -self.v[self.find(x)]  # 連結頂点数を返す


N = int(input())
e = [[] for _ in range(N)]
uf = UnionFind(N)
for _ in range(N - 1):
    u, v, w = map(int, input().split())
    u -= 1
    v -= 1
    # 0-indexedに直した
    e[u].append((v, w))
    e[v].append((u, w))

    uf.same(u, v)

# e[点]:=(隣接頂点,長さ)

root = uf.find(0)
# 根

ans = [None] * N
ans[root] = 0

# print(e)


def rec(vv):
    for nv, w in e[vv]:
        if ans[nv] is not None: continue
        ans[nv] = (ans[vv] + w) % 2
        rec(nv)


rec(root)

print(*ans, sep='\n')


Measured memory_bytes: 160928
import sys

def I(): return int(sys.stdin.readline().rstrip())
def LI(): return list(map(int,sys.stdin.readline().rstrip().split()))  #空白あり

N = I()
xy = [LI() for i in range(N)]

# 円の中心の半径は、ある2点の中点か、ある3点の外心かである

ans = []

def distance(A,B):
    x1,y1 = A
    x2,y2 = B
    return ((x1-x2)**2+(y1-y2)**2)**.5

for i in range(N-1):
    for j in range(i+1,N):
        x1,y1 = xy[i]
        x2,y2 = xy[j]
        x,y = (x1+x2)/2,(y1+y2)/2
        r = distance((x,y),(x1,y1))
        for k in range(N):
            if k == i or k == j:
                continue
            else:
                if distance(xy[k],(x,y)) > r:
                    break
        else:
            ans.append(r)


def gaishin(A,B,C):
    x1,y1 = A
    x2,y2 = B
    x3,y3 = C
    a = 2*((x2-x1)*(y3-y1)-(x3-x1)*(y2-y1))
    b = x2**2-x1**2+y2**2-y1**2
    c = x3**2-x1**2+y3**2-y1**2
    if a == 0:
        return (10000,10000)
    else:
        x = ((y3-y1)*b-(y2-y1)*c)/a
        y = (-(x3-x1)*b+(x2-x1)*c)/a
    return (x,y)

for i in range(N-2):
    for j in range(i+1,N-1):
        for k in range(j+1,N):
            x,y = gaishin(xy[i],xy[j],xy[k])
            if x == 10000 and y == 10000:
                continue
            else:
                r = distance((x,y),xy[i])
                for l in range(N):
                    if l == i or l == j or l == k:
                        continue
                    else:
                        if distance(xy[l],(x,y)) > r:
                            break
                else:
                    ans.append(r)

print(min(ans))

Measured memory_bytes: 76472
#include <bits/stdc++.h>
#define REP(i,n) for(int i=0;i<(n);++i)
#define ALL(v)   (v).begin(),(v).end()
#define debug(x) cerr<<#x<<": "<<(x)<<endl
#define INF      (int)1e9
#define EPS      (double)1e-9
#define MOD      ((int)1e9+7)
using namespace std;
typedef long long     llong;
typedef vector<int>   vi;
typedef vector<vi >   vvi;
typedef vector<vvi >  vvvi;
typedef pair<int,int> pii;
template<class Type> void line(const Type &a){int cnt=0;for(const auto &elem:a){if(cnt++)cout<<' ';cout<<elem;}cout<<endl;}

int main(){
	int n;
	cin>>n;
	
	map<int,int> mp;
	llong sum=0;
	REP(i,n){
		int a;
		cin>>a;
		mp[a]++;
		sum+=a;
	}
	
	int q;
	cin>>q;
	REP(i,q){
		llong b,c;
		cin>>b>>c;
		
		sum-=b*mp[b];
		sum+=c*mp[b];
		mp[c]+=mp[b];
		mp[b]=0;
		
		cout<<sum<<endl;
	}
}

Measured memory_bytes: 8012
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
using namespace std;

#define F first
#define S second
#define pb push_back
#define eb emplace_back
#define PI 3.14159265359
#define sz(c) (int)c.size()
#define len(c) (int)c.length()
#define hcf(a, b) __gcd(a, b)
#define all(c) c.begin(), c.end()
#define vint vector<int>
#define vll vector<ll>
#define vpint vector<pair<int, int>>
#define vpll vector<pair<ll, ll>>
#define fr(it, a, b) \
    for (int it = (a); it < (b); it++)
#define tr(con, it) \
    for (auto it = con.begin(); it != con.end(); it++)
#define present(con, ele) (con.find(ele) != con.end())
#define cpresent(con, ele) (find(all(con), ele) != con.end())
#define maximum(a, b, c) (a > b ? (a > c ? a : c) : (b > c ? b : c))
#define minimum(a, b, c) (a < b ? (a < c ? a : c) : (b < c ? b : c))
typedef unsigned long long lu;
typedef pair<int, int> pi;
typedef long long ll;
typedef long double lf;
const ll mod = 1e9 + 7;
int32_t main()
{
    // freopen("input.txt", "r", stdin);
    // freopen("output.txt", "w", stdout);
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int x, ans = 0;
    cin >> x;
    if (x >= 1800)
    {
        ans = 1;
    }
    else if (x >= 1600)
    {
        ans = 2;
    }
    else if (x >= 1400)
    {
        ans = 3;
    }
    else if (x >= 1200)
    {
        ans = 4;
    }
    else if (x >= 1000)
    {
        ans = 5;
    }
    else if (x >= 800)
    {
        ans = 6;
    }
    else if (x >= 600)
    {
        ans = 7;
    }
    else if (x >= 400)
    {
        ans = 8;
    }
    cout << ans << "\n";
    return 0;
}

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

int main(void){
    int n, tmp, cnt, max_cnt = 0, max_num = 1;
    cin >> n;
    for (int i=1; i<=n; i++){
        tmp = i;
        cnt = 0;
        while (tmp % 2 == 0){
            cnt++;
            tmp /= 2;
        }
        if (max_cnt < cnt){
            max_cnt = cnt;
            max_num = i;
        }
    }
    cout << max_num << endl;
    return 0;
}

Measured memory_bytes: 3492
/*
 *  Created on: 31 / 5 / 2020
 *  Author:     Eslam Waheed
 */
#include <bits/stdc++.h>
//#include <bits/BIGINT.h>
//#include <bits/Int128.h>
using namespace std;

//#define _CRT_SECURE_NO_WARNINGS
//#define __USE_MINGW_ANSI_STDIO
#define Eslam__Waheed ios_base::sync_with_stdio(false);cout.tie(NULL);cin.tie(NULL);

#define ll   long long
#define ull  unsigned ll
#define dd   double
#define ld   long dd
#define ss   string

#define pb      push_back
#define all(c)  c.begin(), c.end()
#define rall(c) c.rbegin(), c.rend()
#define stp(n)  cout << fixed << setprecision(n);
#define iq(v)   v.resize(unique(v.begin(),v.end())-v.begin())
#define sz(s)   (ll)(s.size())
#define endl    '\n'

#define loop(i,m,n)  for(ll i=m;i<n;i++)
#define test(t)      while(t--)

//ll gcd(ll a, ll b) { return !b ? abs(a) : gcd(b, a % b); }
//ll lcm(ll a, ll b) { return abs(a / gcd(a, b)) * b; }

//const ld PI = acos(-1);
//const ll mod = 1e15 + 7;
//__int128
/*
 you can ^_^ (•‿•)

( إن الله وملائكته يصلون على النبي يا أيها الذين آمنوا صلوا عليه وسلموا تسليما )
*/
ss Big_mul(string a, string b)
{
    reverse(all(a));
    reverse(all(b));
    vector<ll>v(600, 0);
    loop(i, 0, sz(a))
    {
        loop(u, 0, sz(b))
        {
            v[i + u] += ((a[i] - '0') * (b[u] - '0'));
        }
    }
    for (int i = 0; i < 599; i++)
    {
        v[i + 1] += v[i] / 10;
        v[i] %= 10;
    }
    int i = 599;
    while (i > 0 && v[i] == 0)
    {
        i--;
    }
    ss ans = "";
    for (; i >= 0; i--)
    {
        ans += to_string(v[i]);
    }
    return ans;
}
int main()
{
    //stp(0)
    Eslam__Waheed
    ll n; cin >> n;
    vector<ss>v(n);
    for (auto& it : v)
    {
        cin >> it;
    }
    sort(all(v));
    ss ans = "1";
    for (auto it : v)
    {
        ans = Big_mul(ans, it);
        if (sz(ans) > 19)
        {
            cout << -1;
            return 0;
        }
    }
    //cout << ans;
   
    ll x = stoll(ans);
    if (x > 1000000000000000000)
    {
        cout << -1;
        return 0;
    }
    cout << x;
}
   

Measured memory_bytes: 11124
#define  _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <algorithm>
#include <utility>
#include <functional>
#include <cstring>
#include <queue>
#include <stack>
#include <math.h>
#include <iterator>
#include <vector>
#include <string>
#include <set>
#include <math.h>
#include <iostream>
#include <random>
#include<map>
#include <iomanip>
#include <time.h>
#include <stdlib.h>
#include <list>
#include <typeinfo>
#include <list>
#include <set>
#include <cassert>
#include<fstream>
#include <unordered_map>
#include <cstdlib>
using namespace std;
#define Ma_PI 3.141592653589793
#define eps 0.00000001
#define LONG_INF 3000000000000000000
#define GOLD 1.61803398874989484820458
#define MAX_MOD 1000000007
#define REP(i,n) for(long long i = 0;i < n;++i)    
#define seg_size 524288
#include <ctype.h>
int main() {
	int a, b;
	cin >> a >> b;
	cout << a * b - (a+b-1) << endl;
}

Measured memory_bytes: 256
local read = setmetatable({}, {__index = function(t, k) local a = {} for i=1,#k do table.insert(a, '*'..string.sub(k, i, i)) end local r = io.read local u = table.unpack or unpack return function() return r(u(a)) end end})
read.N = function(N) local t={} for i=1,N do t[i]=read.n() end return t end
string.totable = function(s) local t={} local u=string.sub for i=1,#s do t[i] = u(s, i, i) end return t end
string.split = function(s) local t={} for w in string.gmatch(s, "[^%s]+") do table.insert(t, w) end return (table.unpack or unpack)(t) end
local function array(dimension, default_val) assert(type(default_val) ~= 'table') local n=dimension local m={}if default_val~=nil then m[1]={__index=function()return default_val end}end for i=2,n do m[i]={__index=function(p, k)local c=setmetatable({},m[i-1])rawset(p,k,c)return c end}end return setmetatable({},m[n])end
local function tostringxx(o, depth) depth = depth or 0 if depth > 10 then return "<too deep!>" end if o == _G then return "<_G>" end local indent0 = (" "):rep((depth) * 2) local indent1 = (" "):rep((depth+1) * 2) local indent2= (" "):rep((depth+2) * 2) if type(o) == 'table' then local keys = {} local types = {} for k in pairs(o) do types[type(k)] = true table.insert(keys, k) end local types_count = 0 local lasttype for k in pairs(types) do types_count = types_count + 1 lasttype = k end if types_count == 1 and (lasttype == 'string' or lasttype == 'number') then table.sort(keys) end local inside = {} for i=1,#keys do local k = keys[i] local v = o[k] if type(k) == 'string' then k = string.format('%q', k) end table.insert(inside, indent1 .. '['..tostring(k)..'] = ' .. tostringxx(v, depth + 1)) end return '{\n' .. table.concat(inside, ',\n') .. '\n' .. indent0 .. '}' else if type(o) == 'string' then o = string.format('%q', o) end return tostring(o) end end
local function richtraceback() local x = 2 while true do local info = debug.getinfo(x) if not info then break end local fname = '<' .. info.short_src .. ":" .. info.linedefined .. ">" if info.name then fname = info.name end print(info.short_src .. ":" .. info.currentline .. ": in " .. ("%q"):format(fname)) print(" LOCALS:") local p = 1 while true do local name, val = debug.getlocal(x,p) if not name then break end print(" " .. name .. ": " .. tostringxx(val, 3)) p = p + 1 end print(" UPVALUES:") for p=1,info.nups do local name, val = debug.getupvalue(info.func,p) if not name then break end print(" " .. name .. ": " .. tostringxx(val, 3)) end x = x + 1 end end
local function myassert(b) if not b then richtraceback() error("assertion failed") end end 
-----------------------
local N = read.n()
local sum = 0
local nums = {}
for i=1,N do
    local a = read.n()
    sum = sum + a
    nums[a] = (nums[a] or 0) + 1
end
local Q = read.n()
for i=1,Q do
    local b, c = read.nn()
    local cnt = (nums[b] or 0)
    local diff = cnt * c - cnt * b
    sum = sum + diff
    nums[b] = 0
    nums[c] =  (nums[c] or 0) + cnt
    print(sum)
end


Measured memory_bytes: 15920