PAT1037. Magic Coupon (25)
#include <iostream> #include <algorithm> #include <vector> using namespace std; int nc,np; vector<int> vc,vp; int tmp; int i,j; int total; int main(){ cin>>nc; for(i=0;i<nc;i++){ cin>>tmp; vc.push_back(tmp); } cin>>np; for(int i=0;i<np;i++){ cin>>tmp; vp.push_back(tmp); } sort(vc.begin(),vc.end()); sort(vp.begin(),vp.end()); int a=0,b=0; for(a=0;a<vc.size();a++){ if(vc[a]>0) break; } for(b=0;b<vp.size();b++){ if(vp[b]>0) break; } i=vc.size()-1;j=vp.size()-1; while((i>=a)&&(j>=b)){ total+=vc[i]*vp[j]; i--; j--; } int m=0,n=0; while(m<a&&n<b){ total+=vc[m]*vp[n]; m++; n++; } cout<<total<<endl; return 0; }