USACO:Humble Numbers

/*
ID: Jang Lawrence
PROG: humble
LANG: C++
*/
#include<iostream>
#include<cstring>
#include<cstdio>
#include<string>
#include<algorithm>
#include<queue>
#include<set>
#include<cmath>
#include<climits>
#define X first
#define Y second
#define sqr(a) ((a)*(a))
using namespace std;
typedef long long lng;
int k,n;
int s[111];
set<int> q;
int main()
{
  #ifndef  DEBUG
  freopen("humble.in","r",stdin);
  freopen("humble.out","w",stdout);
  #endif
  scanf("%d%d",&k,&n);
  for(int i=0;i<k;++i)
  {scanf("%d",s+i);q.insert(s[i]);}
  for(int i=0;i<k;++i)
  {
      set<int>::iterator it=q.begin();
      while(1)
      {
          int t=(*it)*s[i];
          if(t<0) break;
          if(q.size()>n)
          {
              q.erase(--q.end());
              if(t>=*(--q.end())) break;
          }
          q.insert(t);
          ++it;
      }
  }
  printf("%d\n",*(--q.end()));
    return 0;
}

你可能感兴趣的:(USACO:Humble Numbers)