Advertisement
Guest User

Untitled

a guest
Feb 21st, 2018
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.89 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. typedef unsigned long long ull;
  6.  
  7. int main(int argc, char const *argv[])
  8. {
  9.     ull n;
  10.     cin>>n;
  11.     int line[n];
  12.     for (int i = 0; i < n; ++i) cin>>line[i];
  13.     ull flous(0);
  14.     int i(0),j(n-1),day(1);
  15.     while(i<j)
  16.     {
  17.         if (line[i]*day < line[j]*day)
  18.         {
  19.             flous += line[i]*day;
  20.             day++; i++;
  21.         }
  22.         else if (line[i]*day > line[j]*day)
  23.         {
  24.             flous += line[j]*day;
  25.             day++; j--;
  26.         }
  27.         else if (j - i > 2)
  28.         {
  29.             // first keep going until there's two distinct values
  30.             // then do something like the following:
  31.             if (line[i+1]*(day+1) < line[j-1]*(day+1))
  32.             {
  33.                 flous += line[i]*day;
  34.                 day++; i++;
  35.             }
  36.             else if (line[i]*(day+1) < line[j]*(day+1))
  37.             {
  38.                 flous += line[j]*day;
  39.                 day++; j--;
  40.             }
  41.         }
  42.         else
  43.         {
  44.             flous += line[j]*day;
  45.             day++; j--;
  46.         }  
  47.     }
  48.     flous += line[j] * day;
  49.     cout<<flous<<'\n';
  50.     return 0;
  51. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement