HackerRank Bela Jornada problem solution


HackerEarth Bela Jornada problem solution


HackerEarth Bela Jornada problem solution.

#pragma GCC optimize("O3")
#include <bits/stdc++.h>

#define ll long long
#define ull unsigned long long
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define be begin()
#define en end()
#define all(x) (x).begin(),(x).end()
#define alli(a, n, k) (a+k),(a+n+k)
#define REP(i, a, b, k) for(__typeof(a) i = a;i < b;i += k)
#define REPI(i, a, b, k) for(__typeof(a) i = a;i > b;i -= k)
#define REPITER(it, a) for(__typeof(a.begin()) it = a.begin();it != a.end(); ++it)

#define y0 sdkfaslhagaklsldk
#define y1 aasdfasdfasdf
#define yn askfhwqriuperikldjk
#define j1 assdgsdgasghsf
#define tm sdfjahlfasfh
#define lr asgasgash
#define norm asdfasdgasdgsd
#define have adsgagshdshfhds

#define eps 1e-6
#define pi 3.141592653589793

using namespace std;

template<class T> inline T gcd(T a, T b) { while(b) b ^= a ^= b ^= a %= b; return a; }
template<class T> inline T mod(T x) { if(x < 0) return -x; else return x; }

typedef vector<int> VII;
typedef vector<ll> VLL;
typedef pair<int, int> PII;
typedef pair<ll, ll> PLL;
typedef pair<int, PII > PPII;
typedef vector< PII > VPII;
typedef vector< PPII > VPPI;

const ll MOD = 1e10;
const int INF = 1e9;
// Template End

const int MAX = 1e5 + 5;
int a[MAX];

int main(int argc, char* argv[])
{
if(argc == 2 or argc == 3) freopen(argv[1], "r", stdin);
if(argc == 3) freopen(argv[2], "w", stdout);
int n;
long long ans = 0, sum = 0, sum1 = 0;
cin >> n;
REP(i, 0, n, 1)
{
cin >> a[i];
sum += a[i];
}
REP(i, 0, n, 1)
{
sum1 += a[i];
ans = max(ans, (sum1 * (sum - sum1)));
}
cout << ans << endl;
return 0;
}

Second solution

#include <iostream>
#include <stdio.h>
#include <assert.h>
#include <algorithm>
using namespace std;

long long readInt(long long l,long long r,char endd){
long long x=0;
int cnt=0;
int fi=-1;
bool is_neg=false;
while(true){
char g=getchar();
if(g=='-'){
assert(fi==-1);
is_neg=true;
continue;
}
if('0'<=g && g<='9'){
x*=10;
x+=g-'0';
if(cnt==0){
fi=g-'0';
}
cnt++;
assert(fi!=0 || cnt==1);
assert(fi!=0 || is_neg==false);

assert(!(cnt>19 || ( cnt==19 && fi>1) ));
} else if(g==endd || g==-1){
if(is_neg){
x= -x;
}
assert(l<=x && x<=r);
return x;
} else {
assert(false);
}
}
}
string readString(char endd){
string ret="";
while(true){
char g=getchar();
assert(g!=-1);
if(g==endd){
break;
}

ret+=g;
}
return ret;
}
long long readIntSp(long long l,long long r){
return readInt(l,r,' ');
}
long long readIntLn(long long l,long long r){
return readInt(l,r,'\n');
}
string readStringLn(){
return readString('\n');
}
string readStringSp(){
return readString(' ');
}

int n;
long long arr[100100];
long long sol=0;
long long sm=0,sm1=0;
int main(){
n=readIntLn(2,100000);
for(int i=1;i<=n;i++){
if(i==n){
arr[i]=readInt(1,10000,-1);
} else {
arr[i]=readIntSp(1,10000);
}
sm+=arr[i];
}
for(int i=1;i<n;i++){
sm1+=arr[i];
sol=max(sol,sm1*(sm-sm1));
}
cout<<sol<<endl;
assert(getchar()==-1);
}