akhauriyash/Code-Regression huggingface dataset:
#include <bits/stdc++.h>
using namespace std;
using ll = long long int;
const ll N = 1e2+9;
const ll K = 1e5+9;
const ll MOD = 1e9+7;

ll mod(ll n) {
    n %= MOD;
    if(n < 0)  n += MOD;
    return n;
}

ll addmod(ll &a, ll b) {
    return a = mod( mod(a) + mod(b) );
}

struct BIT {
    vector<ll> bit;
    int n;
    void init(int _n) {
        n = _n;
        bit.assign(n+1, 0);
    }

    void add(int i, ll x) {
        i++;
        while(i <= n)  addmod(bit[i], x), i += i&-i;
    }

    int sum(int i) {
        ll res = 0;
        while(i > 0)  addmod(res, bit[i]), i -= i&-i;
        return res;
    }
};


ll n, k;
ll a[N];
BIT dp[N];

int main() {
    cin >> n >> k;
    for(int i = 1; i <= n; i++)  cin >> a[i];
    for(int i = 0; i < N; i++)  dp[i].init(K);

    for(int ch = 1; ch <= n; ch++) {
        for(int ca = 0; ca <= k; ca++) {
            if(ch == 1)  dp[ch].add(ca, a[ch] >= ca);
            else if(ca == 0)  dp[ch].add(ca, 1);
        }
    }
    
    for(int ch = 2; ch <= n; ch++) {
        for(int ca = 1; ca <= k; ca++) {
            dp[ch].add(ca, dp[ch-1].sum(ca+1) - dp[ch-1].sum(ca-a[ch]));
        }
    }

    cout << mod(dp[n].sum(k+1) - dp[n].sum(k)) << endl;
}

Measured memory_bytes: 85760
#include<stdio.h>
int main(){
	int a,b,c,t;
	scanf("%d %d %d",&a,&b,&c);
if(a>b){
t=b;
b=a;
a=t;
}
if(b>c){
t=c;
c=b;
b=t;
}
if(a>c){
t=c;
c=a;
a=t;
}
if(a>b){
t=b;
b=a;
a=t;
}
printf("%d %d %d\n",a,b,c);
	return 0;
}

Measured memory_bytes: 596
#include<iostream>
using namespace std;
int main(){
	int a,b,c;
	cin>>a>>b>>c;
	int cnt=0;
	for(int i=a; i<=b; i++){
		if(c%i==0){
			cnt+=1;
		}
	}
	cout<<cnt<<"\n";
	return 0;
}


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

class Main{
    public static void main(String[] args){
	BufferedReader sc = new BufferedReader(new InputStreamReader(System.in));
	try{
	    String st;
	    while((st=sc.readLine())!=null){
		double a=0, b=0;
		Stack<Double> x = new Stack<Double>();
		String[] p = st.split(" ");
		for(int i=0; i<p.length; i++){
		    if(p[i].equals("-")){
			b = x.pop();
			a = x.pop();
			x.push(a-b);
		    }
		    else if(p[i].equals("+")){
			b = x.pop();
			a = x.pop();
			x.push(a+b);
		    }
		    else if(p[i].equals("*")){
			b = x.pop();
			a = x.pop();
			x.push(a*b);
		    }
		    else if(p[i].equals("/")){
			b = x.pop();
			a = x.pop();
			x.push(a/b);
		    }
		    else
			x.push(Double.valueOf(p[i]));
		}
		System.out.println(x.pop());
	    }
	}catch(Exception e){
	    System.out.println("Error");
	}
    }
}

Measured memory_bytes: 9812
#define _USE_MATH_DEFINES
#include <iostream>
#include <string>
#include <list>
#include <vector>
#include <queue>
#include <algorithm>
#include <climits>
#include <cstring>
#include <cmath>
#include <stack>
#include <iomanip>
#include <tuple>
#include <functional>
#include <cfloat>
#include <map>
#include <stdio.h>
#include <string.h>
using ll = long long;

//LLONG_MAX
#define int long long
#define CONTAINS(v,n) (find((v).begin(), (v).end(), (n)) != (v).end())
#define SORT(v) sort((v).begin(), (v).end())
#define RSORT(v) sort((v).rbegin(), (v).rend())
#define ARY_SORT(a, size) sort((a), (a)+(size))
#define MAX(a,b) (((a) > (b)) ? (a) : (b))
#define MIN(a,b) (((a) < (b)) ? (a) : (b))
#define REMOVE(v,a) (v.erase(remove((v).begin(), (v).end(), (a)), (v).end()))
#define REVERSE(v) (reverse((v).begin(), (v).end()))
#define LOWER_BOUND(v,a) (lower_bound((v).begin(), (v).end(), (a)))
#define UPPER_BOUND(v,a) (upper_bound((v).begin(), (v).end(), (a)))
#define REP(i, n) for (int (i) = 0; (i) < (n); (i)++)
#define CONTAINS_MAP(m, a) (m).find((a)) != m.end()
using namespace std;


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

	int d = a - b;
	int e = c - d;

	cout << MAX(0, e) << endl;
}











Measured memory_bytes: 256
a, b, h, m = gets.chomp.split(" ").map(&:to_i)
mizikai = h * 30 + m * 0.5
nagai = m * 6

if mizikai > nagai
    s = mizikai - nagai
    if s > 180
        s = 360 -s
    end
    c = Math.cos( s/ 180.0 * Math::PI)
    d = a**2 + b**2 - 2*a*b*c
    puts  (d**0.5).floor(9)
elsif mizikai == nagai
    puts (b-a).abs

else
    t = nagai - mizikai
    if t > 180
        t = 360 - t 
    end
    f = Math.cos( t/ 180.0 * Math::PI)
    g = a**2 + b**2 - 2*a*b*f
    puts  g**0.5
end

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

int main() {
    char S[105], T[105];
    cin >> S;
    cin >> T;
    
    for (long i = 0; i < strlen(T); i++) {
        char temp = S[strlen(T) - 1];
        for (long j = strlen(T) - 1; j > 0; j--) {
            S[j] = S[j-1];
        }
        S[0] = temp;
        string A = S, B = T;
        if (A.compare(B) == 0) {
            cout << "Yes" << endl;
            return 0;
        }
    }
    
    cout << "No" << endl;
    return 0;
}

Measured memory_bytes: 256
#include <bits/stdc++.h>                                                                                                                              
                                                                                                                                                      
using namespace std;                                                                                                                                  
                                                                                                                                                      
typedef long long ll;                                                                                                                                 
                                                                                                                                                      
main(){                                                                                                                                               
  ll n,k;                                                                                                                                             
  cin >> n >> k;                                                                                                                                      
  ll ans = 0;                                                                                                                                         
  for(ll i=1;i<=n;++i){                                                                                                                               
    ll a = i, b = k - a%k;                                                                                                                            
    if((a+b)%k!=0) continue;                                                                                                                          
    if(b==0) b += a;                                                                                                                                  
    if(a%k != b%k) continue;                                                                                                                          
    ll c = b;                                                                                                                                         
    b = (n-b)/k + 1; c = (n-c)/k + 1;                                                                                                                 
    ans += b*c;                                                                                                                                       
  }                                                                                                                                                   
  cout << ans << endl;                                                                                                                                
}

Measured memory_bytes: 256
#!/usr/bin/env python3

from typing import List


def main():
    N = int(input())
    args = list(map(lambda x: int(x), input().split()))
    counter = 0
    target = args
    while canDev(target):
        counter += 1
        target = dev2(target)

    print(counter)


def canDev(target: List[int]) -> bool:
    for item in target:
        if item % 2 != 0:
            return False
    return True


def dev2(target: List[int]) -> List[int]:
    return list(map(lambda x: int(x / 2), target))


if __name__ == "__main__":
    main()


Measured memory_bytes: 10152
a = input()
count = 0
words = []
while True:
  word = input().split()
  if word[0] == "END_OF_TEXT":
    break
  for subword in word:
    words.append(subword)
for b in words:
  if b.lower() == a:
    count += 1
print(count)
  
    


Measured memory_bytes: 5564
#include <bits/stdc++.h>
#define pb push_back
#define ff first
#define ss second
using namespace std;
using ll = long long;
using vi = vector<int>;
using pii = pair<int, int>;
ll p[100005], q[100005], r[100005];
int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
  priority_queue<ll, vector<ll>, greater<ll>> pq;
  int x, y, a, b, c;
  cin >> x >> y >> a >> b >> c;
  for (int i = 0; i < a; i++)
    cin >> p[i];
  for (int i = 0; i < b; i++)
    cin >> q[i];
  for (int i = 0; i < c; i++)
    cin >> r[i];
  sort(p, p + a, greater<ll>());
  sort(q, q + b, greater<ll>());
  // sort(r, r + c, greater<ll>());
  for (int i = 0; i < x; i++) {
    pq.push(p[i]);
  }
  for (int i = 0; i < y; i++) {
    pq.push(q[i]);
  }
  for (int i = 0; i < c; i++) {
    if (pq.top() < r[i]) {
      pq.pop(), pq.push(r[i]);
    }
  }
  ll ans = 0;
  while (!pq.empty()) {
    ans += pq.top();
    pq.pop();
  }
  cout << ans << endl;
}

Measured memory_bytes: 4852
#include <iostream>
#include <vector>
#include <queue>
#include <map>
#include <algorithm>
#include <string>
#include <set>

using namespace std;

int main() {
	int N, M, X; cin >> N >> M >> X;
	int tl = 0, tr = 0;
	for (int i = 0; i < M; i++) {
		int x; cin >> x;
		if (x < X) tl += 1;
		else tr += 1;
	}
	cout << min(tl, tr) << endl;
	return 0;
}

Measured memory_bytes: 256
#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <queue>
#include <vector>

#define INF (1e+9)
#define LLINF (1e+18)
#define LL long long

#define MAX_V 100

using namespace std;

typedef struct edge{
	int to;
	int cost;
	edge(){

	}
	edge(int _to,int _cost){
		to = _to;
		cost = _cost;
	}
	bool operator<(const edge &a)const{
		if(cost == a.cost) return to > a.to;
		return cost > a.cost;	//for asc
	}
}edge;


int cost[MAX_V + 1];
vector<edge> G[MAX_V + 1];
priority_queue<edge> pque;


int V;
int K;


void solve(int s){
	for(int i = 1;i <= V;i++){
		cost[i] = INF;
	}

	pque.push(edge(s,0));

	while(!pque.empty()){
		edge e = pque.top();pque.pop();
		if(cost[e.to] != INF) continue;
		cost[e.to] = e.cost;
		for(int i = 0;i < G[e.to].size();i++){
			int _to = G[e.to][i].to;
			int _cost = e.cost + G[e.to][i].cost;
			pque.push(edge(_to,_cost));
		}
	}

	return ;
}

int main (void){

	while(true){
		scanf("%d%d",&V,&K);
		if(V == 0 && K == 0) break;
		int query;
		int _u,_v,_cost;
		for(int i = 0;i < K;i++){
			scanf("%d",&query);
			if(query){
				scanf("%d%d%d",&_u,&_v,&_cost);
				G[_u].push_back(edge(_v,_cost));
				G[_v].push_back(edge(_u,_cost));
			}
			else {
				scanf("%d%d",&_u,&_v);
				solve(_u);
				if(cost[_v] == INF)puts("-1");
				else printf("%d\n",cost[_v]);
			}
		}
		for(int i = 1;i <= V;i++) G[i].clear();
	}

	return 0;
}

Measured memory_bytes: 1244
#include<bits/stdc++.h>
using namespace std;
#define pb push_back
#define eb emplace_back
#define f first
#define s second
#define ll long long int
#define ull unsigned long long int
#define fast ios_base::sync_with_stdio(false); cin.tie(NULL);
#define mod 1000000007
vector<int> g[111111];
int in[111111];
int dp[111111];
ll dfs(ll u)
{
    if(dp[u])return dp[u];
    ll ans=0;
    for(int v:g[u]){
        ans = max(ans , 1+dfs(v));
    }
    return dp[u]=(ans);
}
int main()
{
    fast;
    int n,m;
    cin>>n>>m;
    for(int i=0;i<m;i++)
    {
        int u,v;
        cin>>u>>v;
        g[u].pb(v);
        in[v]++;
    }
    ll ans=-1;
    for(int i=1;i<=n;i++)
    {
        if(!dp[i])
        {
            ans=max(ans,dfs(i));
        }
    }
    cout<<ans;
return 0;
}

Measured memory_bytes: 8832
#include<bits/stdc++.h>
#define PI 2.0*acos(0.0)
#define ll              long long int
#define gcd(a,b)        __gcd(a,b)
#define lcm(a,b)        ((a)*(b))/gcd(a,b)
#define MOD             1000000007
#define mx              123456789
using namespace std;
int n,m;
double a[110];
 
bool cmp(int a,int b)
{
	return a>b;
}
 
int main(void)
{
	double sum=0;
	cin>>n>>m;
	for(int i=0;i<n;i++)
	{
		cin>>a[i];
		sum+=a[i];
	}
	sort(a,a+n,cmp);
	double c=sum/(4*m);
	if(a[m-1]<c)
		printf("No");
	else
		printf("Yes");
	return 0;
}


Measured memory_bytes: 256
#include<iostream>
#include<cstdio>
#include<string>
#include<vector>
#include<cmath>
#include<algorithm>
#include<functional>
#include<iomanip>
#include<queue>
#include<ciso646>
#include<random>
#include<map>
#include<set>
#include<complex>
using namespace std;
typedef long long ll;
const ll MOD = 1000000007;
const ll INF = (ll)1000000007 * 1000000007;
const double EPS = 1e-9;
typedef pair<int, int> P;
typedef unsigned int ui;
#define stop char nyaa;cin>>nyaa;
#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 long double ld;
const ld eps=1e-8;
int main() {
	int n;
	int a[10000];
	cin >> n;
	rep(i, n) {
		cin >> a[i];
	}
	int cnt = 0;
	rep(i, n) {
		while (a[i] % 2==0) {
			a[i] /= 2; cnt++;
		}
	}
	cout << cnt << endl;
	return 0;
}

Measured memory_bytes: 256
#include<iostream>
#include<string>
#include<cstdio>
#include<vector>
#include<cmath>
#include<algorithm>
#include<functional>
#include<iomanip>
#include<queue>
#include<ciso646>
#include<random>
#include<map>
#include<set>
#include<complex>
#include<bitset>
#include<stack>
#include<unordered_map>
#include<utility>
using namespace std;
typedef long long ll;
typedef unsigned int ui;
const ll mod = 1000000007;
typedef long double ld;
const ll INF = 1e+14;
typedef pair<int, int> P;
#define stop char nyaa;cin>>nyaa;
#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<ld, ld> LDP;
typedef pair<ll, ll> LP;
#define fr first
#define sc second

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

  int N; cin >> N;
  ll M; cin >> M;
  ll A[100100], B[100100];
  B[0] = 0;
  map<ll, ll> m;
  map<ll, ll>::iterator ite;
  m[B[0]] ++;
  rep(i, N) {
      cin >> A[i];
      B[i + 1] = B[i] + A[i];
      B[i + 1] %= M;
      m[B[i + 1]] ++;
  }
  ll ans = 0;
  for(ite = m.begin(); ite != m.end(); ite++) {
      if(ite->second >= 2) {
          ll x = ite->second;
          ll X = x * (x - 1) / 2;
          ans += X;
      }
  }
  cout << ans << "\n";

  return 0;
}

Measured memory_bytes: 5760
#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>

int main(void){
    double r;
    std::cin >> r;
    std::cout << 2 * r * 3.141592653589793238462 << std::endl;

    return 0;
}

Measured memory_bytes: 3748
a = str(input())
b = str(input())
c = str(input())
cards = {'a':a, 'b':b, 'c':c}
turn = 'a'

while len(cards[turn]) > 0 :
  next_turn = cards[turn][0]
  cards[turn] = cards[turn][1:]
  turn = next_turn
print(turn.upper())   

Measured memory_bytes: 3060
#include <bits/stdc++.h>
using namespace std;
const long long mod=1e9+7;
int n;
long long a[200005];
long long f[200005];
long long s[200005];

int main() {
	scanf("%d",&n);
	for(int i=1;i<=n;i++) {
		scanf("%lld",&a[i]);
	}
	for(int i=1;i<=n;i++) {
		s[i]=(s[i-1]+a[i])%mod;
	}
	for(int i=1;i<=n;i++) {
		f[i]=(f[i-1]+(a[i]*s[i-1])%mod)%mod;
	}
	printf("%d",f[n]);
	return 0;
}

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


using namespace std;

#define re return
#define sz(a) (int)a.size()
#define mp(a, b) make_pair(a, b)
#define fi first
#define se second
#define re return
#define forn(i, n) for (int i = 0; i < int(n); i++)


typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> pii;
typedef pair<long long, long long> pll;
typedef long double ld;

int n, ans[2 * 100000];
vector<int> e[2 * 100000];

bool cmp(int a, int b) {
	re ans[a] < ans[b];
}

void dfs(int nu) {
	for (int v : e[nu]) {
		dfs(v);
	}
	sort(e[nu].begin(), e[nu].end(), cmp);
	ans[nu] = 0;
	for (int v : e[nu]) {
		ans[nu] = max(ans[nu] + 1, ans[v] + 1);
	}
	//cout << nu << " " << ans[nu] << endl;
}

int main() {
	iostream::sync_with_stdio(0);
	//freopen("a.in", "r", stdin);
	cin >> n;
	forn (i, n - 1) {
		int k;
		cin >> k;
		k--;
		e[k].push_back(i + 1);
	}
	dfs(0);
	cout << ans[0] << "\n";
}

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

int h, w;
int a[128][128];
int dx[2][6] = {{-1, 0, -1, 1, -1, 0}, {0, 1, -1, 1, 0, 1}};
int dy[2][6] = {{-1, -1, 0, 0, 1, 1}, {-1, -1, 0, 0, 1, 1}};

void init(int y=0, int x=0)
{
  if (y < 0 || h + 1 < y || x < 0 || w + 1 < x) return;
  if (a[y][x]) return;

  a[y][x] = 2;
  for (int i = 0; i < 6; i++){
    init(y + dy[y&1][i], x + dx[y&1][i]);
  }
}

int main()
{
  cin >> w >> h;
  for (int i = 1; i <= h; i++){
    for (int j = 1; j <= w; j++){
      cin >> a[i][j];
    }
  }

  init();
  
  int ans = 0;
  for (int i = 1; i <= h; i++){
    for (int j = 1; j <= w; j++){
      if (a[i][j] == 2) continue;
      
      int cnt = 0;
      for (int k = 0; k < 6; k++){
        if (a[i + dy[i&1][k]][j + dx[i&1][k]] != 2) {
          cnt++;
        }
      }
      ans += (6 - cnt);
    }
  }

  cout << ans << endl;
}

Measured memory_bytes: 3340
#include <iostream>
#include <cstring>
#include <algorithm>

#define MAX_N  100000

using namespace std;

#define debug(x) cout << #x << "==" << x << endl;
//入力
int K;
int N;
int a[MAX_N + 1];

typedef long long ll;


int main() {

  cin >> N >> K;
  for(int i = 0; i < N; i++) {
    cin >> a[i];
  }


  ll cumsum[MAX_N + 1];
  ll cumsum_pos[MAX_N + 1];
  cumsum[0] = 0;
  cumsum_pos[0] = 0;
  for(int i = 1; i < N+1; i++) {
    cumsum[i] = cumsum[i-1] + a[i-1];
    cumsum_pos[i] = cumsum_pos[i-1] + max(0, a[i-1]);
  }

  ll mx = 0;
  for(int i = 0; i < N - K + 1; i++) {

    ll all_black = cumsum[i+K] - cumsum[i];
    ll all_white = 0;

    ll other = cumsum_pos[i] +  (cumsum_pos[N] - cumsum_pos[i+K]) ;

    mx = max(mx, other + max(all_black, all_white));

  }

  cout << mx << endl;

  return 0;

}


Measured memory_bytes: 2176
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <cassert>
#include <iostream>
#include <sstream>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <utility>
#include <numeric>
#include <algorithm>
#include <bitset>
#include <complex>
#include <array>
#include <list>
#include <stack>
#include <valarray>

using namespace std;

typedef unsigned uint;
typedef long long Int;
typedef unsigned long long UInt;

const int INF = 1001001001;
const Int INFLL = 1001001001001001001LL;

template<typename T> void pv(T a, T b) { for (T i = a; i != b; ++i) cout << *i << " "; cout << endl; }
template<typename T> void chmin(T& a, T b) { if (a > b) a = b; }
template<typename T> void chmax(T& a, T b) { if (a < b) a = b; }
int in() { int x; scanf("%d", &x); return x; }
double fin() { double x; scanf("%lf", &x); return x; }
Int lin() { Int x; scanf("%lld", &x); return x; }

Int A[100050];

int main() {
  int K = in();
  for (int i = 0; i < K; ++i) {
    A[i] = lin();
  }

  Int lo = 2, hi = 2;
  for (int i = K - 1; i >= 0; --i) {
    lo = (lo + A[i] - 1) / A[i] * A[i];
    hi = hi / A[i] * A[i];
    if (lo > hi) {
      puts("-1");
      return 0;
    }
    hi += A[i] - 1;
  }

  printf("%lld %lld\n", lo, hi);

  return 0;
}


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

int main() {
  int N,R;
  cin >> N >>R;
  if(N>=10) cout << R;
  else cout << R + 100*(10-N);
}



Measured memory_bytes: 256
#include<bits/stdc++.h>
typedef long long ll;
using namespace std;
int INF = 1LL << 30;
int MOD = 1e9+7;
main(){
    int N,M;
    cin >> N >> M;
    ll ans = 1;
    for(int i = 1;i <= N;i++){
        ans = ans * i % MOD;
    }
    for(int i = 1;i <= M;i++){
        ans = ans * i % MOD;
    }
    if(!(N ^ M))ans = (ans*2) % MOD;
    if(abs(N - M) >= 2)ans = 0;    
    cout << ans << endl;
}

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

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

int main()
{
    double H,M,A,B,c,ans;
    cin>>A>>B>>H>>M;

    c = (H/12 + (M/60)/12 - M/60) * 360  * acos(-1)/180;
    
    cout<<setprecision(20)<<fixed;
    cout<<sqrt(A*A + B*B - 2*A*B*cosl(c))<<endl;

    return 0;
}

Measured memory_bytes: 4132
#include<bits/stdc++.h>
#define rep(i,N) for(int i=0;i<(N);i++)
#define FOR(i,a,b) for(int i=(a);i<(b);i++)
using namespace std;
const long long MOD = 1e9 + 7;
const long long INF = 1e12;
const int inf = 1e9;
const int mod = 1e9+7;
typedef long long ll;
typedef pair<ll,int> P;
typedef set<int> S;
int main(){
	cout << fixed << setprecision(10);
	int n,k,r,s,p;
	cin >> n >> k >> r >> s >> p;
	string t;
	cin >> t;
	int  ans = 0;
	rep(i,k){
		int pre = -1;
		for(int j = i ; j < t.size(); j += k){
			if(t[j] == 'r'){
				if(pre == 0){
					pre = -1;
				}else{
					pre = 0;
					ans += p;
				}
			}else if(t[j] == 's'){
				if(pre == 1){
					pre = -1;
				}else{
					pre = 1;
					ans += r;
				} 
			}else{
				if(pre == 2){
					pre = -1;
				}else{
					pre = 2;
					ans += s;
				}
			}
		}
	}
	cout << ans << endl;
	return 0;
}

Measured memory_bytes: 512
#include <algorithm>
#include <cassert>
#include <cctype>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <vector>
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)
#define show(x) cout << #x << " = " << (x) << endl;
using namespace std;
using ll = long long;
using pii = pair<int,int>;
int main(){
    int n;
    cin >> n;
    vector<int>a(n);
    int sum = 0;
    rep(i,n){
        cin >> a[i];
        sum += a[i];
    }
    int hoge = 1e9;
    int ans = 0;
    rep(i,n){
        if(hoge > abs(n*a[i]-sum)){
            ans = i;
            hoge = abs(n*a[i]-sum);
        }
    }
    cout << ans << endl;
}

Measured memory_bytes: 256
import statistics
n = int(input())
X = list(map(int, input().split()))
X_ = sorted(X)
med = X_[n//2-1]
med_2 = X_[n//2]
for i in range(n):
    if X[i]>med:
        print(med)
    else:
        print(med_2)

Measured memory_bytes: 34176
// Math = ♥️

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;

#define ll long long
#define ld long double
typedef pair<ll, ll> ii;
typedef vector<ll> vi;
typedef vector<vi> vvi;
typedef vector<ii> vii;
typedef vector<vii> vvii;
#define pq priority_queue
#define ff first
#define ss second
#define pb push_back
#define mp make_pair
#define all(c) (c).begin(), (c).end()
#define ordered_set                            \
    tree<ll, null_type, less<ll>, rb_tree_tag, \
         tree_order_statistics_node_update>

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

    ll x, y, z;
    cin >> x >> y >> z;

    swap(x, y);
    swap(x, z);

    cout << x << " " << y << " " << z << "\n";
}


Measured memory_bytes: 768
#include <iostream>
#include <cstdio>
#include <iomanip>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <bitset>
#include <stack>
#include <utility>
#include <numeric>
#include <algorithm>
#include <functional>
#include <cctype>
#include <complex>
#include <string>
#include <sstream>
#include <fstream>
#include <cassert>
using namespace std;

//common
typedef long long i64,ll;

#define BR "\n"
#define ALL(c) (c).begin(),(c).end()
#define REP(i,n) for(int i=0;i<(int)(n);++i)
#define FOR(i,l,r) for(int i=(l);i<(int)(r);++i)
#define EACH(it,o) for(auto it = (o).begin(); it != (o).end(); ++it)
#define IN(l,v,r) ((l)<=(v) && (v)<(r))

//debug
#ifdef NDEBUG
#define DUMP(x)
#define DUMPLN(x)
#define DEBUG(x)
#define DEBUGLN(x)
#define LINE()
#define LINELN()
#define CHECK(exp,act)
#define STOP(e)
#else
#define DUMP(x)  cerr << #x << " = " << (x)
#define DUMPLN(x)  DUMP(x) <<endl
#define DEBUG(x) DUMP(x) << LINE() << " " << __FILE__
#define DEBUGLN(x) DEBUG(x)<<endl
#define LINE()    cerr<< " (L" << __LINE__ << ")"
#define LINELN()    LINE()<<endl
#define CHECK(exp,act)  if(exp!=act){DUMPLN(exp);DEBUGLN(act);}
#define STOP(e)  CHECK(e,true);if(!(e)) exit(1);
#endif

template<class T> inline string toString(const vector<T>& x) {
	stringstream ss;
	REP(i,x.size()){
		if(i!=0)ss<<" ";
		ss<< x[i];
	}
	return ss.str();
}

template<class T> inline string toString(const vector<vector<T> >& map) {
	stringstream ss;
	REP(i,map.size()){
		if(i!=0)ss<<BR;
		ss<< toString(map[i]);
	}
	return ss.str();
}
template<class K,class V>  string toString(map<K,V>& x) {
	string res;stringstream ss;
	for(auto& p:x)ss<< p.first<<":" << p.second<<" ";
	return ss.str();
}

string BITtoString(int bit){
    stringstream ss;
    while(bit!=0){ss<<(bit%2);bit/=2;}
    string res=ss.str();reverse(ALL(res));
    return res;
}

template<typename T,typename V> inline T pmod(T v,V MOD){
	return (v%MOD+MOD)%MOD;
}


int INF=1<<28;

int dp[1<<16],ndp[1<<16];
class Main{
public:

	void run(){
		while(true){
			int n,c;cin >> n >> c;if(n==0 && c==0)break;
			vector<int> as(n,0);
			REP(y,n)REP(i,16){
				int v;cin >> v;as[y]+=v<<i;
			} 
			vector<int> bs(c+1,0);
			REP(y,c)REP(i,16){
				int v;cin >> v;bs[y]+=v<<i;	
			} 


			//残ってる光
			vector<vector<int>> score(c+1,vector<int>(1<<16));
			REP(y,c+1)REP(bit,1<<16){
				int bitc=0;REP(i,16)if(((bs[y]&bit)>>i)&1)bitc++;
				score[y][bit]=bitc;
			}
			REP(bit,1<<16)dp[bit]=ndp[bit]=-INF;
			dp[0]=0;

			REP(y1,n){
				//更新
				REP(bit,1<<16)if(dp[bit]!=-INF){
					//on
					int nbit=as[y1]|bit;

					//touch
					REP(y2,c+1){
						int nnbit=nbit&~bs[y2];
						ndp[nnbit]=max(ndp[nnbit],dp[bit]+score[y2][nbit]);
					}
				}

				//next
				REP(bit,1<<16)dp[bit]=ndp[bit];
			}

			int Mv=0;
			REP(bit,1<<16)Mv=max(Mv,dp[bit]);
			cout << Mv <<endl;
		}
	}
};

 int main(){
	//ios::sync_with_stdio(false);
 	Main().run();
 	return 0;
 }

Measured memory_bytes: 9760
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
#define int long long
#define sim template <class c
#define ris return *this
#define dor > debug &operator<<
#define eni(x)                                                                    \
	sim > typename enable_if<sizeof dud<c>(0) x 1, debug &>::type operator<<(c i) \
	{
sim > struct rge
{
	c b, e;
};
sim > rge<c> range(c i, c j) { return rge<c>{i, j}; }
sim > auto dud(c *x) -> decltype(cerr << *x, 0);
sim > char dud(...);
struct debug
{
#ifndef LOCAL
	~debug()
	{
		cerr << endl;
	}
	eni(!=) cerr << boolalpha << i;
	ris;
} eni(==) ris << range(begin(i), end(i));
}
sim, class b dor(pair<b, c> d)
{
	ris << "(" << d.first << ", " << d.second << ")";
}
sim dor(rge<c> d)
{
	*this << "[";
	for (auto it = d.b; it != d.e; ++it)
		*this << ", " + 2 * (it == d.b) << *it;
	ris << "]";
}
#else
	sim dor(const c &)
	{
		ris;
	}
#endif
}
;
#define imie(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] "
template <typename T>
using indexed_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
#define int long long
#define ld long double
#define pb push_back
#define mp make_pair
#define in insert
#define fi first
#define se second
#define lb lower_bound
#define ub upper_bound
#define mod 1000000007
#define sz(v) (int)(v.size())
#define all(x) x.begin(), x.end()
#define allr(x) x.rbegin(), x.rend()
#define prec fixed << setprecision(12)
#define rep(i, a, b) for (int i = a; i <= b; ++i)
#define repr(i, a, b) for (int i = a; i >= b; i--)
#define test  \
	int t;    \
	cin >> t; \
	while (t--)
typedef vector<int> vi;
typedef pair<int, int> pii;
typedef set<int> si;
typedef map<int, int> mii;
typedef vector<pii> vpi;

void solve()
{
	int n, l;
	cin >> n >> l;
	int ans = 0;
	rep(i, 0, n - 1) ans += l + i;
	if (l <= 0 && l + n - 1 >= 0)
		cout << ans;
	else if (l < 0)
		cout << (ans - (l + n - 1));
	else
		cout << ans - l;
}

int32_t main()
{
	ios::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif
	// test
	{
		solve();
	}
	return 0;
}


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

int main()
{
	int n, m;
	cin>>n>>m;
	vector<int> a(n);
	int t=0;
	int sum=0;
	for(int i = 0; i < n; i++)
	{
		cin>>a[i];
		sum+=a[i];
	}
	for(int i = 0; i < n; i++)
	{
		int prop = ceil((sum*1.0)/(4*m));
		// cout<<prop<<endl;
		// p
		if(prop<=a[i])
		{
			t++;
		}
	}
	if(t>=m)
	{
		cout<<"Yes"<<endl;
	}
	else cout<<"No"<<endl;


}

Measured memory_bytes: 256
#include <bits/stdc++.h>
#define rep(i,n) for(int i=0;i<(int)(n);i++)
using namespace std;
using ll = long long ;
using P = pair<int,int> ;
using pll = pair<long long,long long>;
constexpr int INF = 1e9;
constexpr long long LINF = 1e17;
constexpr int MOD = 1000000007;

int main(){
    int q;
    cin >> q;
    while(q--){
        string x,y;
        cin >> x >> y;
        vector<vector<int>> dp(x.size()+1,vector<int>(y.size()+1,0));
        rep(i,x.size())rep(j,y.size()){
            dp[i+1][j+1] = max(dp[i+1][j],dp[i][j+1]);
            if(x[i]==y[j]) dp[i+1][j+1] = max(dp[i+1][j+1],dp[i][j] + 1);
        }
        cout << dp.back().back() << endl;
    }
    return 0;
}


Measured memory_bytes: 6776
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Numerics;
using System.Text;
using System.Threading.Tasks;
using static System.Console;
using static System.Math;
class Z { static void Main() => new K(); }
class K
{
	int F => int.Parse(Str);
	long FL => long.Parse(Str);
	int[] G => Strs.Select(int.Parse).ToArray();
	long[] GL => Strs.Select(long.Parse).ToArray();
	string Str => ReadLine();
	string[] Strs => Str.Split(new char[] { ' ', '\t' }, StringSplitOptions.RemoveEmptyEntries);
	const int MOD = 1000000007;
	public K()
	{
		SetOut(new StreamWriter(OpenStandardOutput()) { AutoFlush = false });
		Solve();
		Out.Flush();
	}
	void Solve()
	{
		var I = G;
		var N = I[0];
		var M = I[1]/1000;
		//   x+ y+z=N
		// 10x+5y+z=M
		// 5y+5z=5N-5x
		// 5y+ z=M-10x
		// 4z=5N-M+5x
		for (var x = 0; x <= N; x++)
		{
			var z4 = 5 * (N + x) - M;
			if (z4 < 0 || z4 % 4 != 0) continue;
			var z = z4 / 4;
			var y = N - x - z;
			if (y < 0) continue;
			WriteLine($"{x} {y} {z}");
			return;
		}
		WriteLine("-1 -1 -1");
	}
}


Measured memory_bytes: 13396
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography.X509Certificates;

class Progaram
{
    static void Main(String[] args)
    {
        var l = Console.ReadLine().Split(' ').Select(x => int.Parse(x)).ToArray();
        var n = l[0]; var r = l[1];

        if (n >= 10)
        {
            Console.WriteLine(r);
        }
        else
        {
            Console.WriteLine(r +100 * (10 - n));
        }
    }
}


Measured memory_bytes: 28156
#include<iostream>
#include<vector>
using namespace std;
#define rep(i,n) for(int i=0;i<(n);++i
using ll = long long;
using P = pair<int, int>;
#include<algorithm>
#include<math.h>
#include<map>
#include<queue>

int main() {
	int a, b;
	cin >> a >> b;
	int c = a + b;
	int d = a - b;
	c = max(c, d);
	int e = a * b;
	cout << max(c, e);
	return 0;
}

Measured memory_bytes: 256
H,W = map(int,input().split())
c = list(list(map(int,input().split()))for i in range(10))

cost = [float('inf')]*10
cost[1] = 0
for i in range(10):
    for y in range(10):
        for x in range(10):
            cost[x] = min(cost[x],cost[y]+c[x][y])

A = list(list(map(int,input().split()))for i in range(H))
ans = 0
for a in A:
    for i in a:
        if i != -1:
            ans += cost[i]
print(ans)

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

template <typename T1, typename T2>
struct less_second {
    typedef pair<T1, T2> type;
    bool operator ()(type const& a, type const& b) const {
        return a.second > b.second;
    }
};

int main(){
	int n;cin>>n;
	string s[n];
	map<string,int> mp;
	for(int i=0;i<n;i++){
		cin>>s[i];	
	}
	for(int i=0;i<n;i++){
		
		mp[s[i]]++;
	}
	
	vector<pair<string, int> > mapcopy(mp.begin(), mp.end());
	sort(mapcopy.begin(), mapcopy.end(), less_second<string, int>());
	vector<string> vs;
	int max = -1;
	for(auto &x : mapcopy){
		if(x.second>=max){
			max = x.second;
			vs.push_back(x.first);
		}
		
	}
	                         
	sort(vs.begin(),vs.end());
	for(int i=0;i<(int)vs.size();i++) cout<<vs[i]<<endl;
return 0;
}


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

#define fs first
#define se second
#define all(v) v.begin(), v.end()
#define rep(i, n) for (int i = 0; i < n; ++i)
#define pb emplace_back

using pii = pair<int, int>;
using vi = vector<int>;
using lint = long long;

const int inf = 1001001001;
const lint linf = 1001001001001001001ll;
const int mod = 1e9 + 7;
const int dx[]{0, 1, 0, -1, -1, -1, 1, 1}, dy[]{1, 0, -1, 0, -1, 1, -1, 1};

template<typename T> inline bool chmin(T &a, T b) { if (a > b) { a = b; } return a > b; }
template<typename T> inline bool chmax(T &a, T b) { if (a < b) { a = b; } return a < b; }
template<typename T> inline void print(const T &x, string s = "\n") { cout << x << s; }
template<typename T> inline void print(const vector<T> &v, string s = " ") 
{ rep(i, v.size()) cout << v[i] << (i + 1 == v.size() ? "\n" : s); }
inline bool inside(int y, int x, int H, int W) { return 0 <= y && y < H && 0 <= x && x < W; }
inline lint in() { lint x; std::cin>>x; return x; }

int d(lint n) {
	int ret = 0;
	while (n) {
		n /= 10;
		ret++;
	}

	return ret;
}

int main() {
	lint n = in();

	int ans = inf;
	for (int i = 1; i <= min(100000ll, n); ++i) {
		if (n % i) continue;
		chmin(ans, max(d(i), d(n / i)));
	}

	cout << ans << endl;
}


Measured memory_bytes: 256
<?php

fscanf(STDIN, "%d %d", $n, $t);

$c_arr = [];
$t_arr = [];
for ($i = 0; $i < $n; ++$i) {
    $inputs = explode(' ', trim(fgets(STDIN)));
    $c_arr[] = $inputs[0];
    $t_arr[] = $inputs[1];
}

$min = 1001;
$exists = false;
for ($i = 0; $i < $n; ++$i) {
    if ($t_arr[$i] <= $t) {
        if ($c_arr[$i] < $min) {
            $min = $c_arr[$i];
            $exists = true;
        }
    }
}

echo $exists ? $min : 'TLE';


Measured memory_bytes: 4728
#include <bits/stdc++.h>
  
using namespace std;
  
#define rep(i,n) REP(i,0,n)
#define REP(i,s,e) for(int i=(s); i<(int)(e); i++)
#define repr(i, n) REPR(i, n, 0)
#define REPR(i, s, e) for(int i=(int)(s-1); i>=(int)(e); i--)
#define pb push_back
#define all(r) r.begin(),r.end()
#define rall(r) r.rbegin(),r.rend()
#define fi first
#define se second
  
typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
 
const int INF = 1e9;
const ll MOD = 1e9 + 7;
double EPS = 1e-8;

ll dp[int(1e5+10)];

