Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- int main(){
- int n;
- cin>>n;
- int negativeCounter=0;
- int first=1;
- int last=1;
- int a;
- int ans=-0x80000000;
- int ans2=-0x80000000;
- int product=1;
- int nymCounter=0;
- for(int i=0;i<n;i++){
- cin>>a;
- if(a>0){
- product*=a;
- nymCounter++;
- if(negativeCounter==0){
- first*=a;
- }
- else{
- last*=a;
- }
- }
- else if(a<0){
- nymCounter++;
- product*=a;
- if(negativeCounter==0){
- first*=a;
- }
- if(negativeCounter==1){
- ans=max(ans, last);
- }
- last=a;
- negativeCounter++;
- }
- else{//Если встречаем 0
- if(product<0){
- if(nymCounter!=1){
- ans2=product/first;
- ans2=max(ans2, product/last);
- }
- else{
- ans2=max(ans2, product);
- }
- ans2=max(ans2, 0);
- }
- else{
- ans2=product;
- }
- ans=max(ans, ans2);
- negativeCounter=0;
- first=1;
- last=1;
- product=1;
- nymCounter=0;
- }
- }
- if(product<0){
- if(nymCounter!=1){
- ans2=product/first;
- ans2=max(ans2, product/last);
- }
- else{
- ans2=max(ans, product);
- }
- cout<<max(ans, ans2);
- }
- else{
- cout<<max(product, ans2);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement