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





using namespace std;

long long n;

long long k;

vector<long long> vec;



long long meme[100007];



long long solve(long long index){



    if(index==n-1){

        return 0;

    }

    

    if(meme[index]!=-1){

        return meme[index];

    }

    

    long long ret = 10000000000000000;

    long long sol = 10000000000000000;

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

        if(index+i<n){

            ret = abs (vec[index]- vec[index+i]) + solve(index+i); 

        }

        sol = min(sol,ret);

    }



    return meme[index] = sol;

}



int main(){

    cin>>n;

    cin>>k;

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

    long long num;

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

        cin>>num;

        vec.push_back(num);

    }

    cout<<solve(0)<<endl;

    return 0;

}

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

using namespace std;

#define fix(f,n) std::fixed<<std::setprecision(n)<<f

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

typedef long long int ll;

typedef unsigned long long int ull;

#define vi vector<int>

#define pii pair<int,int>

#define vii vector<pii>

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

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

#define max3(a,b,c) ((a>b)?(a>c)?a:c:(b>c)?b:c)

#define min3(a,b,c) ((a<b)?(a<c)?a:c:(b<c)?b:c)

#define REP(i,a,n) for(ll i=a;i<n;i++)

#define pb push_back

#define mp make_pair



int main(){

	fast;

	int test=1;

	//cin >> test;

	while(test--){

	    int n,k;

	    cin >> n >> k;

	    ll dp[n+1]={};

	    ll arr[n+1];

	    dp[1]=0;

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

	        cin >> arr[i];

	    }

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

	        ll temp=1000000000000000000;

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

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

	        }

	        dp[i]=temp;

	    }

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

	        ll temp=1000000000000000000;

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

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

	        }

	        dp[i]=temp;

	    }

	    cout << dp[n] << endl;

	}

	return 0;

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

  #define endl "\n"

  #define ll long long int

  #define vi vector<int>

  #define vll vector<ll>

  #define vvi vector < vi >

  #define pii pair<int,int>

  #define pll pair<long long, long long>

  #define mod 1000000007

  #define inf 1000000000000000001;

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

  #define mp(x,y) make_pair(x,y)

  #define mem(a,val) memset(a,val,sizeof(a))

  #define pb push_back

  #define f first

  #define se second

  

  using namespace std;

  ll k2;

  ll arr[1000000];

  ll arr2[1000000];



  ll func(ll n,ll cur){

    if(cur==n)

      return 0;

    if(cur>n)

      return inf;

    if(arr2[cur]!=-1)

      return arr2[cur];



    ll sum=inf;

    for (ll i = 1; i <=k2; ++i)

    {

      sum=min(sum,func(n,cur+i)+abs(arr[cur]-arr[cur+i]));

    }



    return arr2[cur]=sum;

  }





  int main()

  {

    std::ios::sync_with_stdio(false);

    ll n=0,m=0,k=0,x=0,t1=0,t2=0,t3=0,f=0,sum=0,count=0;



    cin>>n>>k2;



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

      cin>>arr[i];

    }



    mem(arr2,-1);



    cout<<func(n-1,0);



    return 0;

  }

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

  #define endl "\n"

  #define ll long long int

  #define vi vector<int>

  #define vll vector<ll>

  #define vvi vector < vi >

  #define pii pair<int,int>

  #define pll pair<long long, long long>

  #define mod 1000000007

  #define inf 1000000000000000001;

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

  #define mp(x,y) make_pair(x,y)

  #define mem(a,val) memset(a,val,sizeof(a))

  #define pb push_back

  #define f first

  #define se second

  

  using namespace std;

  int main()

  {

    std::ios::sync_with_stdio(false);

    ll n=0,m=0,k=0,x=0,t1=0,t2=0,t3=0,f=0,sum=0,count=0;



    cin>>n>>k;

    ll arr[n+1],dp[n+1];

    arr[0]=inf; dp[0]=0; 

   

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

      cin>>arr[i];

    }

    dp[1]=0;



    for (ll i = 2; i <= n; ++i)

    {

      dp[i]=INT_MAX;

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

      {

        if(i-j>=1)

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

      }

    }



    cout<<dp[n]<<endl;



    return 0;

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



using namespace std;



int main()

{

    long int K;

    cin >> K;

    string palabra;

    cin >> palabra;

    int lon= palabra.size();

    if(lon>K){

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

            cout<<palabra[i];

        }

        cout<<"...";



    }

    else{

        cout << palabra;

    }

    return 0;

}

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



int main(){

	int k, len = 0;

	char str[100];

	scanf("%d %s", &k, str);

	for(int i = 0; str[i] != '\0'; i++){

		len++;

	}

	if(len <= k){

		printf("%s\n", str);

	}

	else{

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

			if(i < k + 3){

				str[i] = '.';

			}

			else{

				str[i] = '\0';

			}

		}

		printf("%s\n", str);

	}

	return 0;

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

using namespace std;



int n;

int k;

vector<int> arr(100004, 0);

vector<int> memo(100004, -1);



int cost(int i) {

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

	if(i==n-2) return abs(arr[n-2]-arr[n-1]);



	if(memo[i]==-1) 

	{

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

		{

			if(i+j>n-1) break;

			int temp = abs(arr[i+j]-arr[i]) + cost(i+j);

			memo[i] = (memo[i]==-1) ? temp : min(memo[i], temp);

		}

	}

	return memo[i]; 

}





int main() {



	cin >> n >> k;

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



	cout << cost(0) << endl;

	return 0;

}

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

using namespace std;



int main() {

    int n, k;

    cin >> n >> k;



    vector<int> heights(n, 0);

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



    vector<int> dp(n, 0);

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

    {

        dp[i] = dp[i-1] + abs(heights[i]-heights[i-1]);

        for(int j=1; j<=k && i-j>=0; ++j)

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

    }

    

    cout << dp[n-1] << '\n';



    return 0;

}
// Slower C++ solution:
#define _CRT_SECURE_NO_WARNINGS

#include<bits/stdc++.h>

#include<iostream>

#include<algorithm>

#include<iomanip>

#include<string>

#include<cmath>

#include<vector>

#include<set>

#include<queue>

#include<stack>

#include<fstream>

#include<map>

#include<bitset>

#include<unordered_map>

#include<cstring>

#include<cctype>

using namespace std;

#define  ll long long

#define  ull unsigned long long

#define  ul unsigned long

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

#define sz(n) (int)n.size()

#define pb(s) push_back(s);

#define format(n) fixed<<setprecision(n)

#define finl "\n"

#define Mohammed_Atef_Hassan fast();

#define mod 1000000007

#define INF LLONG_MAX

#define PI acos(-1)

#define clr(dp,n) memset(dp,n,sizeof dp)

#define ex(n) return cout<<n<<finl,0;

int dx[] = { 0, 1, 0, -1, 1, 1, -1, -1 };

int dy[] = { 1, 0, -1, 0, 1, -1, 1, -1 };

ll fix_mod(ll x, ll y)

{

	return (y + x % y) % y;

}

void fast() {

	std::ios_base::sync_with_stdio(false);

	cin.tie(nullptr); cout.tie(nullptr);





}

int n,c;

vector<ll>v;

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

ll solve(int i)

{

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

	ll& ret = mem[i];

	if (~ret)return ret;

	ret = solve(i + 1) + abs(v[i + 1] - v[i]);

	for (int k = 2; k <= c; k++)

		if (k + i < n)ret = min(ret, solve(i + k) + abs(v[i + k] - v[i]));

	return ret;

}

int main()

{

	Mohammed_Atef_Hassan

		clr(mem, -1);

		cin >> n>>c;

	v = vector<ll>(n);

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

	ex(solve(0));

}

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

#define ull unsigned long long

#define ll long long

#define pb push_back

#define endl '\n'

#define line cout<<"----------------------"<<endl

#define mp make_pair

#define mod 1000000007

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

