Created
November 20, 2021 09:18
-
-
Save Shivani13121007/dbd5422c4e11aa50fafecc15e2deaac8 to your computer and use it in GitHub Desktop.
Max Sum In The Configuration
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include<bits/stdc++.h> | |
using namespace std; | |
int maximise(vector<int>& arr) | |
{ | |
int n = arr.size(); | |
int sum = 0; | |
int S0 = 0; | |
for(int i=0; i < n;i++) { | |
sum += arr[i]; | |
S0 += arr[i]*i; | |
} | |
int max = S0; | |
int Si = S0; | |
for(int i=0; i < n-1 ;i++) { | |
int temp = Si + sum - n*arr[n-i-1]; | |
Si = temp; | |
if(temp > max) { | |
max = temp; | |
} | |
} | |
return max; | |
} | |
int main() | |
{ | |
int n; | |
cin>>n; | |
vector<int> arr(n,0); | |
for(int i =0;i<n;i++) | |
{ | |
cin>>arr[i]; | |
} | |
int ans = maximise(arr); | |
cout<<ans; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include<bits/stdc++.h> | |
using namespace std; | |
int maximise(vector<int>& arr) | |
{ | |
// write your code here | |
} | |
int main() | |
{ | |
int n; | |
cin>>n; | |
vector<int> arr(n,0); | |
for(int i =0;i<n;i++) | |
{ | |
cin>>arr[i]; | |
} | |
int ans = maximise(arr); | |
cout<<ans; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment