stablegradients/pie-gem5-bysrc huggingface dataset:
// Original C++ solution (slower):
#include<bits/stdc++.h>

using namespace std;







int main()



{

    long long int x=0,y=0,a;

    int n,i;

    scanf("%d",&n);

    long long int ara[n];

    for(i=0;i<n;i++) scanf("%lld",&ara[i]);



    sort(ara,ara+n);



    for(i=n-1;i>=1;i--){

        if(ara[i]==ara[i-1]){

            x=ara[i];

            break;

        }

    }

    i=i-2;

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

        if(ara[i]==ara[i-1]){

            y=ara[i];

            break;

        }

    }

    if(x==0||y==0){

        printf("0");

        return 0;

    }

    else{

        a=x*y;

        printf("%lld",a);

    }







}


// Optimized solution to the same problem (faster):
//h

#include<stdio.h>

#include<algorithm>

typedef long long ll;



int n;

ll a[100005];



bool cmp(const ll &a, const ll &b)

{

	return a>b;

}



int main()

{

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

	int i;

	ll l=0ll, w=0ll;

	scanf("%d", &n);

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

	{

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

	}

	std::sort(a, a+n, cmp);

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

	{

		if(a[i-1]==a[i])

		{

			l=a[i];

			break;

		}

	}

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

	{

		if(a[i-1]==a[i])

		{

			w=a[i];

			break;

		}

	}

	printf("%lld\n", l*w);

	return 0;

}

// Fastest known solution for this problem:
// Original C++ solution (slower):


#include<bits/stdc++.h>

using namespace std;



long long ar[100060];



int main()

{



    int n,j,i,cnt=0;

    long long area=1;



    scanf("%d",&n);



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

    {

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

    }



    sort(ar,ar+n);



    j=n-1;



    while(cnt<2 && j>0)

    {

        if(ar[j]==ar[j-1])

        {

            cnt++;

            area*=ar[j];

            j-=2;

        }



        else j--;

    }



    if(cnt<2)

    {

        printf("0");

    }



    else(printf("%lld",area));



    return 0;



}


// Optimized solution to the same problem (faster):
//h

#include<stdio.h>

#include<algorithm>

typedef long long ll;



int n;

ll a[100005];



bool cmp(const ll &a, const ll &b)

{

	return a>b;

}



int main()

{

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

	int i;

	ll l=0ll, w=0ll;

	scanf("%d", &n);

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

	{

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

	}

	std::sort(a, a+n, cmp);

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

	{

		if(a[i-1]==a[i])

		{

			l=a[i];

			break;

		}

	}

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

	{

		if(a[i-1]==a[i])

		{

			w=a[i];

			break;

		}

	}

	printf("%lld\n", l*w);

	return 0;

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#include<iostream>

#include<cstdio>

#include <cstring>

#include <iomanip>

#include <queue>

#include <map>



using namespace std;



map <int,int> ans;



int main(){

    priority_queue<int> res;

    int n;

    scanf("%d",&n);

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

        int k;

        scanf("%d",&k);

        ans[k]++;

        if (ans[k] == 2){

            res.push(k);

        }

        else if(ans[k] == 4){

            res.push(k);

        }

    }

    long long result = 1;

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

        if (res.empty()){

            printf("0\n");

            return 0;

        }

        else{

            result = result*res.top();

            res.pop();

        }

    }

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

    return 0;

}


// Optimized solution to the same problem (faster):
//h

#include<stdio.h>

#include<algorithm>

typedef long long ll;



int n;

ll a[100005];



bool cmp(const ll &a, const ll &b)

{

	return a>b;

}



int main()

{

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

	int i;

	ll l=0ll, w=0ll;

	scanf("%d", &n);

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

	{

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

	}

	std::sort(a, a+n, cmp);

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

	{

		if(a[i-1]==a[i])

		{

			l=a[i];

			break;

		}

	}

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

	{

		if(a[i-1]==a[i])

		{

			w=a[i];

			break;

		}

	}

	printf("%lld\n", l*w);

	return 0;

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#include <iostream>

#include <algorithm>

using namespace std;

const int maxn = 1e6+5;

long long arr[maxn];

bool com(int a,int b){

    return a>b;

}

int main(){

    int a,b,c,i,n;

    cin>>n;

    long long s;

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

        cin>>arr[i];

    }

    sort(arr,arr+n,com);

    int j=-1,k=-1;

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

        if(j!=-1&&arr[i]==arr[i+1]){

            k = i;

        }

        if(arr[i]==arr[i+1]&&k==-1){

            j = i;

            i++;

        }

        if(j!=-1&&k!=-1){

            break;

        }

    }

    if(j==-1||k==-1){

        cout<<0;

    }

    else{

        s = arr[j]*arr[k];

        cout<<s;

    }

}