#define filein freopen("input.txt","r",stdin)

#define fileout freopen("output.txt","w",stdout)

using namespace std;

const ll  mx=300009;

ll  ara[mx];

ll  cost[mx];

int main()

{

    ll n,k;

    cin>>n>>k;

    for(ll  i=1;i<=n;i++) cin>>ara[i];

    cost[1]=0;

    cost[2]=abs(ara[2]-ara[1]);

    for(ll  i=3;i<=n;i++)

    {

        cost[i]=LLONG_MAX;

        for(ll  j=1;j<=min(i-1,k);j++)

        {

            cost[i]=min(cost[i],cost[i-j]+abs(ara[i]-ara[i-j]));

        }

    }

    cout<<cost[n]<<endl;





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

#include <cstdio>

#include <algorithm>

#include <string>

#include <cstring>



using namespace std;

int main() {

       int a, b;

cin>>a>>b;

if(a+b>=10)

cout<<"error"<<endl;

else

cout<<a+b<<endl;





	system("pause");

	return 0;

}

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

int main(){

	int a,b;

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

	if(a+b<10){printf("%d\n",a+b);}

	else {printf("error\n");}

	return 0;

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

using namespace std;

int main()

{

	char a[10];

	int c[100005]={0},d[100005]={0};

	int m,n,b,x=0,y=0,z=0,i;

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

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

	{

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

		if(d[b]==-1)

		continue;

		if(a[0]=='A')

		{

			d[b]=-1;

			x++;

			z+=c[b];

			//printf("%d ",z);	

		} 

		if(a[0]=='W')

		{

			c[b]++;

		}

	}

	if(n==0)

	printf("0 0");

	else

	printf("%d %d",x,z);

}

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

using namespace std;

#define ll	    long long

#define vi      vector <int>

#define vii     vector <pair<int,int>>

#define ii      pair<int,int>

#define loop(n)	for(int i=0;i<n;i++)

ll mod=1e9+7;

bool is_prime(int n)

{

    if(n==2)

        return true;

    if(n%2==0||n<2)

        return false;

    for(int i=3; i<=sqrt(n); i+=2)

    {

        if(n%i==0)

            return false;

    }

    return true;

}

long long power(ll n, ll k){

    if(k==0) return 1;

    ll sq=(power(n,k/2))%mod;

    sq=(sq*sq)%mod;

    if(k%2==1)

        sq=(sq*n)%mod;

    return sq%mod;

}

void low(char &c){

c=tolower(c);

}

void primeFactors(int n)

{

    while (n % 2 == 0)

    {

        cout << 2 << " ";

        n = n/2;

    }



    for (int i = 3; i <= sqrt(n); i = i + 2)

    {

        while (n % i == 0)

        {

            cout << i << " ";

            n = n/i;

        }

    }



    if (n > 2)

        cout << n << " ";

}



#include <iostream>

using namespace std;

void fast();

int arr[100005];

map <int,int> M;

int main() {

//fast();

int n,m,ch;

string s;

cin >> n >> m;

int AC=0,WA=0;

loop(m){

    cin >> ch >> s;

    if(arr[ch]!=-1){

       if(s=="WA"){

            arr[ch]++;

        }else{

            AC++;

            WA += arr[ch];

            arr[ch]=-1;

        }

    }



}

cout << AC << " " << WA;

}











void fast(){

std::ios_base::sync_with_stdio(0);

    cin.tie(NULL);

    cout.tie(NULL);



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

using namespace std;

typedef long long ll;

int n,k;

ll sum;

int main()

{

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

    if(k==0)

    {

        printf("%lld\n",(ll)n*n);

        return 0;

    }

    int x=1;

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

    {

        sum+=(ll)n/i*x;

        if(n%i>=k)

            sum+=n%i-k+1;

        x++;

    }

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

}

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

#include<algorithm>

using namespace std;

int N,K;

long long ans=0;

int main() {

	#ifdef LOACL

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

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

	#endif

	scanf("%d %d",&N,&K);

	if(K==0)ans=1ll*N*N;

	else {

		for(int b=K+1;b<=N;b++) {

			int x1=b-K;

			int x2=N/b;

			int k=N-x2*b;

			if(k>=K)ans+=k-K+1;

			ans+=x1*x2;

		}

	}

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

	return 0;

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

#include      <cmath>

#include      <queue>

#include     <string>

#include     <cstdio>

#include     <vector>

#include    <cstring>

#include   <iostream>

#include  <algorithm>

using namespace std;

#define ll long long

const ll inf = 0x3f3f3f3f;

const int maxn = 1e5+5;

const ll mod = 1e9+7;



int main(){

    int k,x;

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

    int t = min(x+k-1,1000000);

    for(int i=max(x-k+1,-1000000);i<=t;i++)

        printf("%d%c",i," \n"[i==t]);

    return 0;

}


// Faster C++ solution for the same problem (measured speedup: 5.1624854473029655x):
////sfef

#include<stdio.h>

int main(){



int k,x,i,p,q,r;

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

p=(x-(k-1));

q=(x+(k-2));

r=(x+(k-1));

if(k==1)

{

    printf("%d",x);

}

else{

    for(i=p;i<=q;i++)    

    {

        printf("%d ",i);

    }

    printf("%d",r);

}



return 0;

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

#include<cstdio>

#include<cmath>

#include<cstring>

using namespace std;

const int N=100005;

int f[N],a[N];

int main()

{

	int n,k;

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

	memset(f,0x3f,sizeof(f));

	for(int i=1;i<=n;i++) scanf("%d",&a[i]);

	f[1]=0;

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

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

			if(i-j>=1) f[i]=min(f[i],f[i-j]+abs(a[i]-a[i-j]));

		}

	}

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

}

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



#define Solved int main()

#define pb push_back

#define mp make_pair

#define F first

#define S second

#define SpeedForce ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0)

#define forn(i, n) for(int i = 1; i <= n; i++)

#define rep(i, x, y) for(int i = x; i <= y; i++)

#define sz(x) (int)x.size()

#define pii pair<int, int>

#define ll long long

#define ull unsigned long long

#define ld long double

#define mii map<int, int>

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

using namespace std;



inline ll bip(ll x,ll n,ll mod){ll res=1;while(n){if(n&1){res=(res*x)%mod;}x=(x*x)%mod;n>>=1;}return res;}



const int maxn = 1e9;

const ll inf = 1e18;

const int mod = 998244353;

//const int mod = 1e9 + 7;

const int N = 1e5 + 10;



int n, k;

ll a[N];

ll dp[N];



void solve() {

    cin >> n >> k;



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

        cin >> a[i];



    dp[2] = abs(a[2] - a[1]);

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

        dp[i] = inf;

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

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

        }

    }

    cout << dp[n];

}



Solved {

    SpeedForce;

	int tt = 1;

    //cin >> tt;

	while (tt--) {

		solve();

		cout << '\n';

	}



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

#include <string>

#include <vector>



using namespace std;



int main(void) {

    string s, t;

    cin >> s;



    for (char c: s) {

        if (c=='0' || c=='1') {

            t.push_back(c);

        } else if (c=='B' && t.size()) {

            t.erase(t.end()-1);

        }

    }

    cout << t << endl;

    return 0;

}


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



using namespace std;



int main() {

    char s[16], t[16]={};

    scanf("%s", s);



    int j=0;

    for (int i=0; s[i]; ++i) {

        switch (s[i]) {

            case 'B':

                if (j)

                    t[--j] = '\0';



                break;

            default:

                t[j++] = s[i];

        }

    }



    printf("%s\n", t);

    return 0;

}
// Slower C++ solution:
// Author : Mohamed Sameh

#include <bits/stdc++.h>

typedef long long ll ;

#define pb push_back

#define f first

#define s second

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

#define rall(v) v.rbegin(),v.rend()

#define SZ(a) (int)a.size()

#define Flush fflush(stdout);

using namespace std ;

const int N = 16;

const ll MN = -1e9*N;

int n;

int a[N][N];

ll dp[(1<<N)];

ll value[(1<<N)];

int arr[N];

ll solve(int msk = (1<<n)-1)

{

  if (!msk)return 0;

  ll &ret = dp[msk];

  if (~ret)return ret;

  ret = MN;

  for (int cur_msk = msk; cur_msk; cur_msk=(cur_msk-1)&msk)

  {

    ret = max(ret, value[cur_msk] + solve(msk^cur_msk));

  }

  return ret;

}

ll get_cost(int sz)

{

  ll ret = 0;

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

    for (int j = i+1; j < sz; j++)

      ret += a[arr[i]][arr[j]];

  return ret;

}

int main()

{

  scanf("%d", &n);

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

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

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

  memset(dp, -1, sizeof dp);

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

  {

    int idx = 0;

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

    {

      if (i & (1<<j))arr[idx++] = j;

    }

    value[i] = get_cost(idx);

  }

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

}


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

#define eps 1e-3

#define pi acos(-1.0)

#define inf 0x3f

#define INF 0x3f3f3f3f

#define pb push_back

#define debug1 cout<<"&&";

#define debug2 cout<<"**";

#define ms(a, x) memset(a, x, sizeof(a))

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

#define for1(i, n) for(int i = 1; i <= n; ++i)

using namespace std;

typedef double db;

typedef long long ll;

typedef unsigned long long ull;

typedef pair<int, int> pii;

typedef pair<long long, int> pli;

typedef pair<long long, long long> pll;

const int mod = 1e9 + 7;

const int N = 50;

const int M = 1e5+10;

/*=================================================================================*/



int n, a[N][N], con[N];

ll ans, dp[M], tmp[M];



int main() {

   scanf("%d", &n);

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



   for0(now, (1<<n)) {

      int cnt = 0;

      for0(i, 16)

         if(now & (1<<i)) {

            con[cnt++] = i;

         }

      ll sum = 0;

      for0(i, cnt) for(int j = i + 1; j < cnt; ++j) {

         sum += a[con[i]][con[j]];

      }

      dp[now] = tmp[now] = sum;

   }



   for0(now, (1<<n)) for(int last = now; last; last = (last-1) & now) {

      dp[now] = max(dp[now], dp[now ^ last] + tmp[last]);

   }



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

   return 0;

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

#include <iostream>

#include <algorithm>



using namespace std;



int N, A, B, rings[100], G[100][100]; bool used[100];



int rec(int pos)

{

	int ret = 1; used[pos] = true;



	for(int i = 0; i < rings[pos]; i++)

	{

		int r = G[pos][i];

		

		if(!used[r])

		{

			ret = max(ret, rec(r) + 1);

		}

	}



	used[pos] = false;



	return ret;

}



int main()

{

	int N, A, B;



	while(true)

	{

		scanf("%d", &N);

 

		if(N == 0) { break; }



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

		{

			used[i] = false;

			rings[i] = 0;

		}



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

		{

			scanf("%d", &A); A--;

			scanf("%d", &B); B--;



			G[A][rings[A]] = B; rings[A]++;

			G[B][rings[B]] = A; rings[B]++;

		}



		int nodes = 0;



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

		{

			if(rings[i])

			{

				nodes++;

			}

		}



		int ret = 0;



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

		{

			ret = max(ret, rec(i));



 			if(ret == nodes) break;

		}

 

		printf("%d\n", ret);

	}



	return 0;

}

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

#include <algorithm>

#pragma warning(disable : 4996)

using namespace std;

int N, A, B, v[100], r[100], G[100][199];

int rec(int pos) {

	int ret = 0; v[pos] = 1;

	for (int i = 0; i < r[pos]; i++) {

		if (!v[G[pos][i]]) ret = max(ret, rec(G[pos][i]));

	}

	v[pos] = 0;

	return ret + 1;

}

int main() {

	while (scanf("%d", &N), N) {

		for (int i = 0; i < 100; i++) r[i] = 0;

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

			scanf("%d%d", &A, &B), A--, B--;

			G[A][r[A]++] = B, G[B][r[B]++] = A;

		}

		int ret = 0;

		for (int i = 0; i < 100; i++) ret = max(ret, rec(i));

		printf("%d\n", ret);

	}

	return 0;

}
// Slower C++ solution:


#include <iostream>

#include <vector>

#include <map>

#include <cmath>

#include <algorithm>

using namespace std;

int main()

{

    int n,m,q;

    cin >> n >> m >> q;

    int a[q], b[q], c[q], d[q];

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

        cin >> a[i] >> b[i] >> c[i] >> d[i];

    }

    

    int cnt = 0;

    std::vector<int> v[100000];

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

    for(int i1=i0; i1<m; i1++){

    for(int i2=i1; i2<m; i2++){

    for(int i3=i2; i3<m; i3++){

    for(int i4=i3; i4<m; i4++){

    for(int i5=i4; i5<m; i5++){

    for(int i6=i5; i6<m; i6++){

    for(int i7=i6; i7<m; i7++){

    for(int i8=i7; i8<m; i8++){

    for(int i9=i8; i9<m; i9++){

        v[cnt] = {i0, i1, i2, i3, i4, i5, i6, i7, i8, i9}; 

        cnt++;

    }}}}}}}}}}

    

    long long int max = 0;

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

        long long int sum = 0;

        for(int j=0; j<q; j++){

            int aj = a[j]-1;

            int bj = b[j]-1;

            if(v[i][bj]-v[i][aj] == c[j]){

                sum += d[j];

            }

        }

        if(sum > max) max = sum;

    }

            

    cout << max << endl;

}

// Faster C++ solution for the same problem (measured speedup: 3.903208131563506x):
//g++  5.4.0



#include <iostream>

#include <vector>

using namespace std;



int n, m, q;

int a[50],b[50],c[50],d[50];

long long int maxnum = 0;



long long int calc(std::vector<int>& v)

{

        long long int sum = 0;

        for(int j=0; j<q; j++){

            int aj = a[j]-1;

            int bj = b[j]-1;

            if(v[bj]-v[aj] == c[j]){

                sum += d[j];

            }

            //cout << a[j]-1 << "," << b[j]-1 << "," << c[j] << "," << d[j] << endl;

        }

        //cout << sum << endl;

        return sum;

}



void dfs(std::vector<int>& v)

{

    if(v.size() >= n){

        //for(int i=0; i<n; i++) cout << v[i];

        //cout << endl;

        long long int sum = calc(v);

        if(sum > maxnum) maxnum = sum;

        return;

    }

    

    int last = 1;

    if(v.size() > 0) last = v[v.size()-1];

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

        v.push_back(i);

        dfs(v);

        v.pop_back();

    }

}



int main()

{

    cin >> n >> m >> q;

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

        cin >> a[i] >> b[i] >> c[i] >> d[i];

    }   

    std::vector<int> v;

    dfs(v);

    cout << maxnum << endl;

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

using namespace std;

int main()

{

    long long int n,m,i,r,s=0,b,c,j;

    scanf("%lld",&n);

    long long int a[n+10];

    long long int ar[100001]={0};

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

        cin>>a[i];

        s+=a[i];

        ar[a[i]]++;

    }

    cin>>m;

    long long int f[m+10];

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

        cin>>b>>c;

        s+=(c-b)*ar[b];

        ar[c]+=ar[b];

        ar[b]=0;

        cout<<s<<endl;

    }



}


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

using namespace std;

typedef long long ll;

int main()

{

	ll n,q,ans=0,a,b,c;

	cin>>n;

	map<ll,ll>m;

	while (n--)

	{

		cin>>a;

		ans+=a;

		m[a]++;

	}

	cin>>q;

	while (q--)

	{

		cin>>b>>c;

		ans+=m[b]*(c-b);

		m[c]+=m[b];

		m[b]=0;

		cout<<ans<<"\n";

	}

}