#include <iostream>
#include <sstream>
#include <iomanip>
#include <vector>
#include <string>
#include <map>
#include <set>
#include <algorithm>
#include <numeric>
#include <iterator>
#include <functional>
#include <complex>
#include <queue>
#include <stack>
#include <cmath>
#include <cassert>
#include <tuple>
using namespace std;
typedef long long LL;
typedef complex<double> CMP;

class AlienAndHamburgers { public:
	int getNumber(vector <int> type, vector <int> taste)
	{
		const int N = type.size();

		vector<pair<int,int>> taste_type;
		for(int i=0; i<N; ++i)
			taste_type.emplace_back(taste[i], type[i]);
		sort(taste_type.rbegin(), taste_type.rend());

		int best = 0;

		set<int> Y;
		int A = 0;
		for(auto& tt: taste_type) {
			int ta = tt.first;
			int ty = tt.second;
			if(ta >= 0 || !Y.count(ty)) {
				Y.insert(ty);
				A += ta;
			}
			best = max<int>(best, Y.size()*A);
		}

		return best;
	}
};

// BEGIN CUT HERE
#include <ctime>
double start_time; string timer()
 { ostringstream os; os << " (" << int((clock()-start_time)/CLOCKS_PER_SEC*1000) << " msec)"; return os.str(); }
template<typename T> ostream& operator<<(ostream& os, const vector<T>& v)
 { os << "{ ";
   for(typename vector<T>::const_iterator it=v.begin(); it!=v.end(); ++it)
   os << '\"' << *it << '\"' << (it+1==v.end() ? "" : ", "); os << " }"; return os; }
void verify_case(const int& Expected, const int& Received) {
 bool ok = (Expected == Received);
 if(ok) cerr << "PASSED" << timer() << endl;  else { cerr << "FAILED" << timer() << endl;
 cerr << "\to: \"" << Expected << '\"' << endl << "\tx: \"" << Received << '\"' << endl; } }
#define CASE(N) {cerr << "Test Case #" << N << "..." << flush; start_time=clock();
#define END	 verify_case(_, AlienAndHamburgers().getNumber(type, taste));}
int main(){

CASE(0)
	int type_[] = {1, 2};
	  vector <int> type(type_, type_+sizeof(type_)/sizeof(*type_)); 
	int taste_[] = {4, 7};
	  vector <int> taste(taste_, taste_+sizeof(taste_)/sizeof(*taste_)); 
	int _ = 22; 
END
CASE(1)
	int type_[] = {1, 1};
	  vector <int> type(type_, type_+sizeof(type_)/sizeof(*type_)); 
	int taste_[] = {-1, -1};
	  vector <int> taste(taste_, taste_+sizeof(taste_)/sizeof(*taste_)); 
	int _ = 0; 
END
CASE(2)
	int type_[] = {1, 2, 3};
	  vector <int> type(type_, type_+sizeof(type_)/sizeof(*type_)); 
	int taste_[] = {7, 4, -1};
	  vector <int> taste(taste_, taste_+sizeof(taste_)/sizeof(*taste_)); 
	int _ = 30; 
END
CASE(3)
	int type_[] = {1, 2, 3, 2, 3, 1, 3, 2, 3, 1, 1, 1};
	  vector <int> type(type_, type_+sizeof(type_)/sizeof(*type_)); 
	int taste_[] = {1, 7, -2, 3, -4, -1, 3, 1, 3, -5, -1, 0};
	  vector <int> taste(taste_, taste_+sizeof(taste_)/sizeof(*taste_)); 
	int _ = 54; 
END
CASE(4)
	int type_[] = {30, 20, 10};
	  vector <int> type(type_, type_+sizeof(type_)/sizeof(*type_)); 
	int taste_[] = {100000, -100000, 100000};
	  vector <int> taste(taste_, taste_+sizeof(taste_)/sizeof(*taste_)); 
	int _ = 400000; 
END
CASE(5)
	int type_[] = {1,2,4,4,3};
	  vector <int> type(type_, type_+sizeof(type_)/sizeof(*type_)); 
	  int taste_[] = {100,100,-1,-1,-1};
	  vector <int> taste(taste_, taste_+sizeof(taste_)/sizeof(*taste_)); 
	int _ = 792; 
END
/*
CASE(6)
	int type_[] = ;
	  vector <int> type(type_, type_+sizeof(type_)/sizeof(*type_)); 
	int taste_[] = ;
	  vector <int> taste(taste_, taste_+sizeof(taste_)/sizeof(*taste_)); 
	int _ = ; 
END
*/
}
// END CUT HERE