// Optimized solution to the same problem (faster):
//h

#include<stdio.h>

#include<algorithm>

typedef long long ll;



int n;

ll a[100005];



bool cmp(const ll &a, const ll &b)

{

	return a>b;

}



int main()

{

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

	int i;

	ll l=0ll, w=0ll;

	scanf("%d", &n);

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

	{

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

	}

	std::sort(a, a+n, cmp);

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

	{

		if(a[i-1]==a[i])

		{

			l=a[i];

			break;

		}

	}

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

	{

		if(a[i-1]==a[i])

		{

			w=a[i];

			break;

		}

	}

	printf("%lld\n", l*w);

	return 0;

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#include<bits/stdc++.h>

using namespace std;



int main()

{

	int n;

	cin>>n;

	map<int,int> hm;

	vector<int> ans;

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

	{

		int a;

		cin>>a;

		hm[a]++;

		if(hm[a]==2)

		{

			ans.push_back(a);

			hm[a] = 0;

		}

	}

	sort(ans.begin(),ans.end());

	if(ans.size()<2) cout<<0;

	else

		cout<<1LL*ans[ans.size()-1]*ans[ans.size()-2];

	return 0;

}

// Optimized solution to the same problem (faster):
//h

#include<stdio.h>

#include<algorithm>

typedef long long ll;



int n;

ll a[100005];



bool cmp(const ll &a, const ll &b)

{

	return a>b;

}



int main()

{

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

	int i;

	ll l=0ll, w=0ll;

	scanf("%d", &n);

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

	{

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

	}

	std::sort(a, a+n, cmp);

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

	{

		if(a[i-1]==a[i])

		{

			l=a[i];

			break;

		}

	}

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

	{

		if(a[i-1]==a[i])

		{

			w=a[i];

			break;

		}

	}

	printf("%lld\n", l*w);

	return 0;

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#include <iostream>

#include <cmath>

#include <stdio.h>

#include <algorithm>

#include <string>

#include <vector>

#include <queue>

#include <deque>

#include <stack>

#include <map>

#include <set>

#include <cstring>



using namespace std;



#define f first

#define s second

#define pb push_back

#define mp make_pair

#define ll long long

#define pii pair < int, int >

#define pll pair < long long, long long>

#define ull unsigned long long

#define vi vector < int >

#define vll vector < long long >

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

#define forit(it, s) for(__typeof(s.begin()) it = s.begin(); it != s.end(); it++)

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

#define sqr(x) ((x) * (x))

#define fname "searching"



const int inf = (int)1e9;

const ll INF = (ll)1e18;

const double eps = 1e-9;

const double pi = acos(-1.0);

const int mod = (int)1e9 + 7;



int n;

ll a[100100];



ll solve(){

    cin >> n;

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

    sort(a, a + n);

    

    ll x = 0, cx = 0;

    ll y = 0, cy = 0;

    ll cnt = 2;

    for(int i = n - 1; i >= 0; --i){

        if(a[i] == x) cx++;

        else if(a[i] == y) cy++;



        if(cx >= 2 * cnt) return x * x;

        if(cx >= cnt && cy >= cnt){

            return x * y;

        }

        

        if(a[i] != x && cx < cnt){

            x = a[i];

            cx = 1;

        }else if(a[i] != y && cy < cnt){

            y = a[i];

            cy = 1;

        }

    }

    return 0;

}



int main(){

    ios_base::sync_with_stdio(0);

    cin.tie(0);



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

    

    return 0;

}


// Optimized solution to the same problem (faster):
//h

#include<stdio.h>

#include<algorithm>

typedef long long ll;



int n;

ll a[100005];



bool cmp(const ll &a, const ll &b)

{

	return a>b;

}



int main()

{

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

	int i;

	ll l=0ll, w=0ll;

	scanf("%d", &n);

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

	{

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

	}

	std::sort(a, a+n, cmp);

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

	{

		if(a[i-1]==a[i])

		{

			l=a[i];

			break;

		}

	}

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

	{

		if(a[i-1]==a[i])

		{

			w=a[i];

			break;

		}

	}

	printf("%lld\n", l*w);

	return 0;

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#include<bits/stdc++.h>

#define LLI long long int

#define ULTRA 2134567890

#define de(x) if(x!=0 && MODE==x)

#define MODE 1

using namespace std;



int A[100007];



int main()

{

    int i,j;

    int N;



    scanf("%d", &N);

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

    {

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

    }

    sort( A, A+N, greater<int>());



    LLI h = 0;

    LLI w = 0;

    for( i=0; i<N-1; i++)

    {

        if( A[i]==A[i+1])

        {

            if( h==0)

            {

                h = A[i];

                i++;

            }

            else if( w==0)

            {

                w = A[i];

                break;

            }

        }

    }

    printf("%lld\n", h*w);



    return 0;

}


// Optimized solution to the same problem (faster):
//h

#include<stdio.h>

#include<algorithm>

typedef long long ll;



int n;

ll a[100005];



bool cmp(const ll &a, const ll &b)

{

	return a>b;

}



int main()

{

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

	int i;

	ll l=0ll, w=0ll;

	scanf("%d", &n);

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

	{

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

	}

	std::sort(a, a+n, cmp);

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

	{

		if(a[i-1]==a[i])

		{

			l=a[i];

			break;

		}

	}

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

	{

		if(a[i-1]==a[i])

		{

			w=a[i];

			break;

		}

	}

	printf("%lld\n", l*w);

	return 0;

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
/*

--------------              |   /

      |                     |  /

      |                     | /

      |             *       |/          |    |         ------            *

      |                     |           |    |        /      \

      |             |       |\          |    |       |       |\          |

   \  |             |       | \         |    |       |       | \         |

    \ |             |       |  \        |    |        \     /   \        |

     V              |       |   \        \__/|         -----     \       |

*/

#include <bits/stdc++.h>

using namespace std;



#define EmiliaMyWife ios::sync_with_stdio(0); cin.tie(NULL);

#define endl '\n'

#define mem(i,j) memset(i,j,sizeof i);

#define F first

#define S second

#define pb push_back

#define mp make_pair

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

#define bit(s,i) (((s)>>(i))&1LL)

#define lowbit(x) (x&-x)

#define siz(v) (long long)v.size()

typedef long long ll;

typedef unsigned long long ull;

typedef pair<int,int> pii;

typedef pair<ll, ll> pll;

const double EPS  = 1e-8;

const int INF     = 0x3F3F3F3F;

const ll LINF     = 4611686018427387903;

const int MOD     = 1e9+7;

const int MAXN    = 2e5+9;

/*-----------------------------------------------------------------------------------------------------*/



int main() {

    EmiliaMyWife



    int n, a;

    cin >> n;

    vector<int> can;

    map<int, int> now;

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

        cin >> a;

        now[a]++;

        if(now[a]>1) {

            can.pb(a);

            now[a]=0;

        }

    }

    if(can.size()>1) {

        sort(all(can), greater<int>());

        cout << 1LL*can[0]*can[1];

    }

    else cout << 0;



	return 0;

}


// Optimized solution to the same problem (faster):
//h

#include<stdio.h>

#include<algorithm>

typedef long long ll;



int n;

ll a[100005];



bool cmp(const ll &a, const ll &b)

{

	return a>b;

}



int main()

{

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

	int i;

	ll l=0ll, w=0ll;

	scanf("%d", &n);

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

	{

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

	}

	std::sort(a, a+n, cmp);

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

	{

		if(a[i-1]==a[i])

		{

			l=a[i];

			break;

		}

	}

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

	{

		if(a[i-1]==a[i])

		{

			w=a[i];

			break;

		}

	}

	printf("%lld\n", l*w);

	return 0;

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#include<bits/stdc++.h>

#define int long long



using namespace std;



signed main()

{

    ios_base::sync_with_stdio(false);

    cin.tie(0);



    int n, arr[100000];

    cin >> n;

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

        cin >> arr[i];

    sort(arr, arr + n, greater<int>());



    int s = -1;

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

        if (arr[i] == arr[i + 1])

            if (s == -1)

                s = arr[i], i++;

            else

                return cout << s * arr[i] << '\n', 0;

    cout << "0\n";



    return 0;

}


// Optimized solution to the same problem (faster):
//h

#include<stdio.h>

#include<algorithm>

typedef long long ll;



int n;

ll a[100005];



bool cmp(const ll &a, const ll &b)

{

	return a>b;

}



int main()

{

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

	int i;

	ll l=0ll, w=0ll;

	scanf("%d", &n);

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

	{

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

	}

	std::sort(a, a+n, cmp);

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

	{

		if(a[i-1]==a[i])

		{

			l=a[i];

			break;

		}

	}

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

	{

		if(a[i-1]==a[i])

		{

			w=a[i];

			break;

		}

	}

	printf("%lld\n", l*w);

	return 0;

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#include <bits/stdc++.h>

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

using namespace std;



int main(){

//	IOS;

	int n,inp;

	cin >> n;

	map<int,int> num;

	vector<int> can;

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

		cin >> inp;

		num[inp]++;

		if(num[inp]%2==0) can.push_back(inp);

	}

	sort(can.begin(),can.end(),greater<int>());

	if(can.size()<2) cout << "0\n";

	else cout << (long long int)can[0]*can[1] << '\n';

	return 0;

}


// Optimized solution to the same problem (faster):
//h

#include<stdio.h>

#include<algorithm>

typedef long long ll;



int n;

ll a[100005];



bool cmp(const ll &a, const ll &b)

{

	return a>b;

}



int main()

{

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

	int i;

	ll l=0ll, w=0ll;

	scanf("%d", &n);

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

	{

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

	}

	std::sort(a, a+n, cmp);

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

	{

		if(a[i-1]==a[i])

		{

			l=a[i];

			break;

		}

	}

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

	{

		if(a[i-1]==a[i])

		{

			w=a[i];

			break;

		}

	}

	printf("%lld\n", l*w);

	return 0;

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#include<iostream>

#include<vector>

#include<map>

#include<algorithm>

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

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

#define pb push_back

#define mp make_pair

#define F first

#define S second

#define eb emplace_back

#define int long long

#define MOD 1000000007

#define PI 2*acos(0)

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

using namespace std;

int32_t main() {

	ios::sync_with_stdio(0); cin.tie(0);

	int n, a; cin >> n;

	map<int, int> c;

	vector<int> v;

	forn(i, n) {

		cin >> a;

		c[a]++;

		if (c[a] == 2)v.pb(a);

	}

	sort(allr(v));

	if (v.size() && c[v[0]] >= 4)

		cout << v[0] * v[0] << '\n';

	else if (v.size() > 1)

		cout << v[0] * v[1] << '\n';

	else

		cout << "0\n";	

	return 0;

}


// Optimized solution to the same problem (faster):
//h

#include<stdio.h>

#include<algorithm>

typedef long long ll;



int n;

ll a[100005];



bool cmp(const ll &a, const ll &b)

{

	return a>b;

}



int main()

{

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

	int i;

	ll l=0ll, w=0ll;

	scanf("%d", &n);

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

	{

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

	}

	std::sort(a, a+n, cmp);

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

	{

		if(a[i-1]==a[i])

		{

			l=a[i];

			break;

		}

	}

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

	{

		if(a[i-1]==a[i])

		{

			w=a[i];

			break;

		}

	}

	printf("%lld\n", l*w);

	return 0;

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#include <iostream>

#include <set>

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

using namespace std;



int main()

{

    BurstStream

    int n;

    while (cin >> n)

    {

        long long mxside[] = {0, 0};

        set<long long> s;



        while (n--)

        {

            long long temp;

            cin >> temp;



            if (s.find(temp) == s.end()) s.insert(temp);

            else

            {

                s.erase(temp);

                if (temp > mxside[0])

                {

                    mxside[1] = mxside[0];

                    mxside[0] = temp;

                }

                else if (temp > mxside[1]) mxside[1] = temp;

            }

        }

        cout << mxside[0] * mxside[1] << '\n';

    }

    return 0;

}


// Optimized solution to the same problem (faster):
#include<stdio.h>

#include<vector>

#include<set>

#include<algorithm>

using namespace std;

set<long long>k;

vector<long long>t;

int main()

{

    long long a,h,siz,p,q;

    scanf("%lld",&a);

    for(long long i=0;i<a;i++)

    {

        siz=k.size();

        scanf("%lld",&h);

        k.insert(h);

        if(k.size()==siz)

        {

            t.push_back(h);

        }

    }

    sort(t.begin(),t.end());

    p=count(t.begin(),t.end(),t[t.size()-1]);

    if(p>=3)

    {

    printf("%lld",t[t.size()-1]*t[t.size()-1]);

    }

    else if(t.size()>=2)

    {

    printf("%lld",t[t.size()-1]*(t[t.size()-p-1]));

    }

    else printf("0");

    return 0;

}




// Fastest known solution for this problem:
// Original C++ solution (slower):
  #include <map>

    #include <set>

    #include <cmath>

    #include <queue>

    #include <stack>

    #include <cstdio>

    #include <string>

    #include <vector>

    #include <cstdlib>

    #include <cstring>

    #include <sstream>

    #include <iostream>

    #include <algorithm>

    #include <functional>

    using namespace std;

    #define MX 100000

    #define ll long long

    bool cmd(ll a,ll b){

    	return a>b;

    }

    int main(){

    	ll	 N,A[110000];

    	cin>>N;

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

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

    	}

    	sort(A,A+N,cmd);

    	int flag=0;

    	int n=0;

    	ll tp1,tp2,sum;

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

    		if(A[i]==A[i+1])

    		{

    			i++;

    			n++;

    			if(n==1)

    			tp1=A[i];

    			else{

    				sum=tp1*A[i];

    				cout<<sum;

    				return 0;

    			}

    		}

    	}

    	cout<<"0";

    	return 0;

    }

// Optimized solution to the same problem (faster):
#include<stdio.h>

#include<string.h>

#include<stdlib.h>

#include<algorithm>

#include<math.h>

#define maxn 100005

const double INF=0xfffff;

using namespace std;

long long num[maxn];

int main()

{

  int n;

  long long flag1,flag2,flag3;

  int flag;

  while(scanf("%d",&n)!=EOF)

  {

  	flag1=-1;

  	flag2=-1;

  	flag=0;

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

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

    sort(num,num+n);

    for(int i=n-2;i>=0;i--)

    {

    	if(num[i]==num[i+1]&&flag1==-1)

    		{

    			flag1=num[i];

    			flag2=i;

    		}

    	else if(num[i]==num[i+1]&&i<flag2-1)

    		{

    			flag=1;

    			flag3=num[i];

    			break;

    		}

    		

    }

    if(flag==0)

    	printf("0\n");

    	else

    		printf("%lld\n",flag1*flag3);

  }

   return 0;

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#include <bits/stdc++.h>



// dp[i][500yen][num_1000yen] := remain ([i] is deleted)

int dp[128][1024];

int n;

int P[128];



void init() {

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

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

      dp[j][k] = -1;

    }

  }

}



int main() {

  for(;;) {

    init();

    scanf("%d", &n);

    if( n == 0 ) break;

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

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

    }

    dp[0][0] = 0;

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

      for(int j = n-1; j >= 0; --j) {

        for(int k = 1024 - 1; k >= 0; --k) {

          if( dp[j][k] == -1 ) continue;

          int n1kyen = (P[i] + 999) / 1000;

          int rem = dp[j][k] + n1kyen * 1000 - P[i];

          int next_num500yen = j + (rem >= 500 ? 1 : 0);

          int next_num1kyen  = k + n1kyen;

          int next_rem       = rem >= 500 ? rem - 500 : rem;

          int& next = dp[next_num500yen][next_num1kyen];

          next = std::max(next, next_rem);

        }

      }

    }    

    int res = (1 << 28);

    int max = -1;

    for(int j = n; j >= 0; --j) {

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

        if( dp[j][k] == -1 ) continue;

        int cost = k * 1000 - 500 * j - dp[j][k];

        res = std::min(res, cost);

      }

      if( res != (1 << 28) ) {

        max = j;

        break;

      }

    }

    printf("%d %d\n", max, res);

  }



  return 0;

}

// Optimized solution to the same problem (faster):
#include <bits/stdc++.h>



// dp[i][500yen][num_1000yen] := remain ([i] is deleted)

int dp[128][512];

int n;

int P[128];



void init() {

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

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

      dp[j][k] = -1;

    }

  }

}



int main() {

  for(;;) {

    init();

    scanf("%d", &n);

    if( n == 0 ) break;

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

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

    }

    int k_max = 0;

    dp[0][0] = 0;

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

      k_max += (P[i] + 999) / 1000;

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

        for(int k = k_max; k >= 0; --k) {

          if( dp[j][k] == -1 ) continue;

          int n1kyen = (P[i] + 999) / 1000;

          int rem = dp[j][k] + n1kyen * 1000 - P[i];

          int next_num500yen = j + (rem >= 500 ? 1 : 0);

          int next_num1kyen  = k + n1kyen;

          int next_rem       = rem >= 500 ? rem - 500 : rem;

          int& next = dp[next_num500yen][next_num1kyen];

          next = std::max(next, next_rem);

        }

      }

    }    

    int res = (1 << 28);

    int max = -1;

    for(int j = n; j >= 0; --j) {

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

        if( dp[j][k] == -1 ) continue;

        int cost = k * 1000 - 500 * j - dp[j][k];

        res = std::min(res, cost);

      }

      if( res != (1 << 28) ) {

        max = j;

        break;

      }

    }

    printf("%d %d\n", max, res);

  }



  return 0;

}

// Fastest known solution for this problem: