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

#define all(x) (x).begin(),(x).end()
#define pb push_back
#define pf push_front
#define sz(x) (int)(x).size()
#define lb lower_bound 
#define ub upper_bound 
#define mp make_pair
#define fi first
#define se second
#define setbits(x) __builtin_popcount(x)
#define zerobits(x) __builtin_ctz(x)
#define setbitsll(x) __builtin_popcountll(x)
#define zerobitsll(x) __builtin_ctzll(x)
#define ps(x,y) fixed<<setprecision(y)<<x

typedef vector<int> vi;
typedef long long ll;
typedef vector<ll> vl;
typedef pair<int,int> pii;
typedef unsigned long long ull;
typedef long double ld;
typedef map<int,int> mii;

const int MOD = 1e9+7;
const ld PI = acos((ld)-1);

int f (int x) {
	string strNo = to_string(x);
	string strInc = strNo,strDec = strNo;
	sort(all(strInc)),sort(all(strDec),greater<char>());
	while (!strInc.empty() && *strInc.begin() == '0') {
		strInc.erase(strInc.begin());
	}
	int big = stoi(strDec);
	int small = stoi(strInc);
	return big-small;
}

void solve () {
	int n,k;
	cin >> n >> k;
	int aPrev = n;
	for (int i = 0;i < k;++i) {
		int aNext = f(aPrev);
		if (aNext == 0) {
			cout << 0 << '\n';
			return;
		}
		aPrev = aNext;
	}
	cout << aPrev << '\n';
}

int main () {

#ifndef ONLINE_JUDGE
	freopen("input1.txt","r",stdin);
	freopen("output1.txt","w",stdout);
#endif

	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	solve();
	return 0;
}

	
#include<bits/stdc++.h>
#define LL long long
#define pb push_back
#define SZ(x) ((int)x.size()-1)
#define ms(a,b) memset(a,b,sizeof a)
#define F(i,a,b) for (int i=(a);i<=(b);++i)
#define DF(i,a,b) for (int i=(a);i>=(b);--i)
using namespace std;
inline int read(){
	char ch=getchar(); int w=1,c=0;
	for(;!isdigit(ch);ch=getchar()) if (ch=='-') w=-1;
	for(;isdigit(ch);ch=getchar()) c=(c<<1)+(c<<3)+(ch^48);
	return w*c;
}
const int M=5e5+10;
int n,a[M],b[M]; 
int main(){
	n=read();
	F(i,1,n) a[i]=read();
	F(i,1,n) b[i]=read();
	int m1=1,m2=1;
	LL ans=0;
	F(i,1,n){
		m1=max(m1,a[i]);
		ans=max(ans,1ll*b[i]*m1);
		cout<<ans<<"\n";
	}
	return 0;
}
/* stuff you should look for
	* int overflow, array bounds
	* special cases (n=1?)
	* do smth instead of nothing and stay organized
	* WRITE STUFF DOWN
	* DON'T GET STUCK ON ONE APPROACH
*/
