stablegradients/pie-gem5-pairs huggingface dataset:
// Slower C++ solution:
#include <bits/stdc++.h>

using namespace std;

#define ll long long

#define pb push_back

#define mp make_pair

#define fi first

#define se second

#define si size()

#define bk back()

#define popb pop_back()

#define fastio ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL)

#define endl "\n"

#define lb lower_bound

#define ub upper_bound

#define emp empty()

#define beg begin()

#define en end()

#define ump unordered_map

#define forl(i,a,n) for(int i=a;i<n;i++)

#define rfor(i,n,a) for(int i=n;i>=a;i--)

#define fore(i,a,n) for(int i=a;i<=n;i++)

#define len length()

#define N 200000 

int dp[N];

int func(int arr[],int i,int n,int k)

{

	if(i>=n-1) return dp[i]=0;

	if(dp[i]>=0) return dp[i];

	int t=1;

	int min=INT_MAX;

	while(t<=k )

	{

		int x=abs(arr[i+t]-arr[i])+func(arr,i+t,n,k);

		if(x<min)min=x;

		t++;

	}

	return dp[i]=min;

}

int main()

{

	int n,k;

	cin>>n>>k;

	memset(dp,-1,sizeof(dp));

	int arr[n+k];

	forl(i,0,n) cin>>arr[i];

	forl(i,n,n+k) arr[i]=INT_MAX;

	cout<<func(arr,0,n,k)<<endl;

	

	

}

// Faster C++ solution for the same problem (measured speedup: 1.8026173159801506x):
#include <bits/stdc++.h>

using namespace std;

#define ll long long

#define pb push_back

#define mp make_pair

#define fi first

#define se second

#define si size()

#define bk back()

#define popb pop_back()

#define fastio ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL)

#define endl "\n"

#define lb lower_bound

#define ub upper_bound

#define emp empty()

#define beg begin()

#define en end()

#define ump unordered_map

#define forl(i,a,n) for(int i=a;i<n;i++)

#define rfor(i,n,a) for(int i=n;i>=a;i--)

#define fore(i,a,n) for(int i=a;i<=n;i++)

#define len length()

#define N 200000

int main()

{

	int n,k;

	cin>>n>>k;

	int arr[n];

	forl(i,0,n) cin>>arr[i];

	int dp[n];

	dp[0]=0;

	forl(i,1,n) dp[i]=INT_MAX;

	forl(i,1,n)

	{

		for(int j=i-1;j>=i-k;j--)

		{

			if(j>=0)

			dp[i]=min(dp[i],dp[j]+abs(arr[j]-arr[i]));

		}

	}

	cout<<dp[n-1]<<endl;

	

	

}
// Slower C++ solution:
#include<bits/stdc++.h>

using namespace std;



int main()

{

	int k;cin>>k;

	string a;cin>>a;

	if(a.length()<=k)cout<<a<<endl;

	else{

		for(int i=0;i<k;i++)

		cout<<a[i];

		cout<<"..."<<endl;

	}

	return 0;

}

// Faster C++ solution for the same problem (measured speedup: 5.25072925533829x):
#include<stdio.h>

#include<string.h>

int main(){

	int k;

	char s[105];

	scanf("%d",&k);

	getchar();

	scanf("%s",&s);

	if(strlen(s)>k){

		for(int i=0;i<k;i++){

			printf("%c",s[i]);

		}

		printf("...");

		printf("\n");

	}

	else{

		int l = strlen(s);

		for(int i=0;i<l;i++)

			printf("%c",s[i]);

		printf("\n");

	}

}
// Slower C++ solution:
#include<iostream>

#include<cstdio>

#include<iomanip>

#include<algorithm>

#include<vector>

#include<set>

#include<list> 

#include<string>

#include<cstring>

#include<cmath>



#define vint vector<int>

#define vstr vector<string>

#define vll vector<long long>

#define ll long long

#define pf printf

#define sf scanf

#define sfd(n) scanf("%d", &n)

#define pfd(n) printf("%d", n);

#define pft	printf("\t")

#define pfn printf("\n")

#define pfk printf(" ")

#define N 200010

#define PI 3.1415926



using namespace std;



int main() {

	int n;

	sfd(n);

	ll* num = new ll[n];

	for( int i=0; i<n; i++ ) {

		sf("%lld", &num[i]);

	}

	ll cnt = 0;

	while( 1 ) {

		ll max = num[0];

		int pos = 0; 

		for( int i=0; i<n; i++ ) {

			if( max<num[i] ) {

				max = num[i];

				pos = i;

			}

		}

		if( num[pos]<n ) {

			break;

		}

		for( int i=0; i<n; i++ ) {

			if( i==pos ) {

				num[i] = max%n;

			}else {

				num[i] += max/n;

			}

		}

		cnt += max/n;

	}

	pf("%lld", cnt);

	

	return 0;

}


// Faster C++ solution for the same problem (measured speedup: 4.6886266446468206x):
/*

    数据量很小,直接模拟

    对最大的数-n,其他数+1,直到最大数都小于n

    问操作次数.

    数据量大时,二分也应该可以.

*/

#include<cstdio>

using namespace std;

typedef long long LL;

LL a[55];

int main(){

    LL n, ans = 0;

    scanf("%lld", &n);

    for (int i = 0; i < n;i++){

        scanf("%lld", a + i);

    }

    while(true){

        LL ma = a[0], pos = 0;

        for (int i = 1; i < n;i++){

            if(a[i]>ma){

                ma = a[i];

                pos = i;

            }

        }

        if(ma<n){

            break;

        }

        LL tmp = ma / n;

        a[pos] %= n;

        for (int i = 0; i < n;i++){

            a[i] += tmp;

        }

        a[pos] -= tmp;

        ans += tmp;

    }

    printf("%lld", ans);

    return 0;

}
// Slower C++ solution:
#include <bits/stdc++.h>

#define F first

#define S second

#define T second.first

#define C second.second

#define FIO ios_base::sync_with_stdio(0);cin.tie(0);

using namespace std;

typedef long long ll;

typedef pair<int, int> pii;

typedef pair<int,pair<int,int>> piii;



const int N= 1e5+5, L = 14, OO = 0x3f3f3f3f;

const ll M=10 ;

const double ESP = 1e-8;



int n, k;

int A[N], dp[N];

int main(){

    FIO

//    freopen("input.txt", "r", stdin);

//    freopen("output.txt", "w", stdout);

    cin>>n>>k;

    for(int i=0; i<n; i++)cin>>A[i];

    memset(dp, OO, sizeof dp);

    dp[0] = 0;

    for(int i=0; i<n; i++){

        for(int j=1; j<=k; j++){

            if(i+j<n){

                dp[i+j] = min(dp[i+j], dp[i] + abs(A[i]- A[i+j]));

            }

        }

    }

    cout<<dp[n-1];



}


// Faster C++ solution for the same problem (measured speedup: 1.3910653193315954x):
#define _CRT_SECURE_NO_WARNINGS

#include<bits/stdc++.h>

#include<unordered_map>

using namespace std;

//<DEFINE>

void Fast() {

	ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);

}

void File() {

#ifndef ONLINE_JUDGE

	freopen("Input.txt", "r", stdin);

	freopen("Output.txt", "w", stdout);

#endif

}

#define ll long long

#define ull unsigned long long

#define mod 1000000007

ll gcd(ll a, ll b) {

	return!b ? a : gcd(b, a % b);

}

#define fix(n) cout << fixed << setprecision(n);

#define numOfOnes(n) __builtin_popcount(n)

#define all(a) a.begin(), a.end()

#define skip continue

#define stop(n) return cout<<n,0

#define pi  acos(-1)

#define endl "\n"

#define ceil(x,y) (x+y-1)/y

//</DEFINE>



/*█▀▀▄░░░░░░░░░░░▄▀▀█

░█░░░▀▄░▄▄▄▄▄░▄▀░░░█

░░▀▄░░░▀░░░░░▀░░░▄▀

░░░░▌░▄▄░░░▄▄░▐▀▀

░░░▐░░█▄░░░▄█░░▌▄▄▀▀▀▀█

░░░▌▄▄▀▀░▄░▀▀▄▄▐░░░░░░█

▄▀▀▐▀▀░▄▄▄▄▄░▀▀▌▄▄▄░░░█

█░░░▀▄░█░░░█░▄▀░░░░█▀▀▀

░▀▄░░▀░░▀▀▀░░▀░░░▄█▀

░░░█░░░░░░░░░░░▄▀▄░▀▄

░░░█░░░░░░░░░▄▀█░░█░░█

░░░█░░░░░░░░░░░█▄█░░▄▀

░░░█░░░░░░░░░░░████▀

░░░▀█▄▄▄▀▀▀▀▄▄▄█▀*/

int n,k;

vector<int>v;

ll res[100001];

//<FUNCTIONS>

ll solve(int idx = 0) {

	if (idx == n - 1)return 0;

	if (~(res[idx]))return res[idx];

	ll mn = INT_MAX;

	for (int i = 1; i <= k; i++) {

		if (idx + i >= n)break;

		mn = min(mn, solve(idx + i) + abs(v[idx] - v[idx + i]));

	}

	return res[idx] = mn;

}

//</FUNCTIONS>		

int main() {

	Fast();

	cin >> n >> k;

	for (int i = 0; i <= n; i++)

		res[i] = -1;

	v.resize(n);

	for (int i = 0; i < n; i++)cin >> v[i];

	cout << solve() << endl;

}
// Slower C++ solution:
// 14/06/19 // 8:03 PM //



#include <iostream>

#include <cmath>

#include <cstring>



using  namespace std;



#define  MAXN 100010

#define INF 2000000000



int n, k;

int h[MAXN];



int memo[MAXN];



int funcao(int i){

    if(memo[i]>=0) return memo[i];



    if(i>=n) return memo[i] = 0;



    memo[i] = INF;



    for(int j=1; j<=k; j++){

        if( (i+j)<=n )

            memo[i] = min(memo[i], abs(h[i]-h[i+j]) + funcao(i+j));

        else

            break;

    }



    return memo[i];

}



int main(){



    ios_base::sync_with_stdio(false);

    cin.tie(NULL);



    memset(memo, -1, sizeof(memo));



    cin >> n >> k;



    for(int i=1; i<=n; i++)

        cin >> h[i];



    cout << funcao(1) << "\n";



    return 0;

}


// Faster C++ solution for the same problem (measured speedup: 1.3913720900475721x):
// 14/06/19 // 8:03 PM //



#include <iostream>

#include <cmath>



using  namespace std;



#define  MAXN 100010

#define INF 2000000000



int n, k;

int h[MAXN];



int memo[MAXN];



int funcao(){





    for(int i=2; i<=n; i++){



        memo[i] = INF;



        for(int j=i-1; j>=i-k; j--){

            if(j>=1)

                memo[i] = min(memo[i], abs(h[i]-h[j]) + memo[j]);

            else

                break;

        }

    }



    return memo[n];

}



int main(){



    ios_base::sync_with_stdio(false);

    cin.tie(NULL);



    cin >> n >> k;



    for(int i=1; i<=n; i++)

        cin >> h[i];



    cout << funcao() << "\n";



    return 0;

}
// Slower C++ solution:
#include <bits/stdc++.h>

using namespace std;

//-------------------------------------------MACROS---------------------------------------------

#define 	TIEMPO_CORRIDA		cerr << "tiempo: "<<1.0*clock()/CLOCKS_PER_SEC <<" seg.\n";

#define 	FILES				freopen("input.txt","r",stdin),freopen("output.txt","w",stdout);

#define 	FORIT(it,e)			for(it = e.begin() ; it != e.end() ; it++)

#define 	FOR(i,a,b)			for(i = a ; i < b ; i++)

#define 	RFOR(i,a,b)			for(i = a ; i >= b ;i--)

#define 	FORSW(i,a,b,sw)		for(i = a ; i < b && sw ; i++)

#define 	RFORSW(i,a,b,sw)	for(i = a ; i >= b && sw ; i--)

#define 	fast 				ios::sync_with_stdio(false),cin.tie(),cout.tie();

#define 	to_int(x) 			atoi(x.data())

#define 	to_long(x) 			atoll(x.data())

#define 	ALL(x) 				x.begin(),x.end()

#define 	RALL(x) 			x.rbegin(),x.rend()

typedef 	long long 			ll;

typedef 	pair<int,int> 		pi;

typedef 	pair<ll,ll> 		pl;

const		int 				modN = 1e9+7;

const		double 				PI = 3.1415926535897932384626433832795028841;

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

const int tam = 6e5;

const int oo=1e9;

int ar[tam],memo[tam];

int n,k;



int f(int x){

	if (memo[x]!=-1) return memo[x];

	if(x == n-1) return memo[x]=0;

	int ans=oo, i;

	FOR(i,1,k+1)

		if(x+i<n)

			ans = min(ans,f(x+i) + abs(ar[x+i]-ar[x]));

	return memo[x]=ans;

}



int main(){

	int i;

	while(scanf("%d%d",&n,&k)==2){

		memset(memo,-1,sizeof memo);

		FOR(i,0,n) scanf("%d",&ar[i]);

		printf("%d\n",f(0));

	}

	return 0;

}

// Faster C++ solution for the same problem (measured speedup: 3.806033696621216x):
#include <bits/stdc++.h>

#define ll long long int

ll n,k;

using namespace std;

int main() {			

cin>>n>>k;

int arr[n];

int memo[n];

for(int i=0;i<n;i++)

cin>>arr[i];

for(int i=0;i<n;i++)

memo[i]=INT_MAX;

memo[0]=0;

//memo[1]=abs(arr[0]-arr[1]);//Queda rebundante. Sigue siendo 10000

for(int i=0;i<n;i++)

for(int j=1;j<=k&&j+i<n;j++)

memo[j+i]=min(memo[j+i],memo[i]+abs(arr[i]-arr[j+i]));

cout << memo[n-1] << endl;

return 0;

}
// Slower C++ solution:
#include <bits/stdc++.h>



using namespace std;



#define min3(a, b, c)           min((a), min((b), (c)))

#define max3(a, b, c)           max((a), max((b), (c)))

#define lcm(a,b)                (a / __gcd(a,b) ) *b

#define gcd(a,b)                __gcd(a,b)

#define dist(x1,y1,x2,y2)       sqrt((x2-x1)*(x2-x1) + (y2-y1)*(y2-y1))

#define distl(x1,y1,x2,y2)      sqrtl((x2-x1)*(x2-x1) + (y2-y1)*(y2-y1))

#define sqr(a)                  ((a)*(a))

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

#define fRead(x)                freopen(x,"r",stdin)

#define fWrite(x)               freopen (x,"w",stdout)

#define pb                      push_back

#define mk                      make_pair

#define ff                      first

#define ss                      second

#define lb                      lower_bound

#define ub                      upper_bound

#define all(a)                  a.begin(),a.end()

#define FASTIO                  ios_base::sync_with_stdio(0);cin.tie(NULL);



int main()

{

    FASTIO



    int  n, m;

    cin >> n >> m;



    bool AC[100050];

    memset(AC, false, sizeof AC);



    map<int, int> WA;



    FOR (0, m)

    {

        int submission;

        string verdict;



        cin >> submission;

        cin >> verdict;



        if (verdict == "AC")

            AC[submission] = true;

        if (verdict == "WA")

        {

            if (!AC[submission])

                WA[submission]++;

        }

    }



    int ACCOUNT=0, WACOUNT =0;



    for (auto it: WA)

    {

        if (AC[it.first])

        {

            ACCOUNT++;

            WACOUNT += it.second;

            AC[it.first] = false;

        }

    }



    for (int i=0; i<=n; i++)

        if (AC[i])

            ACCOUNT++;

    cout << ACCOUNT << " " << WACOUNT;



}




// Faster C++ solution for the same problem (measured speedup: 1.1147402917514142x):
#include<bits/stdc++.h>

using namespace std;

int main()

{

   int x=0 ,y=0,m,n,i,j,k;

   cin >> n >> m;

   string s;

   map<int,int>mp,mp1;

   for(i = 0 ;i<m ;i++)

   {

       cin >> k >>s;

       if(s == "AC" && mp1[k]==0)

        {

            x++;y+=mp[k];

            mp1[k]=1;

        }

       else

        mp[k]++;

   }

   cout<<x<<" "<<y<<endl;

}
// Slower C++ solution:
#include<bits/stdc++.h>

#define sf1(n) scanf("%d",&n)

#define sf2(n,q) scanf("%d %d",&n,&q)

#define f first

#define ss second

#define pb push_back

#define lli long long

#define mem(ar,false) memset(ar,false,sizeof(ar))

#define mpr make_pair

#define endl "\n"

#define mod 1000000007

#define inf 9000000000000000000LL

using namespace std;



int ar[200005];

int n,k;

int dp[100005];

int func(int i)

{



    if(i==n-1)

        return 0;

    if(dp[i]!=-1)

        return dp[i];

    int ret = INT_MAX;



    for(int j=1;j<=k;j++)

    {

        if(i+j<n)

            ret = min(ret,abs(ar[i]-ar[i+j])+func(i+j));

    }

    return dp[i] = ret;

}



int main()

{

    ios_base::sync_with_stdio(false);

    cin.tie(NULL);

    mem(dp,-1);

    cin >> n >> k;

    for(int i=0; i<n; i++)

        cin >> ar[i];



     int ans = func(0);

  /*  for(int i=1; i<n; i++)

    {

        int ret1 = dp[i-1]+abs(ar[i]-ar[i-1]);

        int ret2 = INT_MAX;

        if(i-2>=0)

            ret2 = dp[i-2]+abs(ar[i]-ar[i-2]);

        dp[i] = min(ret1,ret2);

    }*/



    cout << ans << endl;

    return 0;



}


// Faster C++ solution for the same problem (measured speedup: 1.3982682536550572x):
#include<bits/stdc++.h>

#define sf1(n) scanf("%d",&n)

#define sf2(n,q) scanf("%d %d",&n,&q)

#define f first

#define ss second

#define pb push_back

#define lli long long

#define mem(ar,false) memset(ar,false,sizeof(ar))

#define mpr make_pair

#define endl "\n"

#define mod 1000000007

#define inf 9000000000000000000LL

using namespace std;



int ar[200005];

int n,k;

int dp[100005];

int func(int i)

{



    if(i==n-1)

        return 0;

    if(dp[i]!=-1)

        return dp[i];

    int ret = INT_MAX;



    for(int j=1;j<=k;j++)

    {

        if(i+j<n)

            ret = min(ret,abs(ar[i]-ar[i+j])+func(i+j));

    }

    return dp[i] = ret;

}



int main()

{

    ios_base::sync_with_stdio(false);

    cin.tie(NULL);

    //mem(dp,-1);

    cin >> n >> k;

    for(int i=0; i<n; i++)

        cin >> ar[i];



  //   int ans = func(0);

    for(int i=1; i<n; i++)

    {

        int ret = INT_MAX;

        for(int j=1;j<=k;j++)

        {

            if(i-j>=0)

                ret = min(ret,dp[i-j]+abs(ar[i]-ar[i-j]));

        }

        dp[i] = ret;

    }



    cout << dp[n-1] << endl;

    return 0;



}
// Slower C++ solution:


#include <iostream>



using namespace std;



int dict[500];

int f[500][500];



int n, m;



void solve(int a, int depth)

{

    if (depth == 3) return;



    dict[a] = 1;



    for(int i = 0; i < n; ++i) if (f[a][i] == 1) solve(i, depth + 1);

}



int main(void)

{

    while (cin >> n >> m, n && m) {

        dict[0] = 1;



        for(int i = 0; i < m; ++i) {

            int a, b; cin >> a >> b;

            a--; b--;

            f[a][b] = f[b][a] = 1;

        }



        solve(0, 0);



        int ans = 0;



        for(int i = 1; i < n; ++i) {

            if (dict[i]) ans++;

        }



        cout << ans << endl;



        for(int i = 0; i < 500; ++i) {

            dict[i] = 0;

            for(int j = 0; j < 500; ++j) f[i][j] = 0;

        }

    }

}

// Faster C++ solution for the same problem (measured speedup: 10.14441256244339x):
#include <cstdio>

#include <cstring>



using namespace std;



int n, m;

int fr[501][501];

int s[501];



void f(int from, int depth)

{

	s[from] = 1;

	if (depth == 3) return;

	for (int i = 1; i <= n; ++i) {

		if (fr[from][i]) f(i, depth + 1);

	}

}





int main()

{

	while (scanf("%d %d", &n, &m), n || m) {

		for (int i = 1; i <= n; ++i) memset(fr[i], 0, sizeof(fr[i]));

		memset(s, 0, sizeof(s));

		for (int i = 0; i < m; ++i) {

			int a, b; scanf("%d %d", &a, &b);

			fr[a][b] = fr[b][a] = 1;

		}

		f(1, 1);

		int cnt = 0;

		for (int i = 1; i <= n; ++i) if (s[i]) cnt++;

		printf("%d\n", cnt - 1);

	}

}
// Slower C++ solution:
#include<bits/stdc++.h>

using namespace std;

typedef long long int ll;

int main()

{

    ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0);

    string s;

    int k;

    cin>>k;

    cin>>s;

    if(s.size()<=k)

        cout<<s<<endl;

    else

    {for(int i=0;i<k;i++)

        cout<<s[i];

        cout<<"..."<<endl;}



    return 0;

}


// Faster C++ solution for the same problem (measured speedup: 5.3093317575479855x):
#include<stdio.h>

#include<string.h>

int main(){

	int k;

	char s[105];

	scanf("%d",&k);

	getchar();

	scanf("%s",&s);

	if(strlen(s)>k){

		for(int i=0;i<k;i++){

			printf("%c",s[i]);

		}

		printf("...");

		printf("\n");

	}

	else{

		int l = strlen(s);

		for(int i=0;i<l;i++)

			printf("%c",s[i]);

		printf("\n");

	}

}
// Slower C++ solution:
#include <cstring>

#include <iostream>

#include <string>

#include <vector>

#include <algorithm>

#include <sstream>

#include <queue>

#include <deque>

#include <bitset>

#include <iterator>

#include <list>

#include <stack>

#include <map>

#include <set>

#include <functional>

#include <numeric>

#include <utility>



#include <cmath>

#include <iomanip>



using namespace std;



int main()

{

  ios::sync_with_stdio(false);

  cin.tie(0);

  int n;

  cin >> n;

  int k;

  cin >> k;

  int h[n];

  for (int i = 0; i < n; i++) {

    int cur;

    cin >> cur;

    h[i] = cur;

  }

  int minCosts[n];

  minCosts[0] = 0;

  minCosts[1] = abs(h[1]-h[0]);

  for (int i = 2; i < n; i++) {

    int cur = abs(h[i]-h[i-1])+minCosts[i-1];

    for (int j = 2; j < min(i, k)+1; j++) {

      cur = min(cur, abs(h[i]-h[i-j])+minCosts[i-j]);

    }

    minCosts[i] = cur;

  }

  cout << minCosts[n-1] << "\n";

  return 0;

}

// Faster C++ solution for the same problem (measured speedup: 5.297291381694043x):
#include <stdio.h>

using namespace std;

#define MAX(a,b) (a>b?a:b)

#define MOD(a) (a>0?a:-(a))

#define custoate(i)

int main(){

  int npedras, maxpulo;

  scanf("%d %d",&npedras,&maxpulo);

  int hpedra[npedras],custoate[npedras];

  for (size_t i = 0; i < npedras; i++)

    scanf("%d", &hpedra[i]);

  custoate[0] = 0;

  int tempcost = 2147483647,k,tempcost2;

  for (size_t i = 1; i < npedras; i++,tempcost = 2147483647) {

    // printf("i = %d, tempcost = %d\n", i,tempcost);

    for (k= (maxpulo>i ? i : maxpulo); k > 0; k--) {

      // printf("k = %d ",k);

      tempcost2 = custoate[i-k] + MOD(hpedra[i-k]-hpedra[i]);

      if (tempcost > tempcost2)

        tempcost = tempcost2;

    }

    // printf("custo[%d] = %d",i,tempcost);

    custoate[i] = tempcost;

    // printf("\n\n");

  }

  printf("%d\n", custoate[npedras-1]);



}
// Slower C++ solution:
#include <iostream>





#define endl '\n'

#define pi acos(-1)

#define euler_e 2.71828

#define pii pair<int,int>

#define pb push_back

#define all(a) a.begin(),a.end()

#define ll long long

#define ull unsigned long long

#define io freopen("input.txt","r",stdin);freopen("output.txt","w",stdout);

#define boost ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);

using namespace std;

void mycode();

const ll int mod=998244353;

ll int ceil(ll int a,ll int b){return (a+b-1)/b;}

ll int min(ll int a, ll int b){if(a>b)return b;else return a;}

bool bit_check(ll int a,int i){

  if((a & (1LL<<i)))return 1;

  return 0;

}

ll int bit_toggle(ll int a,int i){

  return (a^(1LL<<i));

}

ll int bit_sum(ll int a,int i){

  return a+(1LL<<i);

}

ll int bit_sub(ll int a,int i){

  return a-(1LL<<i);

}

ll int mod_power(ll int x,ll int y){//x^y%p

  ll int p=mod;

  ll int res = 1;

  x = x % p;

  while (y > 0) { 

    if (y & 1)res = (res*x) % p;

    y = y>>1;x = (x*x) % p;

  }

  return res;

}

ll int power_of(ll int a,int b){

  if(a==0)return -1;

  return 1+power_of(a/b,b);

}

ll int power(ll int a, ll int b) {

	if(a==1)return 1;

    ll int res = 1;

    while (b > 0) {

        if (b & 1)

            res = res * a;

        a = a * a;

        b >>= 1;

    }

    return res;

}

//__int128

int main(){

	boost;

	//io;

	//auto start = chrono::high_resolution_clock::now();

	mycode();

	//auto end = chrono::high_resolution_clock::now();cerr << "\nExecution time: "<< chrono::duration_cast<chrono::milliseconds>(end - start).count() << " ms" << endl;

	return 0;

}

#include<map>

void mycode(){

	int t,k;

	cin>>t;

	ll int sum=0;

	map<int,int>mp;

	for(int i=0;i<t;i++){cin>>k;mp[k]++;sum+=k;}

	int q,a,b;

	cin>>q;

	while(q--){

		cin>>a>>b;

		sum+=-mp[a]*a+mp[a]*b;

		cout<<sum<<endl;

		mp[b]+=mp[a];

		mp.erase(a);

	}

}

// Faster C++ solution for the same problem (measured speedup: 5.32991075873234x):
#include <cstdio>

#include <cstring>

#include <algorithm>

using namespace std; 

typedef long long LL;

const int MAXN=1E5+10;

int f[MAXN],sum[MAXN],tong[MAXN],a[MAXN];

long long sumans;



int main() {

	int N,K;

	scanf("%d",&N);

	sumans=0;

	for(int i=1;i<=N;++i) {

		scanf("%d",&a[i]);

		sumans+=a[i];

		tong[a[i]]+=1;

	}

	scanf("%d",&K);

	for(int i=1;i<=K;++i) {

		int b,c;scanf("%d%d",&b,&c);

		sumans-=1ll*tong[b]*b;

		sumans+=1ll*tong[b]*c;

		

		tong[c]+=tong[b];

		tong[b]=0;

		

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

	}

	

	return 0;

}
// Slower C++ solution:
#include<cstdio>

#include<cmath>

#include<cstring>

#include<cstdlib>

#include<algorithm>

#include<iostream>

#include<queue>

#include<string>

#include<map>

#include<set>

#include<stack>

#include<vector>

#define ll long long

#define mt(a) memset(a,0,sizeof(a))

#define MAXI 2000000000

#define PAI pair<int,int>



using namespace std;



ll a[200009], dp[200009];

int main()

{

	int n,k;

	scanf("%d%d", &n,&k);

	for (int i = 1; i <= n; i++)

	{

		scanf("%lld", &a[i]);

		dp[i] = MAXI;

	}

	dp[1] = 0;

	for (int i = 1; i <= n; i++)

	{

		for (int j = i + 1; j <= min(n, i + k); j++)

			dp[j] = min(dp[j], dp[i] + abs(a[i] - a[j]));

	}

	printf("%lld\n", dp[n]);





	return 0;

}

// Faster C++ solution for the same problem (measured speedup: 4.546189971597367x):
#include<cstdio>

#include<cmath>

#include<algorithm>

using namespace std;

const int maxn=1e5 + 5;

int main()

{

	int dp[maxn];

	int h[maxn];

	int n,k;

	while(~scanf("%d %d",&n,&k)){

		for(int i=1;i<=n;i++)

			scanf("%d",&h[i]);

		dp[1] = 0;

	    for(int i=2;i<=n;i++){

	    	dp[i]=1e9;

			for(int j=1;j<=k;j++){

				if(i==j) break;

			    dp[i]=min(dp[i],dp[i-j]+abs(h[i-j]-h[i]));

				}

	}

	printf("%d\n",dp[n]);

	}

	return 0;

}