int main(){
    int n, t;
    cin >> n >> t;
    vi a(n);
    rep(i, n) cin >> a[i];
    // sort(all(a));
    // int cnt = 0;
    // do {
    //     bool f = true;
    //     rep(i, n-1) {
    //         if(a[i]-a[i+1] > t) f = false;
    //     }
    //     if(f) {
    //         rep(i, n) {
    //             cout << " " << a[i];
    //         }
    //         cout << endl;
    //         cnt++;
    //     }
    // }while(next_permutation(all(a)));
    // cout << cnt << endl;
    sort(all(a));
    vector<pii> b(n);
    rep(i, n) b[i] = pii(a[i], i);
    dp[0] = 1;
    REP(i, 1, n) {
        int k = lower_bound(all(b), pii(a[i], i)) - lower_bound(all(b), pii(a[i]-t, -INF));
        dp[i] = (dp[i-1] + (dp[i-1]*k)%MOD)%MOD;
    }
    cout << dp[n-1] << endl;
    return 0;
}


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

int d[110];

int main(){
	int N, a[101];
	cin >> N;
	for(int i=0; i<N; i++){
		cin >> d[i];
	}

	for(int i=0; i<101; i++){
		a[i] = 0;
	}

	for(int i=0; i<N; i++){
		a[d[i]]++;
	}

	int counter = 0;
	for(int i=0; i<101; i++){
		if(a[i] != 0){
			counter++;
		}
	}
	cout << counter <<endl;

	return 0;
}


Measured memory_bytes: 256
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef double db;
typedef pair<ll,ll> P;
#define pb push_back
#define ft first
#define sd second
#define mp make_pair
#define fr(i,n) for(int i=0;i<n;i++)
#define Fr(i,n) for(int i=0;i++<n;)
#define ifr(i,n) for(int i=n-1;i>=0;i--)
#define iFr(i,n) for(int i=n;i>0;i--)

const ll MOD=1e9+7;

ll ad(ll x,ll y=0){
  return (x+y)%MOD;
}

int main(){
  ll n,dp[70][3]={};
  cin>>n;
  dp[62][0]=1;
  ifr(i,62){
    if((n>>i)&1){
      dp[i][0]=dp[i+1][0];
      dp[i][1]=ad(dp[i+1][0],dp[i+1][1]);
      dp[i][2]=ad(2*dp[i+1][1],3*dp[i+1][2]);
    }
    else{
      dp[i][0]=ad(dp[i+1][0],dp[i+1][1]);
      dp[i][1]=dp[i+1][1];
      dp[i][2]=ad(dp[i+1][1],3*dp[i+1][2]);
    }
  }
  cout<<ad(dp[0][0]+dp[0][1]+dp[0][2])<<endl;
}

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

#define rep(i,x,y) for(ll i=(x);i<(y);i++)
#define rrep(i,x,y) for(ll i=(ll)(y)-1;i>=(x);i--)
#define all(x) (x).begin(),(x).end()
#ifdef LOCAL
#define dump(x)  cerr << #x << " = " << (x) << endl
#define debug(x) cerr << #x << " = " << (x) << " (L" << __LINE__ << ")" << " " << __FILE__ << endl
#define dassert(...) assert(__VA_ARGS__)
#else
#define dump(x)
#define debug(x)
#define dassert(...)
#endif
//#define int long long

typedef long long ll;
const ll MOD = 1e9 + 7;
const long double EPS = 1e-8;

void solve(std::string S){
    cout << ((S[2] == S[3] && S[4] == S[5]) ? "Yes" : "No") << endl;
}

signed main(){
    // ios_base::sync_with_stdio(false);
    // cin.tie(NULL);
    std::string S;
    std::cin >> S;
    solve(S);
    return 0;
}


Measured memory_bytes: 256
import sys,queue,math,copy,itertools,bisect,collections,heapq

def main():
    sys.setrecursionlimit(10**7)
    INF = 10**18
    MOD = 10**9 + 7
    LI = lambda : [int(x) for x in sys.stdin.readline().split()]
    _LI = lambda : [int(x)-1 for x in sys.stdin.readline().split()]
    NI = lambda : int(sys.stdin.readline())
    SI = lambda : sys.stdin.readline().rstrip()

    N = NI()
    A = LI()
    t = A[0]
    ans = 0
    for x in A[1:]:
        if x < t:
            ans += t - x
        else:
            t = x
    print(ans)

if __name__ == '__main__':
    main()

Measured memory_bytes: 95588
#include<iostream>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<algorithm>
using namespace std;

typedef pair<int,int> pii;

int main(){
    int n;
    cin >> n;
    vector<int> v[n], deg(n, 0);
    vector<pii> vp;
    for(int i = 0; i < n-1; i++){
        int a, b;
        cin >> a >> b;
        a--, b--;
        v[a].push_back(b);
        v[b].push_back(a);
        vp.push_back({a,b});
        deg[a]++, deg[b]++;
    }
    int k = *max_element(deg.begin(), deg.end());
    cout << k << endl;
    set<int> used[n];
    int f;
    for(int i = 0; i < n; i++)  if(deg[i] == k) f = i;
    map<pii,int> col;
    queue<int> q;
    q.push(f);
    vector<bool> vis(n, 0);
    vis[f] = true;
    while(!q.empty()){
        int x = q.front();  q.pop();
        int now = 1;
        for(int next : v[x]){
            if(vis[next])   continue;
            vis[next] = true;
            while(used[x].count(now))  now++;
            used[x].insert(now);
            col[{min(x,next),max(x,next)}] = now;
            used[next].insert(now);
            q.push(next);
        }
    }
    for(pii p : vp) cout << col[p] << endl;
    return 0;
}

Measured memory_bytes: 27508
# coding: UTF-8
n,m,l = map(int,raw_input().split(" "))
a_matrix = [map(int,raw_input().split()) for i in range(n)]
b_matrix = [map(int,raw_input().split()) for i in range(m)]
tmp3_matrix = []

c_matrix = []
for i in range(n):
    tmp2_matrix = []
    for j in range(l):
        sum = 0
        for k in range(m):
            sum += a_matrix[i][k] * b_matrix[k][j]
        tmp2_matrix.append(sum)
    tmp3_matrix.extend([tmp2_matrix])
c_matrix.extend(tmp3_matrix)
for x in range(n):
    print " ".join(map(str,c_matrix[x]))


Measured memory_bytes: 5376
n=int(input())
a=list(map(int,input().split()))
v=[0 for _ in range(10**5+1)]
s=0
for x in a:
    v[x]+=1
    s+=x
    
q=int(input())
for _ in range(q):
    b,c = map(int,input().split())
    if v[b]>0:
        x = v[b]
        v[c] += x
        v[b] = 0
        s += (c-b)*x
    print(s)

Measured memory_bytes: 21152
#include<iostream>
using namespace std;
int main() {
    int H;
    int W;
    int i;
    int j;
    while (1) {
        cin >> H;
        cin >> W;
        if (H==0) {
            if (W==0) break;
        }
        for (i=0;i<H;i++) {
            for (j=0;j<W;j++) {
                cout << "#";
            } cout << endl;
        }
        cout << endl;
    }
    return 0;
} 


