#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;

ll a[30];
int main()
{
    //for(int tc = 0; tc<1; tc++)
   // {
        string s;
        int k;
        cin>>s;
        cin>>k;
        ll ans = 0;
        ll mxp = -1;
        for(int x = 0; x<26; x++)
        {
            cin>>a[x], mxp = max(a[x], mxp);
        }
        for(int i = 0; i<s.size(); i++)
        {
            ll p = i+1;
            ll dp = s[i] - 'a' + 1;
           // cout<<dp<<endl; return 0;
            ans+=(p*a[dp]);
        }
        int d= s.size();
        for(ll i = d+1; i<=k+d; i++)
        {
            ans+=(i*mxp);
           // cout<<i<<endl;
            //cout<<ans<<endl;
        }
        cout<<ans<<endl;
   // }
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;

ll a[30];
int main()
{
    //for(int tc = 0; tc<1; tc++)
   // {
        string s;
        int k;
        cin>>s;
        cin>>k;
        ll ans = 0;
        ll mxp = -1;
        for(int x = 0; x<26; x++)
        {
            cin>>a[x], mxp = max(a[x], mxp);
        }
        for(int i = 0; i<s.size(); i++)
        {
            ll p = i+1;
            ll dp = s[i] - 'a' + 1;
          //  cout<<dp<<endl;
            ans+=(p*a[dp-1]);
        }
        //return 0;
       // cout<<ans<<endl;
        int d= s.size();
        for(ll i = d+1; i<=k+d; i++)
        {
            ans+=(i*mxp);
        }
        cout<<ans<<endl;
   // }
    return 0;
}


#include <string>
#include <algorithm>
#include <iostream>
int number, aray[26], sum = 0;
string str;
using namespace std;
int main() {
	cin >> str >> number;
	for (int c = 0; c < 26; c++)
		cin >> aray[c];
	for (int c = 1; c <= str.size(); c++)
		sum += c * aray[(int)str[c - 1] - 97];
	cout << sum + (str.size() + str.size() + number + 1) * number / 2 * (*max_element(aray, aray + 26));
}


#include <algorithm>
#include <iostream>
int number, aray[26], sum = 0;
string str;
using namespace std;
int main() {
	ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
	cin >> str >> number;
	for (int c = 0; c < 26; c++)
		cin >> aray[c];
	for (int c = 1; c <= str.size(); c++)
		sum += c * aray[(int)str[c - 1] - 97];
	cout << sum + (str.size() + str.size() + number + 1) * number / 2 * (*max_element(aray, aray + 26));
}


#include <algorithm>
#include <iostream>
using namespace std;
int main() {
	int number, aray[26], sum = 0;
	string str;
	cin >> str >> number;
	for (int c = 0; c < 26; c++)
		cin >> aray[c];
	for (int c = 1; c <= str.size(); c++)
		sum += c * aray[(int)str[c - 1] - 97];
	cout << sum + (str.size() + str.size() + number + 1) * number / 2 * (*max_element(aray, aray + 26));
}


#include <algorithm>
#include <iostream>
using namespace std;
int main() {
	ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
	int number, aray[26], sum = 0;
	string str;
	cin >> str >> number;
	for (int c = 0; c < 26; c++)
		cin >> aray[c];
	for (int c = 1; c <= str.size(); c++)
		sum += c * aray[(int)str[c - 1] - 97];
	cout << sum + (str.size() + str.size() + number + 1) * number / 2 * (*max_element(aray, aray + 26));
}


#include <bits/stdc++.h>
using namespace std;

int main() {
  string s;
  int k, sum = 0;
  vector<int> v(26);

  cin >> s >> k;
  for(int i = 0; i < 26; i++) {
    cin >> v[i];
  }

  int max_v = 0;
  for(int i = 0; i < v.size(); i++) {
    if(v[i] > max_v) max_v = v[i];
  }

  for(int i = 0; i < s.size(); i++) {
    sum += v[s[i] - 'a'] * (i+1);
  }

  for(int i = s.size(); i < s.size() + k; i++) {
    sum += (i+1) * max_v;
  }

  cout << sum << endl;

  return 0;
}


#include <bits/stdc++.h>
using namespace std;

int main() {
  string s;
  int k, sum = 0;
  vector<int> v(26);

  cin >> s >> k;
  for(int i = 0; i < 26; i++) {
    cin >> v[i];
  }

  int max_i = 0;
  for(int i = 0; i < v.size(); i++) {
    if(v[i] > max_i) max_i = i;
  }

  for(int i = 0; i < s.size(); i++) {
    sum += v[s[i] - 'a'] * (i+1);
  }

  for(int i = s.size(); i < s.size() + k; i++) {
    sum += (i+1) * v[max_i];
  }

  cout << sum << endl;

  return 0;
}


#include <bits/stdc++.h>
using namespace std;

int main() {
  string s;
  int k, sum = 0;
  vector<int> v(26);

  cin >> s >> k;
  for(int i = 0; i < 26; i++) {
    cin >> v[i];
  }

  int max_v = 0;
  for(int i = 0; i < v.size(); i++) {
    if(v[i] > max_v) max_v = v[i];
  }

  for(int i = 0; i < s.size(); i++) {
    sum += v[s[i] - 'a'] * (i+1);
  }

  for(int i = s.size(); i < s.size() + k; i++) {
    sum += (i+1) * max_v;
    cout << sum<< endl;
  }

  cout << sum << endl;

  return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main(){
    string s;cin>>s;int z=s.length(),sum=0,q,m=0,r=0;

    int k;cin>>k;int ar[z],br[z];
    int w[26];for(int i=0;i<26;i++){cin>>w[i];m=max(m,w[i]);}
    for(int i=0;i<=z;i++){
        q=(s[i]-'a');
        ar[i]=(i+1)*w[q];
    }
    r=z*k*m+k*(m)*(k+1)/2;
    for(int i=0;i<z;i++){sum=sum+ar[i];}
    cout<<sum+r;
}


#include<bits/stdc++.h>
using namespace std;
int main(){
    string s;cin>>s;int z=s.length(),sum=0,q,r=0,f=0;

    int k;cin>>k;int ar[z],br[z];
    int w[26];for(int i=0;i<26;i++)cin>>w[i];
    for(int i=1;i<=z;i++){
        q=(s[i]-'a');
        ar[i-1]=i*w[q];
    }
    for(int i=0;i<z;i++){q=(s[i]-'a');
    	br[i]=ar[i]/w[q];
	}
	 for(int i=0;i<z;i++){if(br[i]==ar[z-1])f=i;}

    sort(ar,ar+z);sort(w,w+26)
     r=z*ar[z-1]/f+k*(ar[z-1]/f)*(ar[z-1]/f)*(k+1)/2;
    for(int i=0;i<z;i++){sum=sum+ar[i];}
    cout<<sum+r;
}


#include<bits/stdc++.h>
using namespace std;
int main(){
    string s;cin>>s;int z=s.length(),sum=0;

    int k;cin>>k;int ar[z];
    string w;cin>>w;
    for(int i=1;i<=z;i++){
        q=(s[i]-'a');
        ar[i]=i*w[q];
    }
    sort(ar,ar+z);
    int r=n*ar[z-1]+k*ar[z-1]*ar[z-1]*(k+1)/2;
    for(int i=0;i<z;i++){sum=sum+ar[i];}
    cout<<sum+r;
}


#include<bits/stdc++.h>
using namespace std;
int main(){
    string s;cin>>s;int z=s.length(),sum=0,q,m=0,r=0;

    int k;cin>>k;int ar[z],br[z];
    int w[26];for(int i=0;i<26;i++){cin>>w[i];m=max(m,w[i]);}
    for(int i=0;i<=z;i++){
        q=(s[i]-'a');
        ar[i]=(i+1)*w[q];
    }
    r=z*m+k*(m)*(m)*(k+1)/2;
    for(int i=0;i<z;i++){sum=sum+ar[i];}
    cout<<sum+r;
}


#include<bits/stdc++.h>
using namespace std;
int main(){
    string s;cin>>s;int z=s.length(),sum=0,q,r=0,f=0;

    int k;cin>>k;int ar[z],br[z];
    int w[26];for(int i=0;i<26;i++)cin>>w[i];
    for(int i=1;i<=z;i++){
        q=(s[i-1]-'a');
        ar[i-1]=i*w[q];
    }
    for(int i=0;i<z;i++){q=(s[i-1]-'a');
    	br[i]=ar[i]/w[q];
	}
	 for(int i=0;i<z;i++){if(br[i]==ar[z-1])f=i;}

    sort(ar,ar+z);
     r=z*ar[z-1]/f+k*(ar[z-1]/f)*(ar[z-1]/f)*(k+1)/2;
    for(int i=0;i<z;i++){sum=sum+ar[i];}
    cout<<sum+r;
}


#include<bits/stdc++.h>
using namespace std;
int main(){
    int r, x, y, x1 y1;cin>>r>>x>>y>>x1>>y1;
    if(r<=sqrt((x-x1)*(x-x1)+(y-y1)*(y-y1))/2){cout<<1;return 0;}
    else cout<<min(mod(x-x1),mod(y-y1));
}


#include<bits/stdc++.h>
using namespace std;
int main(){
    string s;cin>>s;int z=s.length(),sum=0,q,r=0,f=0;

    int k;cin>>k;int ar[z],br[z];
    int w[26];for(int i=0;i<26;i++)cin>>w[i];
    for(int i=1;i<=z;i++){
        q=(s[i]-'a');
        ar[i-1]=i*w[q];
    }
    for(int i=0;i<z;i++){q=(s[i]-'a');
    	br[i]=ar[i]/w[q];
	}
	 for(int i=0;i<z;i++){if(br[i]==ar[z-1])f=i;}

    sort(ar,ar+z);
     r=z*ar[z-1]/f+k*(ar[z-1]/f)*(ar[z-1]/f)*(k+1)/2;
    for(int i=0;i<z;i++){sum=sum+ar[i];}
    cout<<sum+r;
}


#include<bits/stdc++.h>
using namespace std;
int main(){
    string s;cin>>s;int z=s.length(),sum=0,q;

    int k;cin>>k;int ar[z];
    string w;cin>>w;
    for(int i=1;i<=z;i++){
        q=(s[i]-'a');
        ar[i]=i*w[q];
    }
    sort(ar,ar+z);
    int r=z*ar[z-1]+k*ar[z-1]*ar[z-1]*(k+1)/2;
    for(int i=0;i<z;i++){sum=sum+ar[i];}
    cout<<sum+r;
}


#include<bits/stdc++.h>
using namespace std;
int main(){
    string s;cin>>s;int z=s.length(),sum=0,q,r=0,f=0;

    int k;cin>>k;int ar[z],br[z];
    int w[26];for(int i=0;i<26;i++)cin>>w[i];
    for(int i=1;i<=z;i++){
        q=(s[i]-'a');
        ar[i]=i*w[q];
    }
    for(int i=0;i<z;i++){q=(s[i]-'a');
    	br[i]=ar[i]/w[q];
	}
	 for(int i=0;i<z;i++){if(br[i]==ar[z-1])f=i;}

    sort(ar,ar+z);sort(w,w+26)
     r=z*ar[z-1]/f+k*(ar[z-1]/f)*(ar[z-1]/f)*(k+1)/2;
    for(int i=0;i<z;i++){sum=sum+ar[i];}
    cout<<sum+r;
}


#include<bits/stdc++.h>
using namespace std;
int main(){
    string s;cin>>s;int z=s.length(),sum=0,q,r=0,f=0;

    int k;cin>>k;int ar[z],br[z];
    int w[26];for(int i=0;i<26;i++)cin>>w[i];
    for(int i=1;i<=z;i++){
        q=(s[i]-'a');
        ar[i]=i*w[q];
    }
    for(int i=0;i<z;i++){q=(s[i]-'a');
    	br[i]=ar[i]/w[q];
	}
	 for(int i=0;i<z;i++){if(br[i]==ar[z-1])f=i;}

    sort(ar,ar+z);
     r=z*ar[z-1]/f+k*(ar[z-1]/f)*(ar[z-1]/f)*(k+1)/2;
    for(int i=0;i<z;i++){sum=sum+ar[i];}
    cout<<sum+r;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> ii;
vector<int> v;
 main() {
    string s;
    int k, elm;
    cin >> s;
    cin >> k;
    for (int i = 0; i < 26; ++i) {
        cin >> elm;
        v.push_back(elm);
    }
    int mx = *max_element(v.begin(), v.end());
    ll res = 0;
    for (int i = 0; i < s.length(); ++i) {
        res = res + v[s[i] - 'a'] * (i + 1);
    }
    int j = s.length() + 1;
    while (k--) {
        res = res + j * mx;
        ++j;
    }
    cout << res;

}


#include<bits/stdc++.h>
using namespace std;
main()
{
	int n,ans=0;
	string s;
	cin>>s>>n;
	vector<int> a(26);
	for(int i=0;i<26;i++)
	{
		cin>>a[i];
		ans=max(ans,a[i]);
	}
	unordered_map<char,int> mp;
	for(auto x:s)
	{
		mp[x]++;
	}
	int j=1,sum=0;
	for(auto y:mp)
	{
		sum+=y.second*a[j-1]*j;
		j++;
	}
	for(int k=s.size()+1;k<=s.size()+n;k++)
	{
		sum+=k*ans;
	}
	cout<<sum;
}


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> ii;
vector<int> v;
int main() {
    string s;
    int k, elm;
    cin >> s;
    cin >> k;
    for (int i = 0; i < 26; ++i) {
        cin >> elm;
        v.push_back(elm);
    }
    int mx = *max_element(v.begin(), v.end());
    ll res = 0;
    for (int i = 0; i < s.length(); ++i) {
        res = res + v[s[i] - 'a'] * (i + 1);
    }
    int j = s.length() + 1;
    while (k--) {
        res = res + j * mx;
        ++j;
    }
    cout << res;
    return 0;
}


#include<bits/stdc++.h>
main() {
    string s;
    int k, elm;
    cin >> s;
    cin >> k;
    for (int i = 0; i < 26; ++i) {
        cin >> elm;
        v.push_back(elm);
    }
    int mx = *max_element(v.begin(), v.end());
    ll res = 0;
    for (int i = 0; i < s.length(); ++i) {
        res = res + v[s[i] - 'a'] * (i + 1);
    }
    int j = s.length() + 1;
    while (k--) {
        res = res + j * mx;
        ++j;
    }
    cout << res;}


#include <bits/stdc++.h>
using namespace std;
#define ll long long
int main()
{
    string s;
    cin>>s;
    int k;
    cin>>k;
    int w[26];
    int mx=0;
    for(int i=0;i<25;i++){
        cin>>w[i];
        mx = max(w[i],mx);
        }
    int ans=0;
    for(int i=0,x;i<s.size();i++)
    {
        x = s[i] - 'a';
        ans+= (w[x] * (i+1));
    }
    ans+=(mx * (s.size() + 1) );
    ans+=(mx * (s.size() + 2) );
    ans+=(mx * (s.size() + 3) );
    cout<<ans<<endl;
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define ll long long
int main()
{
    string s;
    cin>>s;
    int k;
    cin>>k;
    int w[26];
    int mx=0;
    for(int i=0;i<26;i++){
        cin>>w[i];
        mx = max(w[i],mx);
        }
    ll ans=0;
    for(int i=0,x;i<s.size();i++)
    {
        x = s[i] - 'a';
        ans = ans + (w[x] * (i+1));
    }
    ans= ans + (mx * (s.size() + 1) );
    ans= ans + (mx * (s.size() + 2) );
    ans= ans + (mx * (s.size() + 3) );

    cout<<ans<<endl;
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define ll long long
int main()
{
    string s;
    cin>>s;
    int k;
    cin>>k;
    int w[26];
    int mx=0;
    for(int i=0;i<26;i++){
        cin>>w[i];
        mx = max(w[i],mx);
        }
    ll ans=0;
    for(int i=0,x;i<s.size();i++)
    {
        x = s[i] - 'a';
        ans = ans + (w[x] * (i+1));
    }
    for(int i=1;i<=k;i++)
        ans= ans + (mx * (s.size() + i) );
    cout<<ans<<endl;
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define ll long long int

int main()
{
 long long int t,n,k,i;

      string s;
      cin>>s;
      cin>>k;
      int a[26];
      int mx=INT_MIN;
      for(i=0;i<26;i++)
      {
          cin>>a[i];
          if(a[i]>mx)
          mx=a[i];
      }
      n=s.length();
      ll sum=0;
      for(i=0;i<n;i++)
      {
          sum+=(i+1)*a[i];
      }

      for(i=n+1;i<=k+n;i++)
      {
          sum+=i*mx;
      }
      cout<<sum<<endl;

    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define ll long long int

int main()
{
 long long int t,n,k,i;

      string s;
      cin>>s;
      cin>>k;
      int a[26];
      int mx=INT_MIN;
      for(i=0;i<26;i++)
      {
          cin>>a[i];
          if(a[i]>mx)
          mx=a[i];
      }
      n=s.length();
      ll sum=0;
      for(i=0;i<n;i++)
      {
          sum+=(i+1)*a[(s[i]-'a')];
      }

      for(i=n+1;i<=k+n;i++)
      {
          sum+=i*mx;
      }
      cout<<sum<<endl;

    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define ll long long int

int main()
{
 long long int t,n,k,i;

      string s;
      cin>>s;
      cin>>k;
      int a[26];
      int mx=INT_MIN;
      for(i=0;i<26;i++)
      {
          cin>>a[i];
          if(a[i]>mx)
          mx=a[i];
      }
      n=s.length();
      int sum=0;
      for(i=0;i<n;i++)
      {
          sum+=(i+1)*a[i];
      }

      for(i=n+1;i<=k+n;i++)
      {
          sum+=i*mx;
      }
      cout<<sum<<endl;

    return 0;
}


#include <bits/stdc++.h>
using namespace std;
int compare(int a,int b)
{
    return (a>b);
}
int main()
{
    int a[200]={0};
    string s;
    cin>>s;
    int t;
    cin>>t;
    for(int i=0;i<26;i++)
    {
        cin>>a[i];
    }
    int cnt = 0;
    for(int i=0;i<s.size();i++)
    {
            cnt += a[s[i]-'a']*(i+1);
    }
    sort(a,a+200,compare);
    int g = s.size();
    for(int i = (g+1); i<=(g+t); i++)
    {
        cnt+=(a[0])*(i);
    }

     cout<<cnt<<endl;

    return 0;
}


#include<bits/stdc++.h>
#define _CRT_SECURE_NO_WARNINGS
#define pb push_back
#define Ceil(a,b)              ((a)+(b)-1)/(b)
#define MrGAFs                 file();
#define rep(i,limits)          for(int i = 0;i<(limits);++i)
#define allr(v)                ((v).rbegin()), ((v).rend())
#define all(v)                 ((v).begin()), ((v).end())
#define endl                   "\n"
#define sz(v)	               ((int)((v).size()))
#define dep(x)		       cout << #x << " = " << x << endl ;
#define rt(v)                  return cout<<v,0;
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
const int oo = INT_MAX;
const ll OO = LLONG_MAX;
using namespace std;
int dx[] = { 0, 1, 0, -1, 1, 1, -1, -1 };
int dy[] = { 1, 0, -1, 0, 1, -1, 1, -1 };
int dx1[] = { -1, 1, 0, 0 };
int dy1[] = { 0, 0, -1, 1 };

	//classes
        void file (){
      #ifndef ONLINE_JUDGE
         freopen ("in.txt", "r", stdin);
   //     x = freopen ("out.txt", "w", stdout);
      #else
#endif
  ios_base::sync_with_stdio (false);
  cout.tie (NULL);
  cin.tie (NULL);
}
  /******Functions*********/
ll sumRange(int l,int lon){
return ( ((l * 2) + (lon-1) ) * lon ) /2 ;

}
  /******Main Function*********/
int main (){
  MrGAFs
string str ;
  cin >> str ;
  int n, x  ;
   cin >> n ;
   map <char , int > v ;
    int mx = 0 ;
    for (int i =0 ; i < 26 ; ++ i ){
	    cin >>x ;
	    v[char( i  + 'a' ) ] = x ;
	    mx =  max(mx, x ) ;
    }
    int sum = 0 ;
    for (int  i =0 ; i <  str.size() ; ++ i) sum +=(v[str[i]]*(i+1));
    sum += mx * sumRange(str.size() + 1 ,n ) ;
    cout << sum ;
}


#include<bits/stdc++.h>
#define Ceil(a,b)              ((a)+(b)-1)/(b)
#define MrGAFs                 file();
#define allr(v)                ((v).rbegin()), ((v).rend())
#define all(v)                 ((v).begin()), ((v).end())
#define endl                   "\n"
const int oo = INT_MAX;
const long long  OO = LLONG_MAX;
using namespace std;
int dx[] = { 0, 1, 0, -1, 1, 1, -1, -1 };
int dy[] = { 1, 0, -1, 0, 1, -1, 1, -1 };
int dx1[] = { -1, 1, 0, 0 };
int dy1[] = { 0, 0, -1, 1 };

//functions

void file (){
  ios_base::sync_with_stdio (false);
  cout.tie (NULL);
  cin.tie (NULL);
}

//main function
int main(){
  MrGAFs
    string str ;
    cin >> str ;
    map<int,int>mp;
    for(int i = 0 ; i < str.size();++i)mp[str[i]-'a'] += (i+1);
    long long ans =0;
  int n ,num,mx = 0;
  cin >> n ;
  for(int i = 0 ; i < 26 ; ++ i){
    cin>>num;
    ans+=(num*mp[i]);
    mx=max(mx,num);
  }
  int x = str.size()+1 , y =x + n - 1;
  long long tmp =(x+y) * (n/2.0) ;
  cout << ans  + tmp*mx ;

}


#include<bits/stdc++.h>
#define Ceil(a,b)              ((a)+(b)-1)/(b)
#define MrGAFs                 file();
#define allr(v)                ((v).rbegin()), ((v).rend())
#define all(v)                 ((v).begin()), ((v).end())
#define endl                   "\n"
const int oo = INT_MAX;
const long long  OO = LLONG_MAX;
using namespace std;
int dx[] = { 0, 1, 0, -1, 1, 1, -1, -1 };
int dy[] = { 1, 0, -1, 0, 1, -1, 1, -1 };
int dx1[] = { -1, 1, 0, 0 };
int dy1[] = { 0, 0, -1, 1 };

//functions

void file (){
  ios_base::sync_with_stdio (false);
  cout.tie (NULL);
  cin.tie (NULL);
}

//main function
int main(){
  MrGAFs
    string str ;
    cin >> str ;
    map<int,int>mp;
    for(int i = 0 ; i < str.size();++i)mp[str[i]-'a'] += (i+1);
    long long ans =0;
  int n ,num,mx = 0;
  cin >> n ;
  for(int i = 0 ; i < 26 ; ++ i){
    cin>>num;
    ans+=(num*mp[i]);
    mx=max(mx,num);
  }
  int x = str.size();
  long long tmp = (2*x +1+ n) * (n+1/2) ;
  cout << ans  + tmp ;

}


//    How strange it is :
//        We wish to wear high brands,
//        but we feel most comfortable in pajaymas.
//
//        We wish to sit in Taj & Marriot with elight people,
//        but we enjoy roadside tea with friends and with people we love.
//
//        We wish to own big cars and go on long drives,
//        yet we talk our heart out only while walking down a long road.
//
//    " Don't be serious, Enjoy Life as it Comes ".
//
//    WORDS OF RATAN TATA

#include<bits/stdc++.h>

using namespace std;

#define fast ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
typedef long long ll;
typedef pair<int, int> ii;
typedef vector<ii> vii;
typedef vector<int> vi;
#define INF 1000000000
#define pb push_back
#define mk make_pair

#define flu(i, s, e) for(int i = s; i < e; i++)
#define fld(i, s, e) for(int i = s; i >= e; i--)

int main()
{
    fast;

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

    string s;
    cin >> s;

    int k, ma = 0, count = 0;
    vi v(26);

    cin >> k;

    flu(i, 0, 26)
    {
        cin >> v[i];
        ma = max(ma, v[i]);
    }

    flu(i, 0, s.size())
    {
        count += v[s[i] - 'a'];
    }

    flu(i, s.size() + 1, s.size() + k + 1)
    {
        count += ma*i;
    }

    cout << count << "\n";

    return 0;
}


//    How strange it is :
//        We wish to wear high brands,
//        but we feel most comfortable in pajaymas.
//
//        We wish to sit in Taj & Marriot with elight people,
//        but we enjoy roadside tea with friends and with people we love.
//
//        We wish to own big cars and go on long drives,
//        yet we talk our heart out only while walking down a long road.
//
//    " Don't be serious, Enjoy Life as it Comes ".
//
//    WORDS OF RATAN TATA

#include<bits/stdc++.h>

using namespace std;

#define fast ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
typedef long long ll;
typedef pair<int, int> ii;
typedef vector<ii> vii;
typedef vector<int> vi;
#define INF 1000000000
#define pb push_back
#define mk make_pair

#define flu(i, s, e) for(int i = s; i < e; i++)
#define fld(i, s, e) for(int i = s; i >= e; i--)

int main()
{
    fast;

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

    string s;
    cin >> s;

    int k, ma = 0, count = 0;
    vi v(26);

    cin >> k;

    flu(i, 0, 26)
    {
        cin >> v[i];
        ma = max(ma, v[i]);
    }

    flu(i, 0, s.size())
    {
        count += v[s[i] - 'a'];
    }

    count += k*ma;

    cout << count << "\n";

    return 0;
}


//    How strange it is :
//        We wish to wear high brands,
//        but we feel most comfortable in pajaymas.
//
//        We wish to sit in Taj & Marriot with elight people,
//        but we enjoy roadside tea with friends and with people we love.
//
//        We wish to own big cars and go on long drives,
//        yet we talk our heart out only while walking down a long road.
//
//    " Don't be serious, Enjoy Life as it Comes ".
//
//    WORDS OF RATAN TATA

#include<bits/stdc++.h>

using namespace std;

#define fast ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
typedef long long ll;
typedef pair<int, int> ii;
typedef vector<ii> vii;
typedef vector<int> vi;
#define INF 1000000000
#define pb push_back
#define mk make_pair

#define flu(i, s, e) for(int i = s; i < e; i++)
#define fld(i, s, e) for(int i = s; i >= e; i--)

int main()
{

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

    string s;

    int k, ma = 0, count = 0;
    vi v(26);

    cin >> s >> k;
    int n = s.size();

    flu(i, 0, 26)
    {
        cin >> v[i];
        ma = max(ma, v[i]);
    }

    flu(i, 1, n + 1)
    {
        count += i*v[s[i-1] - 'a'];
    }
    flu(i, n + 1, n + k + 1)
    {
        count += i*ma;
    }

    cout << count << "\n";

    return 0;
}


//    How strange it is :
//        We wish to wear high brands,
//        but we feel most comfortable in pajaymas.
//
//        We wish to sit in Taj & Marriot with elight people,
//        but we enjoy roadside tea with friends and with people we love.
//
//        We wish to own big cars and go on long drives,
//        yet we talk our heart out only while walking down a long road.
//
//    " Don't be serious, Enjoy Life as it Comes ".
//
//    WORDS OF RATAN TATA

#include<bits/stdc++.h>

using namespace std;

#define fast ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
typedef long long ll;
typedef pair<int, int> ii;
typedef vector<ii> vii;
typedef vector<int> vi;
#define INF 1000000000
#define pb push_back
#define mk make_pair

#define flu(i, s, e) for(int i = s; i < e; i++)
#define fld(i, s, e) for(int i = s; i >= e; i--)

int main()
{
    fast;

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

    string s;
    cin >> s;

    int k, ma = 0, count = 0;
    vi v(26);

    cin >> k;

    flu(i, 0, 26)
    {
        cin >> v[i];
        ma = max(ma, v[i]);
    }

    flu(i, 0, s.size())
    {
        count += v[s[i] - 'a'];
    }

    flu(i, n + 1, n + k + 1)
    {
        count += ma*i;
    }

    cout << count << "\n";

    return 0;
}


#include<iostream>
using namespace std;
int main()
{
   string s;
   int k;
   int a[26];
   cin>>s>>k;
   int i;
   for(i=0;i<26;++i)
      cin>>a[i];
   int len=s.length();
   int ans=0;
   for(i=0;i<len;++i)
   {
    ans+= (a[s[i]-'0']*(i+1))
   }
    sort(a,a+26);
    for(i=0;i<k;++i)
     ans+=(a[0]*(len++));
    cout<<ans<<"\n";
}


#include<iostream>
#include<algorithm>
using namespace std;
int main()
{
   string s;
   int k;
   int a[26];
   cin>>s>>k;
   int i;
   for(i=0;i<26;++i)
      cin>>a[i];
   int len=s.length();
   int ans=0;
   for(i=0;i<len;++i)
   {
    ans+= (a[s[i]-'0']*(i+1));
   }
    sort(a,a+26);
    for(i=0;i<k;++i)
     ans+=(a[0]*(len++));

    cout<<ans<<"\n";
    return 0;
}


#include<iostream>
#include<algorithm>
using namespace std;
int main()
{
   string s;
   int k;
   int a[26];
   cin>>s>>k;
   int i;
   for(i=0;i<26;++i)
      cin>>a[i];
   int len=s.length();
   int ans=0;
   for(i=0;i<len;++i)
   {
    ans+= (a[s[i]-'a']*(i+1));
   }
    sort(a,a+26);
    for(i=0;i<k;++i)
     ans+=(a[0]*(len++));

    cout<<ans<<"\n";
    return 0;
}


#include<iostream>
#include<algorithm>
using namespace std;
int main()
{
   string s;
   int k;
   int a[26];
   cin>>s>>k;
   int i;
   int max=-1;
   for(i=0;i<26;++i)
     {
          cin>>a[i];
          if(max<a[i])
           max=a[i];
     }
   int len=s.length();
   int ans=0;
   for(i=0;i<len;++i)
    ans+= (a[s[i]-'a']*(i+1));
   int j=len+1;
    for(i=0;i<k;++i)
     ans+=(max*(j+i));
    cout<<ans<<"\n";
    return 0;
}


#include<iostream>
#include<algorithm>
using namespace std;
int main()
{
   string s;
   int k;
   int a[26];
   cin>>s>>k;
   int i;
   for(i=0;i<26;++i)
      cin>>a[i];
   int len=s.length();
   int ans=0;
   for(i=0;i<len;++i)
   {
    ans+= (a[s[i]-'0']*(i+1))
   }
    sort(a,a+26);
    for(i=0;i<k;++i)
     ans+=(a[0]*(len++));

    cout<<ans<<"\n";
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main(){
	string s;
	int a; // k
	cin>>s>>a; //k
	int ar[26+2];
	for(int i=1;i<=26;i++){
		cin>>ar[i];
	}
	// input procdeure done anove
	int add=0;
	for(int i=0;i<s.size();i++){
		add=add+(ar[s[i]-96]*(i+1));
	}

	int k=*max_element(ar, ar + a);
	for(int i=s.size()+1;i<=s.size()+a;i++){
		add=add+(k*i);
	}

	cout<<add<<endl;
	return 0;

}


#include<bits/stdc++.h>
using namespace std;
int main(){
	string s;
	int a; // k
	cin>>s>>a; //k
	int ar[26];
	for(int i=1;i<=26;i++){
		cin>>ar[i];
	}
	// input procdeure done anove
	int add=0;
	for(int i=0;i<s.size();i++){
		add=add+(ar[s[i]-96]*(i+1));
	}

	int k=*max_element(ar, ar + a);
	for(int i=s.size()+1;i<=s.size()+a;i++){
		add=add+(k*i);
	}

	cout<<add<<endl;
	return 0;

}


#include<bits/stdc++.h>
using namespace std;
int main(){
	string s;
	int a; // k
	cin>>s>>a; //k
	int ar[26];
	for(int i=0;i<26;i++){
		cin>>ar[i];
	}
	// input procdeure done anove
	int add=0;
	for(int i=0;i<s.size();i++){
		add=add+(ar[s[i]-97]*(i+1));
	}

	int k=*max_element(ar , ar + 26);
//	cout<<k<<endl;
	for(int i=s.size()+1;i<=s.size()+a;i++){
		add=add+(k*i);
	}

	cout<<add<<endl;
	return 0;

}


#include<bits/stdc++.h>
using namespace std;
int main(){
	string s;
	int a; // k
	cin>>s>>a; //k
	int ar[26];
	for(int i=0;i<26;i++){
		cin>>ar[i];
	}
	// input procdeure done anove
	int add=0;
	for(int i=0;i<s.size();i++){
		add=add+(ar[s[i]-97]*(i+1));
	}

	int k=*max_element(ar , ar + a);
	for(int i=s.size()+1;i<=s.size()+a;i++){
		add=add+(k*i);
	}

	cout<<add<<endl;
	return 0;

}


#include<bits/stdc++.h>
using namespace std;
int main(){
	string s;
	int a; // k
	cin>>s>>a; //k
	int ar[26+1];
	for(int i=1;i<=26;i++){
		cin>>ar[i];
	}
	// input procdeure done anove
	int add=0;
	for(int i=0;i<s.size();i++){
		add=add+(ar[s[i]-96]*(i+1));
	}

	int k=*max_element(ar, ar + a);
	for(int i=s.size()+1;i<=s.size()+a;i++){
		add=add+(k*i);
	}

	cout<<add<<endl;
	return 0;

}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    string s;
    long long k,i,j,ans=0;
    cin>>s;
    cin>>k;
    long long a[26];
    for(i=0;i<26;i++){
        cin>>a[i];
    }
    for(i=0;i<s.size();i++){
        ans+=(a[i]*(i+1));
    }
    sort(a,a+26,greater<int>());
    j=s.size()+1;
    for(i=0;i<k;i++){
        ans+=j*a[0];
        j++;
    }
    cout<<ans;
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    string s;
    long long k,i,j,ans=0;
    cin>>s;
    cin>>k;
    long long a[26];
    for(i=0;i<26;i++){
        cin>>a[i];
    }
    for(i=0;i<s.size();i++){
        ans+=a[i]*(i+1);
    }
    sort(a,a+26);
    int mx=a[25];
    for(i=0;i<k;i++){
        ans+=(mx*(s.size()+i+1));
    }
    cout<<ans;
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    string s;
    long long k,i,j,ans=0;
    cin>>s;
    cin>>k;
    long long a[26];
    for(i=0;i<26;i++){
        cin>>a[i];
    }
    for(i=0;i<s.size();i++){
        ans+=(a[s[i]]*(i+1));
    }
    sort(a,a+26,greater<int>());
    j=s.size()+1;
    for(i=0;i<k;i++){
        ans+=j*a[0];
        j++;
    }
    cout<<ans;
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    string s;
    long long k,i,j,ans=0;
    cin>>s;
    cin>>k;
    long long a[26];
    for(i=0;i<26;i++){
        cin>>a[i];
    }
    for(i=0;i<s.size();i++){
        ans+=a[s[i]-'a']*(i+1);
    }
    sort(a,a+26);
    int mx=a[25];
    for(i=0;i<k;i++){
        ans+=(mx*(s.size()+i+1));
    }
    cout<<ans;
    return 0;
}


#include<iostream>
#include<string>

#define Kop ios::sync_with_stdio(0)

using namespace std;

int main()
{
    string s;
    cin>>s;
    int n,l,sum=0,k,i;
    cin>>n;
    l=s.size();
    k=l+n;
    for(i=0;i<l;i++){
        if(s[i]=='b' || s[i]=='c'){
            sum+=2*(i+1);
        }
        else
        {
            sum+=(i+1)*1;
        }
    }
    for(i=l+1;i<=k;i++){
        sum+=2*i;
    }
    cout<<sum<<endl;
    return 0;

}


//
// Created by rafee on 6/8/2020.
//

#include<iostream>
#include<string>
#include<vector>
#include<algorithm>

#define Kop ios::sync_with_stdio(0)

using namespace std;

int main()
{
    string s;
    cin>>s;
    int n,l,i,ml;
    cin>>n;
    l=s.size();
    vector<int>v;
    for(i=0;i<26;i++){
        cin>>ml;
        v.push_back(ml);
    }
    int mx = *max_element(v.begin(), v.end());
    int sum=0;
    for(i=0;i<l;i++){
        sum+=v[s[i]-'a']*(i+1);
    }
    int j=l+1;
    while(n--){
        sum+=mx*j;
        ++j;
    }
    cout<<sum<<endl;

    return 0;

}


#include<iostream>
#include<string>

#define Kop ios::sync_with_stdio(0)

using namespace std;

int main()
{
	string s;
	cin>>s;
	int n,l,sum=0,k,i;
	cin>>n;
	l=s.size();
	k=l+n;
	for(i=0;i<l;i++){
		if(s[i]=='a' || s[i]=='b'){
			sum+=2*(i+1);
		}
		else
		{
			sum+=(i+1)*1;
		}
	}
	for(i=l+1;i<=k;i++){
		sum+=2*i;
	}
	cout<<sum<<endl;
	return 0;

}


#include <bits/stdc++.h>
using namespace std;
int main()
{
    vector<int>v;
    string s;
    int a,i,b,c,sum=0;
    cin>>s;
    cin>>c;
    for(i=0; i<26; i++)
    {
        cin>>b;
        v.push_back(b);
    }
    int large=*max_element(v.begin(),v.end());
    int l=s.size();
    for(i=0; i<l; i++)
    {
        int x=s[i];
        sum=sum+((i+1)*(v[x-97]));
    }
    int n=l+c;
   // cout<<n<<endl;
    int t=l+1;
   // cout<<t<<endl;
    int S=(n-t)+1;
    int X=((n+t)/2)*S;
    sum=sum+X*large;
   // cout<<S<<endl;
    cout<<sum;

    return 0;
}


#include <bits/stdc++.h>
using namespace std;
int main()
{
    vector<int>v;
    string s;
    int a,i,b,c,sum=0;
    cin>>s;
    cin>>c;
    for(i=0; i<26; i++)
    {
        cin>>b;
        v.push_back(b);
    }
    int large=*max_element(v.begin(),v.end());
    int l=s.size();
    //cout<<l<<endl;
    for(i=0; i<l; i++)
    {
        int x=s[i];
        sum=sum+((i+1)*(v[x-97]));
    }

    int x=l+c;
    int sum1=(x*(x+1)/2);
    int y=l;
    int sum2=(y*(y+1)/2);
    int sum3=sum1-sum2;
    //cout<<sum3<<endl;
    sum=sum+sum3*large;
    cout<<sum;

    return 0;
}


#include <bits/stdc++.h>
using namespace std;
int main()
{
    vector<int>v;
    string s;
    int a,i,b,c,sum=0,t=0;
    cin>>s;
    cin>>c;
    for(i=0; i<26; i++)
    {
        cin>>b;
        v.push_back(b);
    }
    int large=*max_element(v.begin(),v.end());
    int l=s.size();
    for(i=0; i<l-1; i++)
    {
        int x=s[i];
        sum=sum+((i+1)*(v[x-97]));
    }
    for(i=l;; i++)
    {
        sum=sum+(large*i);
        t++;
        if(t==c)
        {
            break;
        }
    }
    int x=s[l-1];
    sum=sum+((l+c)*v[x-97]);
    cout<<sum;

    return 0;
}


#include <bits/stdc++.h>
using namespace std;
int main()
{
    vector<int>v;
    string s;
    int a,i,b,c,sum=0,t=0;
    cin>>s;
    cin>>c;
    for(i=0; i<26; i++)
    {
        cin>>b;
        v.push_back(b);
    }
    int large=*max_element(v.begin(),v.end());
    int l=s.size();
    for(i=0; i<l; i++)
    {
        int x=s[i];
        sum=sum+((i+1)*(v[x-97]));
    }
    for(i=l+1;; i++)
    {
        sum=sum+(large*i);
        t++;
        if(t==c)
        {
            break;
        }
    }
    cout<<sum;

    return 0;
}


#include <bits/stdc++.h>
#define ll long long
#define f first
#define s second
using namespace std;
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    string s;
    cin>>s;
    ll k;
    cin>>k;
    ll maxVal=INT_MIN;
    ll array[27];
    for(int i=1;i<27;i++){
        ll arr;
        cin>>arr;
        array[i]=arr;
        maxVal=max(arr,maxVal);
    }
    ll ans=0;
    ans+=(k*(k+1))/2;
//    cout<<ans<<endl;
    ans+=k*(s.length());
//    cout<<ans<<endl;
    ans*=maxVal;
    for(int i=0;i<s.length();i++){
        char a=s[i];
        ans+=array[a-'a'+1]*(i+1);
    }
//    for(int i=s.length()+1;i<=s.length()+k;i++){
//        ans+=(maxVal*i);
//    }
    cout<<ans<<endl;
    return 0;
}


#include <bits/stdc++.h>
#define ll long long
#define f first
#define s second
using namespace std;
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    string s;
    cin>>s;
    ll k;
    cin>>k;
    ll maxVal=INT_MIN;
    ll array[27];
    for(int i=1;i<27;i++){
        ll arr;
        cin>>arr;
        array[i]=arr;
        maxVal=max(arr,maxVal);
    }
    ll ans=0;
//    ans+=(k*(k+1))/2;
//    cout<<ans<<endl;
//    ans+=k*k;
//    cout<<ans<<endl;
//    ans*=maxVal;
    for(int i=0;i<s.length();i++){
        char a=s[i];
        ans+=array[a-'a'+1]*(i+1);
    }
    for(int i=s.length()+1;i<=s.length()+k;i++){
        ans+=(maxVal*i);
    }
    cout<<ans<<endl;
    return 0;
}


#include <bits/stdc++.h>
#define ll long long
#define f first
#define s second
using namespace std;
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    string s;
    cin>>s;
    ll k;
    cin>>k;
    ll maxVal=INT_MIN;
    ll array[27];
    for(int i=1;i<27;i++){
        ll arr;
        cin>>arr;
        array[i]=arr;
        maxVal=max(arr,maxVal);
    }
    ll ans=0;
    ans+=(k*(k+1))/2;
    cout<<ans<<endl;
    ans+=k*k;
    cout<<ans<<endl;
    ans*=maxVal;
    for(int i=0;i<s.length();i++){
        char a=s[i];
        ll temp=a-96;
        //cout<<temp<<endl;
        ans+=array[temp]*(i+1);
    }
    cout<<ans<<endl;
    return 0;
}


#include <bits/stdc++.h>
#define ll long long
#define f first
#define s second
using namespace std;
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    string s;
    cin>>s;
    ll k;
    cin>>k;
    ll maxVal=INT_MIN;
    ll array[27];
    for(int i=1;i<27;i++){
        ll arr;
        cin>>arr;
        array[i]=arr;
        maxVal=max(arr,maxVal);
    }
    ll ans=0;
    ans+=(k*(k+1))/2;
//    cout<<ans<<endl;
    ans+=k*k;
//    cout<<ans<<endl;
    ans*=maxVal;
    for(int i=0;i<s.length();i++){
        char a=s[i];
        ll temp=a-96;
        //cout<<temp<<endl;
        ans+=array[temp]*(i+1);
    }
    cout<<ans<<endl;
    return 0;
}


#include <bits/stdc++.h>
#include<string>
#include<map>
using namespace std;
int main()
{
    string s;
    int n,m;
    m=-1;
    unsigned long long int sum=0;
    cin>>s>>n;
    int arr[26];
    for(int i=0;i<26;i++)
    {
        cin>>arr[i];
    }
    for(int i=0;i<26;i++)
    {
        m=max(m,arr[i]);
    }
    int l=s.size();
    for(int i=0;i<s.size();i++)
    {
        int t=s[i]-'a';
        sum+=(i+1)*arr[t];
    }
    int g=n+l;
    m=m/2;
    sum+=m*(g*(g+1)-l*(l+1));
    cout<<sum;
    return 0;
}


#include <bits/stdc++.h>
#include<string>
#include<map>
using namespace std;
int main()
{
    string s;
    int n,m;
    m=-1;
    unsigned long long int sum=0;
    cin>>s>>n;
    int arr[26];
    for(int i=0;i<26;i++)
    {
        cin>>arr[i];
    }
    for(int i=0;i<26;i++)
    {
        m=max(m,arr[i]);
    }
    int l=s.size();
    for(int i=0;i<s.size();i++)
    {
        int t=s[i]-'a';
        sum+=(i+1)*arr[t];
    }
    int g=n+l;
    g=(g*(g+1)/2-l*(l+1)/2);
    sum+=m*g;
    cout<<sum;
    return 0;
}


#include <bits/stdc++.h>
#include<string>
#include<map>
using namespace std;
int main()
{
    string s;
    int n,m;
    m=-1;
    unsigned long long int sum=0;
    cin>>s>>n;
    int arr[26];
    for(int i=0;i<26;i++)
    {
        cin>>arr[i];
    }
    for(int i=0;i<26;i++)
    {
        m=max(m,arr[i]);
    }
    int l=s.size();
    for(int i=0;i<s.size();i++)
    {
        int t=s[i]-'a';
        sum+=(i+1)*arr[t];
    }
    int g=n+l;
    sum+=m*(g*(g+1)-l*(l+1))/2;
    cout<<sum;
    return 0;
}


#include <bits/stdc++.h>
#include<string>
#include<map>
using namespace std;
int main()
{
    string s;
    int n,m,sum;
    m=-1;
    sum=0;
    cin>>s>>n;
    int arr[26];
    for(int i=0;i<26;i++)
    {
        cin>>arr[i];
    }
    for(int i=0;i<26;i++)
    {
        m=max(m,arr[i]);
    }
    int l=s.size();
    for(int i=0;i<s.size();i++)
    {
        int t=s[i]-'a';;
        sum+=(i+1)*arr[t];
    }
    int g=n+l;
    sum+=m*(g*(g+1)-l*(l+1))/2;
    cout<<sum;
    return 0;
}


#include <bits/stdc++.h>
#include<string>
#include<map>
using namespace std;
int main()
{
    string s;
    int n,m;
    m=-1;
    unsigned long long int sum=0;
    cin>>s>>n;
    int arr[26];
    for(int i=0;i<26;i++)
    {
        cin>>arr[i];
    }
    for(int i=0;i<26;i++)
    {
        m=max(m,arr[i]);
    }
    int l=s.size();
    for(int i=0;i<s.size();i++)
    {
        int t=s[i]-'a';;
        sum+=(i+1)*arr[t];
    }
    int g=n+l;
    sum+=m*(g*(g+1)-l*(l+1))/2;
    cout<<sum;
    return 0;
}


#include <bits/stdc++.h>
#include<string>
#include<map>
using namespace std;
int main()
{
    string s;
    int n,m;
    m=-1;
    long long int sum=0;
    cin>>s>>n;
    int arr[26];
    for(int i=0;i<26;i++)
    {
        cin>>arr[i];
    }
    for(int i=0;i<26;i++)
    {
        m=max(m,arr[i]);
    }
    int l=s.size();
    for(int i=0;i<s.size();i++)
    {
        int t=s[i]-'a';;
        sum+=(i+1)*arr[t];
    }
    int g=n+l;
    sum+=m*(g*(g+1)-l*(l+1))/2;
    cout<<sum;
    return 0;
}


#include <bits/stdc++.h>

using namespace std;

int main()
{
    string s; cin>>s; int a[26];int k,S=0; cin>>k; int maxm =-1,j;
    for (int i=1;i<=26;i++) {cin>>a[i]; if (a[i]>=maxm) maxm=a[i], j=i;}
    int x= k+s.size();
    for (int i=0;i<s.size(); i++)
    {
        int y= s[i]- 96;
        //cout <<y<<endl;
        S= S+ (a[y]*(i+1));
    }
    //cout <<S<<endl;
    for (int i=s.size(); i<x; i++)
    {
        S=S+(a[j]*(i+1));
    }
    cout <<S<<endl;

}


#include <bits/stdc++.h>

using namespace std;

int main()
{
    string s; cin>>s; int a[26];int k;long long int S=0; cin>>k; int maxm =-1,j;
    for (int i=1;i<=26;i++) {cin>>a[i]; if (a[i]>=maxm) maxm=a[i], j=i;}
    int x= k+s.size();
    for (int i=0;i<s.size(); i++)
    {
        int y= s[i]- 96;
        //cout <<y<<endl;
        S= S+ (a[y]*(i+1));
    }
    //cout <<S<<endl;
    for (int i=s.size(); i<x; i++)
    {
        S=S+(a[j]*(i+1));
    }
    cout <<S<<endl;

}


#include <bits/stdc++.h>

using namespace std;

int main()
{
    string s; int k;int S=0;

    cin>>s;
    int a[27];
    cin>>k;
    int maxm =-1,j;

    for (int i=1;i<=26;i++)
        {cin>>a[i];
    if (a[i]>=maxm)
        {maxm=a[i]; j=i;}
    }
    //cout <<k<<endl;
    int x= s.size();
    k=k+x;
    cout <<k<<endl;
    for (int i=0;i<s.size(); i++)
    {
        int y= s[i]- 96;
        //cout <<y<<endl;
        S= S+ (a[y]*(i+1));
    }
    //cout <<j<<endl;
    //cout <<S<<endl;
    //cout <<x<< " "<<s.size()<<endl;
    for (int i=s.size(); i<k; i++)
    {
        S=S+(a[j]*(i));
    }
    cout <<S<<endl;

}


#include <bits/stdc++.h>

using namespace std;

int main()
{
    string s; int k;int S=0; cin>>s; int a[27]; cin>>k; int maxm =-1,j;
    for (int i=1;i<=26;i++) {cin>>a[i]; if (a[i]>=maxm) {maxm=a[i]; j=i;}}
    //cout <<k<<endl;
    int x= s.size();
    k=k+x;
    //cout <<k<<endl;
    for (int i=0;i<s.size(); i++)
    {
        int y= s[i]- 96;
        //cout <<y<<endl;
        S= S+ (a[y]*(i+1));
    }
    //cout <<j<<endl;
    //cout <<S<<endl;
    //cout <<x<< " "<<s.size()<<endl;
    for (int i=s.size(); i<k; i++)
    {
        S=S+(a[j]*(i+1));
    }
    cout <<S<<endl;

}


#include <bits/stdc++.h>

using namespace std;

int main()
{
    string s; cin>>s; int a[26];int k,S=0; cin>>k; int maxm =-1,j; char c[1001];
    for (int i=1;i<=26;i++) {cin>>a[i]; if (a[i]>=maxm) maxm=a[i], j=i;}
    for (int i=0;i<s.size(); i++) c[i]=s[i];
    char A= 95+j;
    int x= k+s.size();
    for (int i=0;i<s.size(); i++)
    {
        int y= s[i]- 96;
        //cout <<y<<endl;
        S= S+ (a[y]*(i+1));
    }
    //cout <<S<<endl;
    for (int i=s.size(); i<x; i++)
    {
        S=S+(a[j]*(i+1));
    }
    cout <<S<<endl;

}


#include<bits/stdc++.h>
using namespace std;
int a[30];
int main() {
	string s;
	cin >> s;
	int sum = 0 , k;
	cin >> k;
	for (int i = 0; i < 26; i++) {
		cin >>a[i];
	}
	for (int i = 0; i < s.size(); i++) {
		sum +=(i + 1) * a[s[i] - 'a' - 1];
	}
	sort(a,a+26);
	k -= s.size();
	for(int i = s.size() + 1; i <= k; i++)
		sum += i * a[25];
	cout << sum;
}


#include<bits/stdc++.h>
using namespace std;
int a[30];
int main() {
	string s;
	cin >> s;
	int sum = 0 , k;
	cin >> k;
	for (int i = 0; i < 26; i++) {
		cin >>a[i];
	}
	for (int i = 0; i < s.size(); i++) {
		sum +=(i + 1) * a[s[i] - 'a'];
	}
	sort(a,a+26);
	k -= s.size();
	for(int i = s.size() + 1; i <= k; i++)
		sum += i * a[25];
	cout << sum;
}


#include<bits/stdc++.h>
using namespace std;
int a[30] , b[30];
int main() {
	string s;
	cin >> s;
	int k , sum = 0;
	cin >> k;
	for (int i = 0; i < 26; i++){
		cin >> a[i];
		b[i] = a[i];
	}
	for (int i = 1; i <= s.size(); i++) {
		sum += (i) * a[s[i - 1] - 'a'];
	}
	sort(b , b + 26);
	int sz = s.size();
	for (int i = sz + 1 ; i <= sz + k;i++ ){
		sum += (i + 1) * b[25];
	}
	cout << sum;
}


#include<bits/stdc++.h>
using namespace std;
int a[30] , b[30], k , sum = 0;
int main() {
	string s;
	cin >> s;
	cin >> k;
	for (int i = 0; i < 26; i++){
		cin >> a[i];
		b[i] = a[i];
	}
	for (int i = 1; i <= s.size(); i++) {
		sum += (i) * a[s[i - 1] - 'a'];
	}
	sort(b , b + 26);
	int sz = s.size();
	for (int i = sz + 1 ; i <= sz + k;i++ ){
		sum += (i ) * b[25];
	}
	cout << sum;
}


#include <iostream>
#include <bits/stdc++.h>

using namespace std;
#define ll long long
int main()
{
    char s[1005];
    ll k,ma=0;
    ll w[30];
    scanf("%s%I64d",s,&k);
    int len=strlen(s);
    for(int i=0;i<26;++i){
        scanf("%I64d",&w[i]);
        ma=max(ma,w[i]);
    }
    ll ans=0;
    for(ll i=0;i<len;++i){
        ans+=(i+1)*w[s[i]-'a'];
    }
    ans+=ma*(len*2+1+k)*k/2;
    printf("%I64d\n",ans);
    return 0;
}


#include <iostream>
#include <bits/stdc++.h>

using namespace std;

int main()
{
    char s[1005];
    int k,ma=0;
    int w[30];
    scanf("%s%d",s,&k);
    int len=strlen(s);
    for(int i=0;i<26;++i){
        scanf("%d",&w[i]);
        ma=max(ma,w[i]);
    }
    int ans=0;
    for(int i=0;i<len;++i){
        ans+=(i+1)*w[s[i]-'a'];
    }
    ans+=ma*(len*2+1+k)*k/2;
    printf("%d\n",ans);
    return 0;
}


#include<iostream>
using namespace std;
int max(int n,long a[])
{
	int i,m;
	for(i=0;i<n-1;i++)
	{
		m=max(a[i],a[i+1]);
	}
	return m;
}
main()
{
	int n,i,j;
	long k=0,m;
	long a[100];
	cin>>n;
	for(i=0;i<n;i++)
	{
		cin>>a[i];
	}
	m=max(n,a);
	cout<<m;
	for(i=0;i<n;i++)
	{
		for(j=0;a[i]<m;j++)
		{
			k++;
			a[i]+=1;
		}
	}

	cout<<k;

}


#include<iostream>
using namespace std;
int max(int n,long a[])
{
	int i,m;
	for(i=0;i<n-1;i++)
	{
		m=max(a[i],a[i+1]);
	}
	return m;
}
main()
{
	int n,i,j;
	long k=0,m;
	long a[100];
	cin>>n;
	for(i=0;i<n;i++)
	{
		cin>>a[i];
	}
	m=max(n,a);
	cout<<m;
	for(i=0;i<n;i++)
	{
		for(j=0;a[i]<m;j++)
		{
			k++;
			a[i]+=1;
		}
		cout<<k;
	}

}


#include<iostream>
using namespace std;
int max(int size,int array[])
{
	int maximum ,c;
	 maximum = array[0];
  for (c = 1; c < size; c++)
  {
    if (array[c] > maximum)
    {
       maximum  = array[c];
    }
  }
  return maximum;
}

main()
{
	int n,i,j;
	int k=0,m;
	int a[100];
	cin>>n;
	for(i=0;i<n;i++)
	{
		cin>>a[i];
	}
	m=max(n,a);
	for(i=0;i<n;i++)
	{
		if(a[i]<m)
		{
			k=k+(m-a[i]);
		}
	}
	cout<<k;
}


#include <iostream>

using namespace std;

int main()
{
    int n;
    int c = 0;
    cin >> n;
    int arr[n];
    for (int i = 0; i < n; i++)
    {
        cin >> arr[i];
    }
    int max = arr[0];
    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < n; j++)
        {
            if (arr[i]>arr[j])
            {
                swap(arr[i],arr[j]);
            }
            if (arr[i]>max)
            {
                max=arr[i];
            }
            if (arr[j]>max)
            {
                max=arr[j];
            }
        }
    }
    for (int i = 0; i < n; i++)
    {
        while (arr[i]!=n)
        {
            arr[i]++;
            c++;
        }
    }
    std::cout << c << std::endl;
    return 0;
}


#include <iostream>

using namespace std;

int main()
{
    int n;
    int c = 0;
    cin >> n;
    int arr[n];
    for (int i = 0; i < n; i++)
    {
        cin >> arr[i];
    }
    int max = arr[0];
    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < n; j++)
        {
            if (arr[i]>arr[j])
            {
                swap(arr[i],arr[j]);
            }
            if (arr[i]>max)
            {
                max=arr[i];
            }
            if (arr[j]>max)
            {
                max=arr[j];
            }
        }
    }
    for (int i = 0; i < n; i++)
    {
        while (arr[i]!=n)
        {
            arr++;
            c++;
        }
    }
    std::cout << c << std::endl;
    return 0;
}


#include <iostream>

using namespace std;

int main()
{
    freopen("input.txt","r",stdin);
    freopen("output.txt","w",stdout);
    int n;
    int c = 0;
    cin >> n;
    int arr[n];
    for (int i = 0; i < n; i++)
    {
        cin >> arr[i];
    }
    int max = arr[0];
    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < n; j++)
        {
            if (arr[i]>arr[j])
            {
                swap(arr[i],arr[j]);
            }
            if (arr[i]>max)
            {
                max=arr[i];
            }
            if (arr[j]>max)
            {
                max=arr[j];
            }
        }
    }
    for (int i = 0; i < n; i++)
    {
        while (arr[i]<max)
        {
            arr[i]=arr[i]+1;
            c=c+1;
        }
    }
    std::cout << c;
    return 0;
}


#include <iostream>

using namespace std;

int main()
{
    int n;
    int c = 0;
    cin >> n;
    int arr[n];
    for (int i = 0; i < n; i++)
    {
        cin >> arr[i];
    }
    int max = arr[0];
    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < n; j++)
        {
            if (arr[i]>arr[j])
            {
                swap(arr[i],arr[j]);
            }
            if (arr[i]>max)
            {
                max=arr[i];
            }
            if (arr[j]>max)
            {
                max=arr[j];
            }
        }
    }
    for (int i = 0; i < n; i++)
    {
        while (arr[i]<max)
        {
            arr[i]=arr[i]+1;
            c=c+1;
        }
    }
    std::cout << c;
    return 0;
}


#include <iostream>
using namespace std;

int main()
{
    int n,big,sum=0,b=0;
    cin>>n;
    int a[n];
    for(b=0;b<n;b++){
        cin>>a[b];
        if(b==0) big=a[b];
        if(b>=1 && a[b]>=a[b-1] && a[b]>=big) big=a[b];
        sum+=a[b];
    }
    sum=n*big-sum;
    if(n==1) sum=0;
    cout<<sum<<endl;
    return 0;
}


#include <iostream>

using namespace std;

int main()
{
    int n,a[n],big=0,sum=0,b=0;
    cin>>n;
    for(b=0;b<n;b++){
        cin>>a[b];
        if(b>=1 && a[b]>=a[b-1]) big=a[b];
        sum+=a[b];
    }
    sum=n*big-sum;
    if(n==1) sum=0;
    cout<<sum<<endl;
    return 0;
}


#include <iostream>

using namespace std;

int main()
{
    int n,a[n],big=0,sum=0;
    cin>>n;
    for(int b=0;b<n;b++){
        cin>>a[b];
        if(b>=1 && a[b]>=a[b-1]) big=a[b];
        sum+=a[b];
    }
    sum=n*big-sum;
    if(n==1) sum=0;
    cout<<sum<<endl;
    return 0;
}


#include <iostream>
#include <vector>
using namespace std;

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

	int n;
	vector<int> arr{};

	int max = 0;
	int sum = 0;

	while (N--) {
		cin >> n;
		arr.push_back(n);
		if (n > max) max = n;
	}

	for (int i = 0; i < arr.size(); ++i) {
		sum += max - arr[i];
	}

	cout << sum;

	return 0;
}


#include <iostream>
#include <vector>
using namespace std;

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

	int n;
	vector<int> arr{};

	int max = 0;
	int sum = 0;

	while (N--) {
		cin >> n;
		if (n > max) {
			max = n;
		}
		arr.push_back(n);
	}

	for (int i = 0; i < N; ++i) {
		sum += max - arr[i];
	}

	cout << sum;

	return 0;
}


#include <iostream>
#include <vector>
using namespace std;

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

	int n;
	vector<int> arr{};

	int max = 0;
	int sum = 0;

	while (N--) {
		cin >> n;
		if (n > max) {
			max = n;
		}
		arr.push_back(n);
	}

	for (int i = 0; i < N; ++i) {
		sum += abs(max - n);
	}

	cout << sum;

	return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    int n,i,maximum,count=0;
    cin>>n;
    char a[n];
    for(i=0;i<n;i++)
    {
        cin>>a[i];
    }
    sort(a,a+n);
    if(n==1)
    {
        cout<<0<<endl;
    }
    else{
    for(i=1;i<n;i++)
    {
        maximum=a[0];
        if(a[i]>maximum)
        {
            maximum=a[i];
        }
    }
    for(i=0;i<n;i++)
    {
        count=count+(maximum-a[i]);
    }
    cout<<count<<endl;}
    return  0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    int n,i,maximum,count=0;
    cin>>n;
    char a[n];
    for(i=0;i<n;i++)
    {
        cin>>a[i];
    }
    sort(a,a+n);
    for(i=1;i<n;i++)
    {
        maximum=a[0];
        if(a[i]>maximum)
        {
            maximum=a[i];
        }
    }

    for(i=0;i<n;i++)
    {
        if(a[i]!=maximum){
        count=count+(maximum-a[i]);}
    }
    cout<<count<<endl;
    return  0;

}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    int n,i,maximum,count=0;
    cin>>n;
    long long a[n];
    for(i=0;i<n;i++)
    {
        cin>>a[i];
    }
    sort(a,a+n);
    if(n==1)
    {
        cout<<0<<endl;
    }
    else{
    for(i=1;i<n;i++)
    {
        maximum=a[0];
        if(a[i]>maximum)
        {
            maximum=a[i];
        }
    }
    for(i=0;i<n;i++)
    {
        count=count+(maximum-a[i]);
    }
    cout<<count<<endl;}
    return  0;
}


#include<iostream>
using namespace std;
int z[105];
int main()
{
    int n;
    while(cin>>n)
    {
        int sum=0,max=-2;
        for(int i=0;i<n;i++)
        {
            cin>>z[i];
            if(z[i]>max)
            {
                max=z[i];
            }
        }
        for(int i=0;i<n;i++)
        {
            sum=sum+(max-z[i]);
        }
        cout<<sum<<endl;
    }
}
//2018-11-21 18:42:55.012


#include<bits/stdc++.h>
using namespace std;
int a[100005];
int main ()
{
    int i,n,p;
    scanf("%d",&n);
    for (i=0;i<n;i++)
    {
      scanf("%d",&a[i]);
      if (i==0)
        p=a[i];
      else if (a[i]>p)
        p=a[i];
    }
    if (n==1){
      puts("0");return 0;}
   int  sum=0;
    for (i=0;i<n;i++)
      sum=sum+(p-a[i]);
    printf("%d\n",sum);
    return 0;
}
//2018-11-19 12:16:05.214


#include<bits/stdc++.h>
using namespace std;
int main()
{
    int i,n,a=0,b,sum;
    cin>>n;
    sum=0;
    int x[n];
    for(i=0;i<n;i++)
    {
        cin>>b;
        if(b>a&&i!=0)
        {
        sum+=b;
        a=b;
        }
        else
            a=b;
    }
    cout<<sum;
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    int i,n,a=0,b,sum;
    cin>>n;
    sum=0;
    int x[n];
    for(i=0;i<n;i++)
    {
        cin>>b;
        if(b>a&&n!=1)
        {
        sum+=b;
        a=b;
        }
    }
    cout<<sum;
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    int i,n,b=0,sum=0;
    cin>>n;
    int x[n];
    for(i=0;i<n;i++)
    {
        cin>>x[i];
        b=max(b,x[i]);
        sum+=x[i];

    }
    cout<<((b*n)-sum);
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    int i,n,a=0,b,sum;
    cin>>n;
    sum=0;
    int x[n];
    for(i=0;i<n;i++)
    {
        cin>>b;
        if(b>a&&n!=0)
        {
        sum+=b;
        a=b;
        }
        else
            a=b;
    }
    cout<<sum;
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    int n,i,a[100],c=0;
    cin>>n;

    for(i=0;i<n;i++)
    {
        cin>>a[i];
    }
    sort(a,a+n);
    if(n==1) cout<<0;
    else{
    for(i=0;i<n;i++)
    {
        if(a[i]!=a[i+1])
        {
            c+=a[i];
        }
    }
    cout<<c;
}
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
 int n,a[100],i,s=0,j,temp;
    cin>>n;
    for(i=0;i<n;i++)
    {
    cin>>a[i];
    }
    for(i=0;i<n;i++)
    {
        for(j=i+1;j<n;j++)
        {
            if(a[i]>a[j])
               {
                    temp=a[i];
                    a[i]=a[j];
                    a[j]=temp;
               }
        }

    }    for(i=0;i<n;i++)
    {
        if(a[i]!=a[i+1])
        {
            s+=a[i];
        }
    } if(n==1)
    {
        cout<<0;
    }
else

    cout<<s;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
    int n,i,a[100],c=0;
    cin>>n;
    for(i=0;i<n;i++)
    {
        cin>>a[i];
    }
    sort(a,a+n);
    for(i=0;i<n;i++)
    {
        if(a[i]!=a[i+1])
        {
            c+=a[i];
        }
    }
    cout<<c;
}


#include<bits/stdc++.h>
using namespace std;
int main()
{
 int n,a[100],i,s=0;
    cin>>n;
    for(i=0;i<n;i++)
    {
    cin>>a[i];
    }
    sort(a,a+n);
    for(i=0;i<n;i++)
    {
        s+=a[n-1]-a[i];
    }
    cout<<s;

}


#include<bits/stdc++.h>

using namespace std;

main()
{
    int n,maxx=-10000000;
    cin >> n;
    int a[n] , s=0 ;
    for(int i = 0 ; i < n ; i++){
        cin >> a[i];
        if(maxx < a[i]) maxx = a[i];
    }
    while (n--) s+=maxx-a[i];
    cout << s;
}


#include<bits/stdc++.h>

using namespace std;

main()
{
    int n,maxx=-10000000;
    cin >> n;
    int a[n] , s=0 ;
    for(int i = 0 ; i < n ; i++){
        cin >> a[i];
        if(a[i] > maxx) maxx = a[i];
        s += -a[i] + maxx;
    }
    cout << s;
}


#include<bits/stdc++.h>

using namespace std;

main()
{
    int i,n,maxx=-10000000;
    cin >> n;
    int a[n] , s=0 ;
    for(i = 0 ; i < n ; i++){
        cin >> a[i];
        if(maxx < a[i]) maxx = a[i];
    }
    while (n--) s+=maxx-a[i];
    cout << s;
}


#include<bits/stdc++.h>

using namespace std;

main()
{
    int i,n,maxx=-10000000;
    cin >> n;
    int a[n] , s=0 ;
    for(i = 0 ; i < n ; i++){
        cin >> a[i];
        if(maxx < a[i]) maxx = a[i];
    }
   for(i = 0 ; i < n ; i++) s+=maxx-a[i];
    cout << s;
}


#include<bits/stdc++.h>

using namespace std;

main()
{
    int n,maxx=-10000000;
    cin >> n;
    int a[n] , s=0 ;
    for(int i = 0 ; i < n ; i++){
        cin >> a[i];
        if(a[i] > maxx) maxx = a[i];
        s += maxx - a[i];
    }
    cout << s;
}


#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;

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

    }
    if(sum<0)
    {
        sum=-sum;
    }
     if(n==1){
        sum=0;
         cout<<sum;
    }
    cout<<sum;

        return 0;
}


#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;

int main() {
int n;
    cin>>n;
    int b=0;
    long a[n];
    long sum=0;
    int i;
    for(i=0;i<n;i++)
    {
        cin>>a[i];
    }
    for(i=0;i<n;i++)
    {
if(a[i]>b)
{
   b=a[i];
}
}
    for(i=0;i<n;i++)
    {
        sum+=(b-a[i]);
        }
    if(n==1){
        sum=0;
         cout<<sum;
    }
  else{
      cout<<sum;
  }

        return 0;
}


#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;

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

    }
    if(sum<0)
    {
        sum=-sum;
         cout<<sum;
    }
     if(n==1){

         cout<<"0";
    }

        return 0;
}


#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;

int main() {
int n;
    cin>>n;
    int b=0;
    long a[n];
    long sum=0;
    int i;
    for(i=0;i<n;i++)
    {
        cin>>a[i];
    }
    for(i=0;i<n-1;i++)
    {
if(a[i]>b)
{
   b=a[i];
}
}
    for(i=0;i<n;i++)
    {
        sum+=(b-a[i]);
        }
    if(n==1){
        sum=0;
         cout<<sum;
    }
  else{
      cout<<sum;
  }

        return 0;
}


#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;

int main() {
int n;
    cin>>n;
    int b;
    int a[n];
    int sum=0;
    int i;
    for(i=0;i<n;i++)
    {
        cin>>a[i];
    }
    for(i=0;i<n-1;i++)
    {
if(a[i]>a[i+1])
{
     b=a[i];
}
}
    for(i=0;i<n;i++)
    {
        sum+=(a[i]-b);
        }
    if(sum<0)
    {
        sum=-sum;
     }
     if(n==1){
        sum=0;
         cout<<sum;
    }
  else{
      cout<<sum;
  }

        return 0;
}


#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;

int main() {
int n;
    cin>>n;
    int b;
    int a[n];
    int sum=0;
    int i;
    for(i=0;i<n;i++)
    {
        cin>>a[i];
    }
    for(i=0;i<n-1;i++)
    {
if(a[i]>a[i+1])
{
     b=a[i];
}
        else{
            b=a[i+1];
        }
}
    for(i=0;i<n;i++)
    {
        sum+=(b-a[i]);
        }
    if(n==1){
        sum=0;
         cout<<sum;
    }
  else{
      cout<<sum;
  }

        return 0;
}


#include <iostream>
using namespace std;
int i,people[100],peoplen,anwser,m;
int main() {
	cin>>peoplen;
	for(i=0; i<peoplen; i++){
		cin>>people[i];
	}
		m=people[1];
		for(i=0;i<peoplen-1;i++){
			m=max(m,people[i+1]);
		}
		for(i=0;i<peoplen;i++){
			anwser=anwser+m-people[i];
		}
		cout<<anwser;
	}
}


#include <iostream>
using namespace std;
int i,people[100],peoplen,anwser,m;
int main() {
	cin>>peoplen;
	for(i=0; i<peoplen; i++){
		cin>>people[i];
	}
	if(peoplen>1){
		m=people[0];
		for(i=0;i<peoplen-1;i++){
			m=max(m,people[i+1]);
		}
		for(i=0;i<peoplen;i++){
			anwser=anwser+m-people[i];
		}
		cout<<anwser;
	}
	else{
		cout<<"0";
	}
}


#include <iostream>
using namespace std;
int i,people[100],peoplen,anwser,m;
int main() {
	cin>>peoplen;
	for(i=0; i<peoplen; i++){
		cin>>people[i];
	}
	if(peoplen>1){
		m=people[1];
		for(i=0;i<peoplen-1;i++){
			m=max(m,people[i+1]);
		}
		for(i=0;i<peoplen;i++){
			anwser=anwser+m-people[i];
		}
		cout<<anwser;
	}
	else{
		cout<<"0";
	}
}


#include <iostream>
using namespace std;
int i,people[100],peoplen,anwser,m;
int main() {
	cin>>peoplen;
	for(i=0; i<peoplen; i++){
		cin>>people[i];
	}
		m=people[1];
		for(i=0;i<peoplen-1;i++){
			m=max(m,people[i+1]);
		}
		for(i=0;i<peoplen;i++){
			anwser=anwser+m-people[i];
		}
		cout<<anwser;
	}


#include <iostream>
using namespace std;
int i,people[100],peoplen,anwser,m;
int main() {
	cin>>peoplen;
	for(i=0; i<peoplen; i++){
		cin>>people[i];
	}
	if(peoplen>1){
		for(i=0;i<peoplen-1;i++){
			m=max(people[i],people[i+1]);
		}
		for(i=0;i<peoplen;i++){
			anwser=anwser+m-people[i];
		}
		cout<<anwser;
	}
	else{
		cout<<"0";
	}
}


#include <iostream>
using namespace std;
int i,people[100],peoplen,anwser,m;
int main() {
	cin>>peoplen;
	for(i=0; i<peoplen; i++){
		cin>>people[i];
	}
	for(i=0;i<peoplen-1;i++){
		m=max(people[i],people[i+1]);
	}
	for(i=0;i<peoplen;i++){
		anwser=anwser+m-people[i];
	}
	cout<<anwser;
}


#include <cstdio>
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
int main()
{
    int n;
    cin >>n;
    long long a[n];
        int maxi=-1234;
        int sum=0;

        for(int i=0; i<n; i++)
        {
            cin >>a[i];
            if(a[i]>maxi)
            {
                maxi=a[i];

            }  sum+=a[i];
        }

            cout <<(n*maxi)-sum<<endl;

    return 0;
}


#include <cstdio>
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
int main()
{
    int n;
    cin >>n;
    long long a[n];
        int maxi=-1234;
        int sum=0;

        for(int i=0; i<n; i++)
        {
            cin >>a[i];
            if(a[i]>maxi)
            {
                maxi=a[i];
                sum+=a[i];
            }
        }

            cout <<(n*maxi)-sum<<endl;

    return 0;
}


#include <cstdio>
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
int main()
{
    int n;
    cin >>n;
    int a[n];
    for(int i=0; i<n; i++)
        cin >> a[i];
        int maxi=a[0];
        int mini=0;

        for(int i=1; i<n; i++)
            if(a[i]>a[i-1])maxi=a[i];

            for(int i=0; i<n; i++)
            {
                mini+=maxi-a[i];
            }
            cout <<mini<<endl;

    return 0;
}


#include <cstdio>
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
int main()
{
    int n;
    cin >>n;
    int a[n];
    for(int i=0; i<n; i++)
        cin >> a[i];
        int maxi=a[0];
        int sum=0;

        for(int i=1; i<n; i++)
            if(a[i]>a[i-1])maxi=a[i];

            for(int i=0; i<n; i++)
                sum+=a[i];
            cout <<(n*maxi)-sum<<endl;

    return 0;
}


#include<iostream>
#include<algorithm>
using namespace std;
int main ()
{
    int n,i,max;

    while (cin >> n)
    {
        int arr[n],sum = 0;

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

        sort (arr,arr+i);

        max = arr[n-1];

        for (i=0; i<n-1; i++){
            sum += abs (max - arr[i]);
        }

        cout << sum << endl;
    }

    return 0;
}


#include<iostream>
using namespace std;
int main ()
{
    int n,max;

    while (cin >> n)
    {
        int arr[n],sum = 0;

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

        sort (arr,arr+i);
        }

        max = arr[n-1];

        for (int i=0; i<n-1; i++){
            sum += abs (max - arr[i]);
        }

        cout << sum << endl;
    }

    return 0;
}


#include<iostream>
#include<algorithm>
using namespace std;
int main ()
{
    int n,max;

    while (cin >> n)
    {
        int arr[n],sum = 0;

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

        sort (arr,arr+i);
        }

        max = arr[n-1];

        for (int i=0; i<n-1; i++){
            sum += abs (max - arr[i]);
        }

        cout << sum << endl;
    }

    return 0;
}


#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int main(){
	int a[101],n,mx,ans=0;
	cin>>n;
	memset(a,0,sizeof(a));
	for(int i=0;i<n;i++){
		cin>>a[i];
		if(a[i]>mx)mx=a[i];
	}
	for(int i=0;i<n;i++)ans+=mx-a[i];
	cout<<ans<<endl;
}


#include <iostream>
#include <algorithm>
using namespace std;
int a[105],n,sum;
int main()
{
	cin>>n;
	int i;
	for (i=0;i<n;i++){
		cin>>a[i];
	}
	sort(a,a+n);
	for (i=0;i<n;i++){
		sum+=a[n-1]-a[i];
	}
	cout<<sum<<endl;
	return 0;
}


#include<iostream>
#include<algorithm>
#include<cmath>
using namespace std;
const int maxn=110;
int n;
int a[maxn];
int main(){
	int maxx=0;
	int ans=0;
	cin>>n;
	for(int i=1;i<=n;i++){
		cin>>a[i];
		maxx=max(maxx,a[i]);
	}
	for(int i=1;i<=n;i++){
		ans+=(maxx-a[i]);
	}
	cout<<ans<<endl;
	return 0;
}


#include<bits\stdc++.h>
using namespace std;
long long n,i,maxn,s,a[105];

int main()
{
    cin >> n;
    for(int i=1;i<=n;i++) cin>>a[i];
    maxn=0;
    for(int i=1;i<=n;i++) if(a[i]>=maxn) maxn=a[i];
    s=0;
    for(int i=1;i<=n;i++) s+=maxn-a[i];
    cout << s << endl;
    return 0;
}

/*123*/


#include <cstdio>

int main() {
    int n, max = 0, sum = 0;
    scanf("%d", &n);
    for (int i = 0; i < n; ++i) {
        int a;
        scanf("%d", &a);
        if (a > max) max = a;
        sum += a;
    }
    printf("%d\n", max * m - sum);
    return 0;
}


// 1b
#include <iostream>
#include <algorithm>
using namespace std;

int main(){
	long long n, sum, temp, max;
	sum = 0, max = -1;
	cin >> n;
	int i = n;
	while(i--){
		cin >> temp;
		sum += temp;
		max = max > temp ? max : temp;
	}
	int ans = (max * n) - sum;
	cout << ans;
	return 0;
}


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

ll n;

int main(){
	scanf("%d",&n);
	ll ans = 0;
	ll temp,maxn=0;
		for(int i=0;i<n;i++){
			cin >> temp;
			ans+=temp;
			if(temp>maxn)	maxn=temp;
		}
		 cout << maxn*n-ans << endl;
	return 0;
}


#include<cstring>
#include<iostream>
#include<cstdio>
#include<string>
#include<cmath>
#include<queue>
#include<vector>
#include<set>
#include<algorithm>
using namespace std;
int n;
int a[1003];
int main()
{
    cin>>n;

    int mx=-1;
    for(int i=1;i<=n;i++)
    {
        cin>>a[i];
        mx=max(mx,a[i]);
    }
    int sum=0;
    if(n==1)cout<<0<<endl;
    else
    {
        for(int i=1;i<=n;i++)
        {
            sum+=(mx-a[i]);
        }
        cout<<sum<<endl;
    }

    return 0;
}


#include <iostream>
#include <algorithm>
using namespace std;
int a[1005];
int n,sum;
int main() {
	cin >> n;
	for(int i = 1; i<=n;i++) {
		cin >> a[i];
	}
	sort(a+1,a+n+1);
//	for(int i = 2; i<=n;i++) {
//		if(a[i] > a[i-1]) {
//			sum++;
//		}
//	}
	for(int i = 1; i<n;i++) {
		sum+=a[n]-a[i];
	}
	cout << sum << endl;
	return 0;
}


#include<stdio.h>
#include<iostream>
#include<algorithm>
using namespace std;
int a[105];
int main()
{
    int n,maxn,res;
    while(scanf("%d",&n) == 1) {
        for(int i = 0; i < n; ++i) {
            scanf("%d",&a[i]);
        }
        maxn = *max_element(a,a+n);
        res  = 0;
        for(int i = 0; i < n; ++i) {
            res += (maxn-a[i]);
        }
        printf("%d\n",res);

    }
    return 0;
}


#include<iostream>
#include <bits/stdc++.h>
#define maxn 112345
#define en '\n'
#define ll long  long
//priority_queue<int>q;
using namespace std;
int n;
int a[maxn];
//int cnt[maxn];
int main(){
#ifdef local
    freopen("input.txt","r",stdin);
#endif // local
//    cin>>n;
//    for(int i=1;i<=n;i++){
//        int tem;
//        cin>>tem;
//        cnt[tem]+=1;
//        if(cnt[tem]==1)
//        q.push(tem)
//    }
//    int qian=
//
//    while(!q.empty()){
//         int tem=q.top();
//         q.pop();
//        if(tem<qian){
//            int tem2=q.top()
//
//        }
//
//
//    }

cin>>n;
int da=0;
for(int i=1;i<=n;i++){
    scanf("%d",&a[i]);
    da=max(a[i],da);
}
ll ans=0;
for(int  i=1;i<=n;i++)
{
    ans+=da-a[i];
}
cout<<ans<<en;

return 0;
}


#include<iostream>

using namespace std;

int d[105];
int main()
{
	long long n,ans = 0;
	cin>>n;
	long long  maxn = 0;
	for(int i=0;i<n;i++)
	{
		cin>>d[i];
		maxn = maxn>d[i]?maxn:d[i];
	}
	for(int i=0;i<n;i++)
	{
		ans += maxn - d[i];
	}
	cout<<ans;
	return 0;
}


#include <iostream>
#include <algorithm>
using namespace std;
int n,i,k[5000000],m;
main(){
    cin>>n;
    for (i=0; i<n; i++){
        cin>>k[i];
    }
    sort(k,k+n);
    reverse (k,k+n);
    for (int i=1; i<n; i++){
        if (k[i]!=k[i-1]) m=m+k[0]-k[i];
    }
    cout<<m;
}


#include <iostream>
#include <algorithm>
using namespace std;
int n,i,k[5000000],m;
main(){
    cin>>n;
    for (i=0; i<n; i++){
        cin>>k[i];
    }
    sort(k,k+n);
    reverse (k,k+n);
    for (int i=0; i<n; i++){
       m=m+(k[0]-k[i]);
    }
    cout<<m;
}


#include <iostream>
#include <algorithm>
using namespace std;
int n,i,k[5000000],m;
main(){
    cin>>n;
    for (i=0; i<n; i++){
        cin>>k[i];
    }
    sort(k,k+n);
    m=k[0];
    for (int i=1; i<n; i++){
        if (k[i]!=k[i-1]) m=m+k[i];
    }
    cout<<m;
}


#include <iostream>
using namespace std;
int n,i,k[5000000],m;
main(){
    cin>>n;
    for (i=0; i<n; i++){
        cin>>k[i];
    }
    sort(k,k+n);
    for (int i=1; i<n; i++){
        if (n[i]!=n[i-1]) m=m+n[i];
    }
    cout<<m;
}


#include <iostream>
#include <algorithm>
using namespace std;
int n,i,k[5000000],m;
main(){
    cin>>n;
    for (i=0; i<n; i++){
        cin>>k[i];
    }
    sort(k,k+n);
    for (int i=1; i<n; i++){
        if (k[i]!=k[i-1]) m=m+k[i];
    }
    cout<<m;
}


#include<cstdio>
#include<algorithm>
using namespace std;
int n,a[101],ans;
int main(){
	cin>>n;
	for(int i=1;i<=n;i++)cin>>a[i];
	sort(a+1,a+n+1);
	for(int i=1;i<n;i++){
		ans+=a[n]-a[i];
	}
	cout<<ans;
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
int n,a[101],ans;
int main(){
	cin>>n;
	for(int i=1;i<=n;i++)cin>>a[i];
	sort(a+1,a+n+1);
	for(int i=1;i<n;i++){
		ans+=a[n]-a[i];
	}
	cout<<ans;
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
int m,s,n,a;
int main(){
	cin>>n;
	for(int x=1;x<=n;x++){
	    cin>>a；
	    m=max(m,a)；
	    s+=a;
	}
	cout<<n*m-s;
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, i, sun = 0;
  scanf("%d", &n);
  int a[n];
  for(i = 0; i < n; i++) {
      sum += a[i];
  }
  printf("%d\n", a[n-1] * n = sum);
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
int main() {
    int n, i, sum = 0;
    scanf("%d", &n);
    int a[n];
    for(i = 0; i < n; i++) {
        scanf("%d", &a[i]);
    }
    sort(a, a+n);
    for(i = 0; i < n; i++) {
         sum += a[i];
    }
    printf("%d\n", a[n-1] * n = sum);
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
int main() {
    int n, i, sum = 0;
    scanf("%d", &n);
    int a[n];
    for(i = 0; i < n; i++) {
        scanf("%d", &a[i]);
    }
    sort(a, a+n);
    for(i = 0; i < n; i++) {
         sum += a[i];
    }
    printf("%d\n", a[n-1] * n - sum);
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
int main() {
    int n, i, sum = 0;
    scanf("%d", &n);
    int a[n];
    for(i = 0; i < n; i++) {
        sum += a[i];
    }
    printf("%d\n", a[n-1] * n = sum);
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
int main() {
    int n, i, sum = 0;
    scanf("%d", &n);
    int a[n];
    for(i = 0; i < n; i++) {
        sum += a[i];
    }
    printf("%d\n", a[n-1] * n - sum);
    return 0;
}


#include <iostream>
using namespace std;
int n,a,sum,maxa;
int main()
{
	cin>>n;
	for(register int i=1;i<=n;i++)
	{
		cin>>a;
		sum+=a;
		maxa=(a>maxa)?a:maxa;
	}
	cout<<(maxa*n-sum)<<endl;
}//a


#include <iostream>
using namespace std;
int n,a,sum,maxa;
int main()
{
	cin>>n;
	for(register int i=1;i<=n;i++)
	{
		cin>>a;
		sum+=a;
		maxa=(a>maxa)?a:maxa;
	}
	cout<<(maxa*n-sum)<<endl;
}


#include <iostream>
using namespace std;
int n,a,sum,maxa;
int main()
{
	cin>>n;
	for(register int i=1;i<=n;i++)
	{
		cin>>a;
		sum+=a;
		maxa=(a>maxa)?a:maxa;
	}
	cout<<(maxa*n-sum)<<endl;
}//b


#include <iostream>

using namespace std;

int main()
{
    cout << "Hello world!" << endl;
    return 0;
}


include <iostream>

using namespace std;

int main()
{
    int a[10000],n,k,i,sum=0;
    cin>>n;
    for(i=1;i<=n;i++){
        cin>>a[i];}
     int   Max=a[1];
    for(i=1;i<=n;i++){
        if(Max<a[i])Max=a[i];
        sum+=a[i];
    }
    cout<<Max*n-sum;

}


#include <iostream>

using namespace std;

int main()
{
long long  a[1000005];
int n,k,i;
cin>>n;
for(i=0;i<n;i++)
    cin>>a[i];
int Max=a[1];
for(i=0;i<n;i++){
    if(Max<a[i])Max=a[i];
    k=k+a[i];
    if(Max*n>k)cout<<Max*n-k;
}

}


#include <iostream>

using namespace std;

int main()
{
    int a[10000],n,k,i,sum=0;
    cin>>n;
    for(i=1;i<=n;i++){
        cin>>a[i];}
     int   Max=a[1];
    for(i=1;i<=n;i++){
        if(Max<a[i])Max=a[i];
        sum+=a[i];
    }
    cout<<Max*n-sum;

}


#include <iostream>

using namespace std;

int main()
{
    int a[105],n,i,sum=0;
    cin>>n;
    for(i=0;i<n;i++)
        cin>>a[i];
    int    Max=a[1];
    for(i=0;i<n;i++){
    sum+=a[i];
    if(Max<a[i])Max=a[i];}
    cout<<(Max*n)-sum;

}


#include<stdio.h>

int main()
{
    long long a=0, n, max=0, i ;

    scanf("%d", &n);
     long array[n];
    for(i=0;i<n;i++)
    {
        scanf("%I64d", &array[i]);
        if(array[i]>max)
            max=array[i];
    }

    for(i=0;i<n;i++)
    {
        a=a+(max-array[i]);
    }

    printf("%I64d\n", a);

    return 0;
}


/*

mostofa er code check kora  :(

*/

#include<stdio.h>

int main()
{
    long long a=0, n, max=0, i ;

    scanf("%I64d", &n);
    long long array[n];
    for(i=0;i<n;i++)
    {
        scanf("%I64d", &array[i]);
        if(array[i]>max)
            max=array[i];
    }

    for(i=0;i<n;i++)
    {
        a=a+(max-array[i]);
    }

    printf("%I64d\n", a);

    return 0;
}


#include <bits/stdc++.h>

using namespace std ;

int main()

{

    ios_base::sync_with_stdio(false) ;

    cin.tie(NULL) ;

    int n , sum = 0 ;

    cin >> n ;

    int ara[n] ;

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

    sort( ara , ara + n ) ;

    int max = ara[n-1] ;

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

        sum += ( max - ara[i] ) ;

    cout << sum << '\n' ;

    return 0 ;

}


#include <bits/stdc++.h>

using namespace std ;

int main()

{

    int n , high = -33 , sum = 0 ;

    cin >> n ;

    int ara[n] ;

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

        cin >> ara[i] ;

        if( ara[i] > high )  {

            high = ara[i] ;

        }

    }

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

        sum += ( high - ara[i] ) ;

    }

    cout << sum << endl ;

}


#include <bits/stdc++.h>
#define ll long long
using namespace std;
ll a[100000+9],l,m,n,t;
set<ll>s;
int main()
{
    cin>>t;
    for(int i=0;i<t;i++)
    {
        cin>>a[i];
    }
   sort(a,a+t);
   m=a[t-1];
   for(int i=0;i<t;i++)
   {
    l+=m-a[i];
   }
   cout<<l;
}


#include <bits/stdc++.h>
#define ll long long
using namespace std;
ll a[100000+9],l,m,n,t;
set<ll>s;
int main()
{
    cin>>t;
    if(t==1)return cout<<0,0;
    for(int i=0;i<t;i++)
    {
        int x;
        cin>>x;
        s.insert(x);
    }
    for(auto i=s.begin();i!=s.end();i++)
    {
        l+=*i;
    }
    cout<<l;

}


#include <bits/stdc++.h>
#define ll long long
using namespace std;
ll a[100000+9],l,m,n,t;
set<ll>s;
int main()
{
    cin>>t;
    for(int i=0;i<t;i++)
    {
        int x;
        cin>>x;
        s.insert(x);
    }
    if(s.size()==1)return cout<<0,0;
    for(auto i=s.begin();i!=s.end();i++)
    {
        l+=*i;
    }
    cout<<l;

}


#include <bits/stdc++.h>
#define ll long long
using namespace std;
ll a[100000+9],l,m,n,t;
set<ll>s;
int main()
{
    cin>>t;
    for(int i=0;i<t;i++)
    {
        int x;
        cin>>x;
        s.insert(x);
    }
    for(auto i=s.begin();i!=s.end();i++)
    {
        l+=*i;
    }
    cout<<l;

}


#include<bits/stdc++.h>
using namespace std;
const int MAXN = 100 + 5;
int n, a[MAXN], maxa, sum;
int main() {
	scanf("%d", &n);
	for(int i = 1; i <= n; i++) {
		scanf("%d", &a[i]);
		maxa = max(maxa, a[i]);
	}
	for(int i = 1; i <= n; i++) {
		sum += maxa - a[i];
	}
	printf("%d\n", sum);
	return 0;
}
//


#include<bits/stdc++.h>
using namespace std;
const int MAXN = 100 + 5;
int n, a[MAXN], maxa, sum;
int main() {
	scanf("%d", &n);
	for(int i = 1; i <= n; i++) {
		scanf("%d", &a[i]);
		maxa = max(maxa, a[i]);
	}
	for(int i = 1; i <= n; i++) {
		sum += maxa - a[i];
	}
	printf("%d\n", sum);
	return 0;
}
//
//
//
//
//


#include<bits/stdc++.h>
using namespace std;
const int MAXN = 100 + 5;
int n, a[MAXN], maxa, sum;
int main() {
	scanf("%d", &n);
	for(int i = 1; i <= n; i++) {
		scanf("%d", &a[i]);
		maxa = max(maxa, a[i]);
	}
	for(int i = 1; i <= n; i++) {
		sum += maxa - a[i];
	}
	printf("%d\n", sum);
	return 0;
}
//
//


#include<bits/stdc++.h>
using namespace std;
const int MAXN = 100 + 5;
int n, a[MAXN], maxa, sum;
int main() {
	scanf("%d", &n);
	for(int i = 1; i <= n; i++) {
		scanf("%d", &a[i]);
		maxa = max(maxa, a[i]);
	}
	for(int i = 1; i <= n; i++) {
		sum += maxa - a[i];
	}
	printf("%d\n", sum);
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
ll arr[200005];
/********************************************************************/
#define M1 1000000007	//Standard primes used in modulo operations
#define M2 998244353
#define ll long long
#define pll pair<ll,ll>
#define REP(i, a, b) for(ll i=a;i<b;i++)
#define REPR(i, a, b) for(ll i=b-1;i>=a;i--)
#define forr(i, n) for(ll i=0;i<n;i++)
#define ff first
#define ss second
#define pb push_back
#define ppb pop_back
#define mp make_pair
#define MT make_tuple
#define V(a) vector<a>
#define vi vector<ll>
#define endl '\n'
#define ce(ele) cout<<ele<<' '
#define cs(ele) cout<<ele<<'\n'
#define CASE(t) ll t; cin>>t; while(t--)
#define space ' '
#define yes cout<<"Yes"<<endl
#define no cout<<"No"<<endl

void FAST() {	//FAST Input Output, DONT use both scnaf,printf / cin,cout , Use any ONE system
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
}
ll gcd(ll x, ll y) {	//returns gcd of two long long ints
	if (x == 0) return y;
	return gcd(y % x, x);
}

ll powM(ll x, ll y, ll m) {	//returns (a^b)%m ( ^ is exponent )
	ll ans = 1, r = 1;
	x %= m;
	while (r > 0 && r <= y)
	{
		if (r & y)
		{
			ans *= x;
			ans %= m;
		}
		r <<= 1;
		x *= x;
		x %= m;
	}
	return ans;
}

map<long long, long long> factorize(long long n) {	//returns map containing factor and multiplicity, Eg: 60 = {{2,2},{3,1},{5,1}}
	map<long long, long long> ans;
	for (long long i = 2; i * i <= n; i++) {
		while (n % i == 0) {
			ans[i]++;
			n /= i;
		}
	}
	if (n > 1) {
		ans[n]++;
		n = 1;
	}
	return ans;
}
/* **********END OF TEMPLATE*********** */
int main() {
	FAST();
	CASE(t) {
		//Solve each testcase here
		ll n;
		cin >> n;

		for(ll i=0;i<n;i++)
		{
		    cin >> arr[i];
		}
		ll peak = 0;
		ll i=0;
		ll m = 0;
		ll sum = 0;
		for( i;i<n-1;i++)
		{
		    if(arr[i+1]<arr[i])
		    {
		        peak = i;
		        m = i;
		        break;
		    }
		}

		for(int j=i+1;j<n;j++)
		{
		    if(arr[j]>=arr[peak])
		    {
		        sum += arr[peak]-arr[m];
		        peak = j;
		        m = j;
		    }
		    else
		    {
		        if(arr[j]<arr[m])m = j;
		    }
		}
		sum += arr[peak]-arr[m];
		cout<<sum<<endl;
	}

	return 0;
}


#include <bits/stdc++.h>
using namespace std;

/********************************************************************/
#define M1 1000000007	//Standard primes used in modulo operations
#define M2 998244353
#define ll long long
#define pll pair<ll,ll>
#define REP(i, a, b) for(ll i=a;i<b;i++)
#define REPR(i, a, b) for(ll i=b-1;i>=a;i--)
#define forr(i, n) for(ll i=0;i<n;i++)
#define ff first
#define ss second
#define pb push_back
#define ppb pop_back
#define mp make_pair
#define MT make_tuple
#define V(a) vector<a>
#define vi vector<ll>
#define endl '\n'
#define ce(ele) cout<<ele<<' '
#define cs(ele) cout<<ele<<'\n'
#define CASE(t) ll t; cin>>t; while(t--)
#define space ' '
#define yes cout<<"Yes"<<endl
#define no cout<<"No"<<endl
ll arr[200005];

void FAST() {	//FAST Input Output, DONT use both scnaf,printf / cin,cout , Use any ONE system
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
}
ll gcd(ll x, ll y) {	//returns gcd of two long long ints
	if (x == 0) return y;
	return gcd(y % x, x);
}

ll powM(ll x, ll y, ll m) {	//returns (a^b)%m ( ^ is exponent )
	ll ans = 1, r = 1;
	x %= m;
	while (r > 0 && r <= y)
	{
		if (r & y)
		{
			ans *= x;
			ans %= m;
		}
		r <<= 1;
		x *= x;
		x %= m;
	}
	return ans;
}

map<long long, long long> factorize(long long n) {	//returns map containing factor and multiplicity, Eg: 60 = {{2,2},{3,1},{5,1}}
	map<long long, long long> ans;
	for (long long i = 2; i * i <= n; i++) {
		while (n % i == 0) {
			ans[i]++;
			n /= i;
		}
	}
	if (n > 1) {
		ans[n]++;
		n = 1;
	}
	return ans;
}
/* **********END OF TEMPLATE*********** */
int main() {
	FAST();
	CASE(t) {
		//Solve each testcase here
		ll n;
		cin >> n;

		for(ll i=0;i<n;i++)
		{
		    cin >> arr[i];
		}
		ll peak = 0;
		ll i=0;
		ll m = 0;
		ll sum = 0;
		for( i;i<n-1;i++)
		{
		    if(arr[i+1]<arr[i])
		    {
		        peak = i;
		        m = i;
		        break;
		    }
		}

		for(int j=i+1;j<n;j++)
		{
		    if(arr[j]>=arr[peak])
		    {
		        sum += arr[peak]-arr[m];
		        peak = j;
		        m = j;
		    }
		    else
		    {
		        if(arr[j]<arr[m])m = j;
		    }
		}
		sum += arr[peak]-arr[m];
		cout<<sum<<endl;
	}

	return 0;
}


#include <iostream>
#include <vector>
#include <chrono>
#include <random>
#include <cassert>

std::mt19937 rng((int) std::chrono::steady_clock::now().time_since_epoch().count());

int main() {
	std::ios_base::sync_with_stdio(false); std::cin.tie(NULL);
	int t;
	std::cin >> t;
	while(t--) {
		int n;
		std::cin >> n;
		long long last = 0;
		long long ans = 0;
		while(n--) {
			long long x;
			std::cin >> x;
			x += ans;
			if(x >= last) {
				last = x;
			} else {
				ans += last — x;
			}
		}
		std::cout << ans << '\n';
	}
}


#include <bits/stdc++.h>
using namespace std;

/********************************************************************/
#define M1 1000000007	//Standard primes used in modulo operations
#define M2 998244353
#define ll long long
#define pll pair<ll,ll>
#define REP(i, a, b) for(ll i=a;i<b;i++)
#define REPR(i, a, b) for(ll i=b-1;i>=a;i--)
#define forr(i, n) for(ll i=0;i<n;i++)
#define ff first
#define ss second
#define pb push_back
#define ppb pop_back
#define mp make_pair
#define MT make_tuple
#define V(a) vector<a>
#define vi vector<ll>
#define endl '\n'
#define ce(ele) cout<<ele<<' '
#define cs(ele) cout<<ele<<'\n'
#define CASE(t) ll t; cin>>t; while(t--)
#define space ' '
#define yes cout<<"Yes"<<endl
#define no cout<<"No"<<endl

void FAST() {	//FAST Input Output, DONT use both scnaf,printf / cin,cout , Use any ONE system
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
}
ll gcd(ll x, ll y) {	//returns gcd of two long long ints
	if (x == 0) return y;
	return gcd(y % x, x);
}

ll powM(ll x, ll y, ll m) {	//returns (a^b)%m ( ^ is exponent )
	ll ans = 1, r = 1;
	x %= m;
	while (r > 0 && r <= y)
	{
		if (r & y)
		{
			ans *= x;
			ans %= m;
		}
		r <<= 1;
		x *= x;
		x %= m;
	}
	return ans;
}

map<long long, long long> factorize(long long n) {	//returns map containing factor and multiplicity, Eg: 60 = {{2,2},{3,1},{5,1}}
	map<long long, long long> ans;
	for (long long i = 2; i * i <= n; i++) {
		while (n % i == 0) {
			ans[i]++;
			n /= i;
		}
	}
	if (n > 1) {
		ans[n]++;
		n = 1;
	}
	return ans;
}
/* **********END OF TEMPLATE*********** */
int main() {
	FAST();
	CASE(t) {
		//Solve each testcase here
		ll n;
		cin >> n;
		int arr[n];
		for(int i=0;i<n;i++)
		{
		    cin >> arr[i];
		}
		ll i =0;
		ll peak = 0;
		ll m = 0;
		ll sum = 0;
		for( i;i<n-1;i++)
		{
		    if(arr[i+1]<arr[i])
		    {
		        peak = i;
		        m = i;
		        break;
		    }
		}

		for(int j=i+1;j<n;j++)
		{
		    if(arr[j]>=arr[peak])
		    {
		        sum += arr[peak]-arr[m];
		        peak = j;
		        m = j;
		    }
		    else
		    {
		        if(arr[j]<arr[m])m = j;
		    }
		}
		sum += arr[peak]-arr[m];
		cout<<sum<<endl;
	}

	return 0;
}


#include <iostream>
#include <vector>
#include <chrono>
#include <random>
#include <cassert>
typedef long long ll;
using namespace std;

std::mt19937 rng((int) std::chrono::steady_clock::now().time_since_epoch().count());

int main() {
	std::ios_base::sync_with_stdio(false); std::cin.tie(NULL);
	int t;
	std::cin >> t;
	while(t--) {
		int n;
		std::cin >> n;
		long long ans = 0;
		ll arr[n];
		for(int i=0;i<n;i++)
		{
		    cin >> arr[i];
		}
		for(int i=0;i<n-1;i++)
		{
		    if(arr[i]-arr[i+1]>0)ans+=(arr[i]-arr[i+1]);
		}
		std::cout << ans << '\n';
	}
}


#include <bits/stdc++.h>
using namespace std;

/********************************************************************/
#define M1 1000000007	//Standard primes used in modulo operations
#define M2 998244353
#define ll long long
#define pll pair<ll,ll>
#define REP(i, a, b) for(ll i=a;i<b;i++)
#define REPR(i, a, b) for(ll i=b-1;i>=a;i--)
#define forr(i, n) for(ll i=0;i<n;i++)
#define ff first
#define ss second
#define pb push_back
#define ppb pop_back
#define mp make_pair
#define MT make_tuple
#define V(a) vector<a>
#define vi vector<ll>
#define endl '\n'
#define ce(ele) cout<<ele<<' '
#define cs(ele) cout<<ele<<'\n'
#define CASE(t) ll t; cin>>t; while(t--)
#define space ' '
#define yes cout<<"Yes"<<endl
#define no cout<<"No"<<endl

void FAST() {	//FAST Input Output, DONT use both scnaf,printf / cin,cout , Use any ONE system
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
}
ll gcd(ll x, ll y) {	//returns gcd of two long long ints
	if (x == 0) return y;
	return gcd(y % x, x);
}

ll powM(ll x, ll y, ll m) {	//returns (a^b)%m ( ^ is exponent )
	ll ans = 1, r = 1;
	x %= m;
	while (r > 0 && r <= y)
	{
		if (r & y)
		{
			ans *= x;
			ans %= m;
		}
		r <<= 1;
		x *= x;
		x %= m;
	}
	return ans;
}

map<long long, long long> factorize(long long n) {	//returns map containing factor and multiplicity, Eg: 60 = {{2,2},{3,1},{5,1}}
	map<long long, long long> ans;
	for (long long i = 2; i * i <= n; i++) {
		while (n % i == 0) {
			ans[i]++;
			n /= i;
		}
	}
	if (n > 1) {
		ans[n]++;
		n = 1;
	}
	return ans;
}
/* **********END OF TEMPLATE*********** */
int main() {
	FAST();
	CASE(t) {
		//Solve each testcase here
		ll n;
		cin >> n;
		int arr[n];
		for(int i=0;i<n;i++)
		{
		    cin >> arr[i];
		}
		ll peak = 0;
		int i=0;
		int m = 0;
		ll sum = 0;
		for( i;i<n-1;i++)
		{
		    if(arr[i+1]<arr[i])
		    {
		        peak = i;
		        m = i;
		        break;
		    }
		}

		for(int j=i+1;j<n;j++)
		{
		    if(arr[j]>=arr[peak])
		    {
		        sum += arr[peak]-arr[m];
		        peak = j;
		        m = j;
		    }
		    else
		    {
		        if(arr[j]<arr[m])m = j;
		    }
		}
		sum += arr[peak]-arr[m];
		cout<<sum<<endl;
	}

	return 0;
}


typedef long long int ll;
typedef unsigned long long int ull;
#define fast  ios_base::sync_with_stdio(false); cin.tie(NULL);
#define MOD 1000000007
#define M_PI           3.14159265358979323846  /* pi */
#define sec second
#define fir first
#define rep(i,n) for(int (i)=0;(i)<(n);(i)++)
#define repe(i,m,n) for(int (i)=(m);(i)<(n);(i)++)
#define entire(a) (a).begin(),(a).end()
#define pb push_back
#define pii pair<int,int>
#include<bits/stdc++.h>
#include<cmath>
using namespace std;
void solve()
{
	ll n;
	cin >> n;
	std::vector<ll> v(n);
	rep(i, n)cin >> v[i];

	ll Max = -1, res = 0;
	for (int i = 0; i < n - 1; i++) {
		Max = max(Max, v[i]);
		if (v[i] <= v[i + 1] && v[i] >= Max)
			continue;
		ll cnt = 0, Min = v[i];
		if (v[i] < Max && v[i]<=v[i+1]) {
			while (v[i] <= v[i + 1] && v[i] <= Max && i < n) {
				i++;
			}
			res += Max - Min;
		}else if(v[i] > v[i + 1] && v[i] <= Max){
			while (v[i] > v[i + 1] && v[i] <= Max && i < n) {
				i++;
			}
			res+=Max-v[i];
			// cout<<v[i]<<endl;
		}

		// cout << Max << " " << res << endl;

	}
	cout << res << endl;
}

int main() {
#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif
	fast
	int T = 1;
	cin >> T;
	while (T--)
	{
		solve();
	}
	return 0;
}


typedef long long int ll;
typedef unsigned long long int ull;
#define fast  ios_base::sync_with_stdio(false); cin.tie(NULL);
#define MOD 1000000007
#define M_PI           3.14159265358979323846  /* pi */
#define sec second
#define fir first
#define rep(i,n) for(int (i)=0;(i)<(n);(i)++)
#define repe(i,m,n) for(int (i)=(m);(i)<(n);(i)++)
#define entire(a) (a).begin(),(a).end()
#define pb push_back
#define pii pair<int,int>
#include<bits/stdc++.h>
#include<cmath>
using namespace std;
void solve()
{
	ll n;
	cin >> n;
	std::vector<ll> v(n);
	rep(i, n)cin >> v[i];

	ll Max = -1, res = 0;
	for (int i = 0; i < n ; i++) {
		Max = max(Max, v[i]);
		if (v[i] >= Max)
			continue;
		ll Min = v[i];
		bool flag=false;
		if (v[i] <= Max && v[i]<=v[i+1]) {
			while (v[i] <= v[i + 1] && v[i] <= Max && i < n-1) {
				i++;
			}
			res += Max - Min;
		}else if(v[i] >= v[i + 1] && v[i] <= Max){
			while (v[i] >= v[i + 1] && v[i] <= Max && i < n-1) {
				i++;
			}
			res+=Max-v[i];
			flag=true;
			// cout<<v[i]<<endl;
		}
		if(flag && i==n-1 && v[i-1]>v[i]){
			res-=v[i-1];
			res+=v[i];
			break;
		}

		// cout << Max << " " << res << endl;

	}
	cout << res << endl;
}

int main() {
#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif
	fast
	int T = 1;
	cin >> T;
	while (T--)
	{
		solve();
	}
	return 0;
}


typedef long long int ll;
typedef unsigned long long int ull;
#define fast  ios_base::sync_with_stdio(false); cin.tie(NULL);
#define MOD 1000000007
#define M_PI           3.14159265358979323846  /* pi */
#define sec second
#define fir first
#define rep(i,n) for(int (i)=0;(i)<(n);(i)++)
#define repe(i,m,n) for(int (i)=(m);(i)<(n);(i)++)
#define entire(a) (a).begin(),(a).end()
#define pb push_back
#define pii pair<int,int>
#include<bits/stdc++.h>
#include<cmath>
using namespace std;
void solve()
{
	ll n;
	cin >> n;
	std::vector<ll> v(n);
	rep(i, n)cin >> v[i];

	ll Max = -1, res = 0;
	for (int i = 0; i < n ; i++) {
		Max = max(Max, v[i]);
		if (v[i] >= Max)
			continue;
	ll Min=v[i];
	while(v[i]<Max && i<n){
		Min=min(Min,v[i]);
		i++;
	}
	i--;
	// cout<<Max<<" "<<Min<<endl;
	res+=Max-Min;
	}
	cout << res << endl;
}

int main() {
#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif
	fast
	int T = 1;
	cin >> T;
	while (T--)
	{
		solve();
	}
	return 0;
}


typedef long long int ll;
typedef unsigned long long int ull;
#define fast  ios_base::sync_with_stdio(false); cin.tie(NULL);
#define MOD 1000000007
#define M_PI           3.14159265358979323846  /* pi */
#define sec second
#define fir first
#define rep(i,n) for(int (i)=0;(i)<(n);(i)++)
#define repe(i,m,n) for(int (i)=(m);(i)<(n);(i)++)
#define entire(a) (a).begin(),(a).end()
#define pb push_back
#define pii pair<int,int>
#include<bits/stdc++.h>
#include<cmath>
using namespace std;
void solve()
{
  int n;
  cin>>n;
  std::vector<int> v(n);
  rep(i,n)cin>>v[i];
  ll ans=0;
  rep(i,n-1){
  	ans+=max(0,v[i]-v[i+1]);
  }
  cout<<ans<<endl;
}

int main() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  fast
  int T=1;
  cin >> T;
  while (T--)
  {
    solve();
  }
  return 0;
}


typedef long long int ll;
typedef unsigned long long int ull;
#define fast  ios_base::sync_with_stdio(false); cin.tie(NULL);
#define MOD 1000000007
#define M_PI           3.14159265358979323846  /* pi */
#define sec second
#define fir first
#define rep(i,n) for(int (i)=0;(i)<(n);(i)++)
#define repe(i,m,n) for(int (i)=(m);(i)<(n);(i)++)
#define entire(a) (a).begin(),(a).end()
#define pb push_back
#define pii pair<int,int>
#include<bits/stdc++.h>
#include<cmath>
using namespace std;
void solve()
{
	ll n;
	cin >> n;
	std::vector<ll> v(n);
	rep(i, n)cin >> v[i];

	ll Max = -1, res = 0;
	for (int i = 0; i < n ; i++) {
		Max = max(Max, v[i]);
		// cout<<i<<" ddfs "<<Max<<endl;
		if (v[i] >= Max)
			continue;
		ll Min = v[i];
		bool flag=false;
		if (v[i] <= Max && v[i]<=v[i+1]) {
			while (v[i] <= v[i + 1] && v[i] <= Max && v[i+1]<=Max && i < n-1) {
				i++;
			}
			res += Max - Min;
		}else if(v[i] >= v[i + 1] && v[i] <= Max){
			while (v[i] >= v[i + 1] && v[i] <= Max && i < n-1 && v[i+1]<=Max) {
				i++;
			}
			res+=Max-v[i];
			flag=true;
			// cout<<v[i]<<endl;
		}
		if(flag && i==n-1 && v[i-1]>v[i]){
			res-=v[i-1];
			res+=v[i];
			break;
		}

		// cout << Max << " " << res << endl;

	}
	cout << res << endl;
}

int main() {
#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif
	fast
	int T = 1;
	cin >> T;
	while (T--)
	{
		solve();
	}
	return 0;
}


typedef long long int ll;
typedef unsigned long long int ull;
#define fast  ios_base::sync_with_stdio(false); cin.tie(NULL);
#define MOD 1000000007
#define M_PI           3.14159265358979323846  /* pi */
#define sec second
#define fir first
#define rep(i,n) for(int (i)=0;(i)<(n);(i)++)
#define repe(i,m,n) for(int (i)=(m);(i)<(n);(i)++)
#define entire(a) (a).begin(),(a).end()
#define pb push_back
#define pii pair<int,int>
#include<bits/stdc++.h>
#include<cmath>
using namespace std;
void solve()
{
	ll n;
	cin >> n;
	std::vector<ll> v(n);
	rep(i, n)cin >> v[i];

	ll Max = -1, res = 0;
	for (int i = 0; i < n ; i++) {
		Max = max(Max, v[i]);
		if (v[i] >= Max)
			continue;
		if(i==n-1)
		{
			res+=(Max-v[i]);
			break;
		}
		ll Min = v[i];
		bool flag = false;
		if (v[i] < Max && v[i] < v[i + 1]) {
			while (v[i] < v[i + 1] && v[i] < Max && v[i + 1] < Max && i < n - 1) {
				i++;
			}
			res += Max - Min;
		} else if (v[i] > v[i + 1] && v[i] < Max) {
			while (v[i] > v[i + 1] && v[i] < Max && i < n - 1 && v[i + 1] < Max) {
				i++;
			}
			res += Max - v[i];
			flag = true;
			// cout<<v[i]<<endl;
		}
		// if (flag && i == n - 1 && v[i - 1] > v[i]) {
		// 	res -= (Max-v[i - 1]);
		// 	res += (Max-v[i]);
			// cout<<(Max-v[i])<<endl;
		// cout << Max << " " << res << endl;

		// 	break;
		// }

		// cout << Max << " " << res << endl;

	}
	cout << res << endl;
}

int main() {
#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif
	fast
	int T = 1;
	cin >> T;
	while (T--)
	{
		solve();
	}
	return 0;
}


typedef long long int ll;
typedef unsigned long long int ull;
#define fast  ios_base::sync_with_stdio(false); cin.tie(NULL);
#define MOD 1000000007
#define M_PI           3.14159265358979323846  /* pi */
#define sec second
#define fir first
#define rep(i,n) for(int (i)=0;(i)<(n);(i)++)
#define repe(i,m,n) for(int (i)=(m);(i)<(n);(i)++)
#define entire(a) (a).begin(),(a).end()
#define pb push_back
#define pii pair<int,int>
#include<bits/stdc++.h>
#include<cmath>
using namespace std;
void solve()
{
	ll n;
	cin >> n;
	std::vector<ll> v(n);
	rep(i, n)cin >> v[i];

	ll Max = -1, res = 0;
	for (int i = 0; i < n ; i++) {
		Max = max(Max, v[i]);
		if (v[i] >= Max)
			continue;
		ll cnt = 0, Min = v[i];
		if (v[i] < Max && v[i]<=v[i+1]) {
			while (v[i] <= v[i + 1] && v[i] <= Max && i < n-1) {
				i++;
			}
			res += Max - Min;
		}else if(v[i] > v[i + 1] && v[i] <= Max){
			while (v[i] > v[i + 1] && v[i] <= Max && i < n-1) {
				i++;
			}
			res+=Max-v[i];
			// cout<<v[i]<<endl;
		}

		// cout << Max << " " << res << endl;

	}
	cout << res << endl;
}

int main() {
#ifndef ONLINE_JUDGE
	freopen("input.txt", "r", stdin);
	freopen("output.txt", "w", stdout);
#endif
	fast
	int T = 1;
	cin >> T;
	while (T--)
	{
		solve();
	}
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
#define ll long long int
#define ff(i,a,b) for( ll i=a;i<b;i++)
#define tc() int t; cin>> t; while (t--)
#define MOD 1000000007
#define vec vector<ll>
#define pb push_back

int main()
{ios_base::sync_with_stdio(false);
    cin.tie(NULL);
tc()
{
	ll n;cin>>n;
	vec pk;
	ll o,x;
	ff(i,0,n)
	{
		cin>>o;
		if(x!=o)
		{
			pk.pb(o);
		}
		x=o;
	}

	ll ans=0;
	ff(i,1,pk.size())
	{
		if(is_sorted(pk.begin(), pk.end()))
		{
			break;
		}
		else if(pk.at(i-1)>pk.at(i))
		{
			ll y=i;
			ll b=pk.at(i);
			ff(j,i,pk.size())
			{
				if(pk.at(j)>b)
				{
					y=j-1;
					break;
				}
				y=j;
				b=min(b,pk.at(j));
			}
			ans+=pk.at(i-1)-pk.at(y);
			pk.at(y)=pk.at(i-1);
			i=y-1;
	// 			for(auto x:pk)
	// {
	// 	cout<<x<<" ";
	// }
	// cout<<"\n";
		}

	}

	cout<<ans<<"\n";
}
return 0;}
//PRATIK_SINGH!!
// cout<<"time taken : "<<(float)clock()/CLOCKS_PER_SEC<<" secs"<<endl;

// ░█████╗░░░░░░░░░░░░░░░
// ██╔══██╗░░██╗░░░░██╗░░
// ██║░░╚═╝██████╗██████╗
// ██║░░██╗╚═██╔═╝╚═██╔═╝
// ╚█████╔╝░░╚═╝░░░░╚═╝░░
// ░╚════╝░░░░░░░░░░░░░░░


#include<bits/stdc++.h>
using namespace std;
#define ll long long int
#define ff(i,a,b) for( ll i=a;i<b;i++)
#define tc() int t; cin>> t; while (t--)
#define MOD 1000000007
#define vec vector<ll>
#define pb push_back

int main()
{ios_base::sync_with_stdio(false);
    cin.tie(NULL);
tc()
{
	ll n;cin>>n;
	vec pk;
	ll o,x;
	ff(i,0,n)
	{
		cin>>o;
		if(x!=o)
		{
			pk.pb(o);
		}
		x=o;
	}

	ll ans=0;
	ff(i,1,pk.size())
	{
		if(is_sorted(pk.begin(), pk.end()))
		{
			break;
		}
		else if(pk.at(i-1)>pk.at(i))
		{
			ll y=i;
			ll b=pk.at(i);
			ff(j,i+1,pk.size())
			{
				if(pk.at(j)>b)
				{
					y=j-1;
					break;
				}
				b=min(b,pk.at(j));
			}
			ans+=pk.at(i-1)-pk.at(y);
			pk.at(y)=pk.at(i-1);
			i=y-1;
	// 			for(auto x:pk)
	// {
	// 	cout<<x<<" ";
	// }
	// cout<<"\n";
		}

	}

	cout<<ans<<"\n";
}
return 0;}
//PRATIK_SINGH!!
// cout<<"time taken : "<<(float)clock()/CLOCKS_PER_SEC<<" secs"<<endl;

// ░█████╗░░░░░░░░░░░░░░░
// ██╔══██╗░░██╗░░░░██╗░░
// ██║░░╚═╝██████╗██████╗
// ██║░░██╗╚═██╔═╝╚═██╔═╝
// ╚█████╔╝░░╚═╝░░░░╚═╝░░
// ░╚════╝░░░░░░░░░░░░░░░


#include<bits/stdc++.h>
using namespace std;
#define ll long long int
#define ff(i,a,b) for( ll i=a;i<b;i++)
#define tc() int t; cin>> t; while (t--)
#define MOD 1000000007
#define vec vector<ll>
#define pb push_back

int main()
{ios_base::sync_with_stdio(false);
    cin.tie(NULL);
tc()
{
	ll n;cin>>n;
	vec pk;
	ll o,x;
	ff(i,0,n)
	{
		cin>>o;
		if(x!=o)
		{
			pk.pb(o);
		}
		x=o;
	}

	ll ans=0;
	ff(i,1,pk.size())
	{
		if(is_sorted(pk.begin(), pk.end()))
		{
			break;
		}
		if(pk.at(i-1)>pk.at(i))
		{
			ll y=pk.size()-1;
			ff(a,i+1,pk.size())
			{
				if(pk.at(a)>=pk.at(i))
				{
					y=a;
					break;
				}
			}
		ll x=*min_element(pk.begin()+i, pk.begin()+y);
		ll q=min_element(pk.begin()+i, pk.begin()+y)- pk.begin();
		ans+=pk.at(i-1)-x;
		// cout<<x<<" "<<q<<" "<<ans<<" \n";
		pk.at(y)=pk.at(i-1);
		i=y;
		}

	}

	cout<<ans<<"\n";
}
return 0;}
//PRATIK_SINGH!!
// cout<<"time taken : "<<(float)clock()/CLOCKS_PER_SEC<<" secs"<<endl;

// ░█████╗░░░░░░░░░░░░░░░
// ██╔══██╗░░██╗░░░░██╗░░
// ██║░░╚═╝██████╗██████╗
// ██║░░██╗╚═██╔═╝╚═██╔═╝
// ╚█████╔╝░░╚═╝░░░░╚═╝░░
// ░╚════╝░░░░░░░░░░░░░░░


#include<bits/stdc++.h>
using namespace std;
#define ll long long int
#define ff(i,a,b) for( ll i=a;i<b;i++)
#define tc() int t; cin>> t; while (t--)
#define MOD 1000000007
#define vec vector<ll>
#define pb push_back

int main()
{ios_base::sync_with_stdio(false);
    cin.tie(NULL);
tc()
{
	ll n;cin>>n;
	vec pk;
	ll o,x;
	ff(i,0,n)
	{
		cin>>o;
		if(x!=o)
		{
			pk.pb(o);
		}
		x=o;
	}

	ll ans=0;
	ff(i,1,pk.size())
	{
		if(is_sorted(pk.begin(), pk.end()))
		{
			break;
		}
		if(pk.at(i-1)>pk.at(i))
		{
			ll y=pk.size()-1;
			ff(a,i+1,pk.size())
			{
				if(pk.at(a)>pk.at(i))
				{
					y=a;
					break;
				}
				if(pk.at(a)==pk.at(i))
				{
					y=a-1;
					break;
				}
			}
		ll x=*min_element(pk.begin()+i, pk.begin()+y+1);
		ll q=min_element(pk.begin()+i, pk.begin()+y+1)- pk.begin();
		ans+=pk.at(i-1)-x;
		// cout<<x<<" "<<q<<" "<<y<<" "<<ans<<" \n";
		pk.at(q)=pk.at(i-1);
		i=q;
		}

	}
	// for(auto x:pk)
	// {
	// 	cout<<x<<" ";
	// }
	// cout<<"\n";
	cout<<ans<<"\n";
}
return 0;}
//PRATIK_SINGH!!
// cout<<"time taken : "<<(float)clock()/CLOCKS_PER_SEC<<" secs"<<endl;

// ░█████╗░░░░░░░░░░░░░░░
// ██╔══██╗░░██╗░░░░██╗░░
// ██║░░╚═╝██████╗██████╗
// ██║░░██╗╚═██╔═╝╚═██╔═╝
// ╚█████╔╝░░╚═╝░░░░╚═╝░░
// ░╚════╝░░░░░░░░░░░░░░░