Measured memory_bytes: 3080
#include <iostream>
#include <vector>
#include <algorithm>
#include <map>
#include <cstring>
#include <math.h>
#include <bitset>
#include <queue>
#include <set>
#include <iomanip>
#include <math.h>
#include <assert.h>
// #include<bits/stdc++.h>
using namespace std;
typedef long long ll;
constexpr long long int INFLL = 1001001001001001LL;
constexpr long long int infll = 1001001001001001LL;
constexpr int INF = 1000000007;
constexpr int inf = 1000000007;
const int mod = 1000000007;

template <class T>
inline bool chmin(T &a, T b)
{
    if (a > b)
    {
        a = b;
        return true;
    }
    return false;
}
template <class T>
inline bool chmax(T &a, T b)
{
    if (a < b)
    {
        a = b;
        return true;
    }
    return false;
}

ll gcd(ll a, ll b)
{
    if (a % b == 0)
    {
        return (b);
    }
    else
    {
        return (gcd(b, a % b));
    }
}

ll lcm(ll a, ll b)
{
    return a / gcd(a, b) * b;
}

inline double nCr(const int n, int r)
{

    if (n == 0)
    {
        return 0;
    }
    if (r == 0)
    {
        return 1;
    }
    if (r == 1)
    {
        return n;
    }
    if (n == r)
    {
        return 1;
    }

    if (r > n / 2)
    {
        r = n / 2;
    }

    double result = 1;
    for (double i = 1; i <= r; i++)
    {
        result *= (n - i + 1) / i;
    }

    return (result);
}

template <typename T>
T seinomi(T a)
{
    if (a > 0)
    {
        return a;
    }
    else
    {
        return 0;
    }
}

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

int main()
{
    ll a, b, c;
    ll x, y;
    cin >> a >> b >> c >> x >> y;
    ll ab = c * 2;
    ll ans = infll;
    for (int i = 0; i <= max(x, y); i++)
    {
        ll sum = ab * i + seinomi(x - i) * a + seinomi(y - i) * b;
        chmin(ans, sum);
    }
    cout << ans << endl;
}


Measured memory_bytes: 256
#include <iostream>
#include<string>
#include<math.h>
#include<vector>
#include<algorithm>
#include<array>
#include<functional>
#include<iomanip>
#include<ios>
using namespace std;
#define FOR(i,n) for(int (i)=0;(i)<(n);(i)++)

typedef int value_type; /* ソートするキーの型 */

value_type med3(value_type x, value_type y, value_type z)
/* x, y, z の中間値を返す */
{
    if (x < y)
        if (y < z) return y; else if (z < x) return x; else return z; else
        if (z < y) return y; else if (x < z) return x; else return z;
}

void quicksort(value_type a[], int left, int right,value_type index[])
/* クイックソート
 * a     : ソートする配列 
 * left  : ソートするデータの開始位置
 * right : ソートするデータの終了位置
 * index: index配列
 */
{
    if (left < right) {
        int i = left, j = right;
        value_type tmp, pivot = med3(a[i], a[i + (j - i) / 2], a[j]); /* (i+j)/2ではオーバーフローしてしまう */
        while (1) { /* a[] を pivot 以上と以下の集まりに分割する */
            while (a[i] < pivot) i++; /* a[i] >= pivot となる位置を検索 */
            while (pivot < a[j]) j--; /* a[j] <= pivot となる位置を検索 */
            if (i >= j) break;
            tmp = a[i]; a[i] = a[j]; a[j] = tmp; /* a[i],a[j] を交換 */
            tmp = index[i]; index[i] = index[j]; index[j] = tmp;
            i++; j--;
        }
        quicksort(a, left, i - 1,index);  /* 分割した左を再帰的にソート */
        quicksort(a, j + 1, right,index); /* 分割した右を再帰的にソート */
    }
}


int dfs(int n,int N){
    if (n>N){
        return 0;
    }
    

    int ret;
    
    return ret;
}
int main(){
    int N;
    cin >>N;
    int H[N];
    FOR(i,N){
        cin >>H[i];
    }

    int ans=1;
    for(int i=1;i<N;i++){
        bool x=true;
        for(int j=0;j<i;j++){
            if(H[j]>H[i]){
                x=false;
            }
        }
        if(x){
            ans++;
        }
    }
    std::cout << ans<< endl;
    return 0;
}

Measured memory_bytes: 256
#include <iostream>
#include <algorithm>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#include <queue>
#include <cmath>

// output
#define SPBR(w, n) std::cout<<(w + 1 == n ? '\n' : ' ');
#define YES cout << "YES" << endl
#define Yes cout << "Yes" << endl
#define NO cout << "NO" << endl
#define No cout << "No" << endl
// utility
#define ALL(i) (i).begin(), (i).end()
#define FOR(i, a, n) for(int i=(a);i<(n);++i)
#define RFOR(i, a, n) for(int i=(n)-1;i>=(a);--i)
#define REP(i, n) for(int i=0;i<int(n);++i)
#define RREP(i, n) for(int i=int(n)-1;i>=0;--i)
#define IN(a, x, b) (a<=x && x<b)
#define OUT(a, x, b) (x<a || b<=x)
template<class T> inline T chmax(T & a, const T b) { return a = (a < b) ? b : a; }
template<class T> inline T chmin(T& a, const T b) { return a = (a > b) ? b : a; }

// type/const
#define int ll
using ll = long long;
using ull = unsigned long long;
using ld = long double;
const int MOD = 1000000007;
const int INF = 1e18;

using namespace std;

vector<int> fact;
void init(int N){
  fact = vector<int>(N);
  fact[0] = 1;
  FOR(i, 1, N){
    fact[i] = fact[i-1]*i%MOD;
  }
}

int mypow(int a, int b){
  int sum = 1;
  while(b > 0){
    if(b&1) sum = sum*a%MOD;
    a = a*a%MOD;
    b>>= 1;
  }
  return sum%MOD;
}

int comb(int n, int r){
  if(r > n) return 0;
  return fact[n]*mypow(fact[r]*fact[n-r]%MOD, MOD-2)%MOD;
}

signed main() {
  int N, K;
  cin >> N >> K;

  init(N);

  vector<int> A(N);
  REP(i, N) cin >> A[i];

  sort(ALL(A));
  int ans = 0;
  REP(i, N){
    ans = (ans-A[i]*comb(N-i-1, K-1))%MOD;
    ans = (ans+A[i]*comb(i, K-1))%MOD;
  }

  cout << (ans%MOD+MOD)%MOD << endl;

  return 0;
}

Measured memory_bytes: 1792