In this HackerEarth Scoring in Exam problem solution Milly is at the examination hall where she is reading a question paper. She checked the question paper and discovered that there are N questions in that paper. Each question has some score value. Ideally it's like questions requiring more time have more score value and strangely no two questions on the paper require same time to be solved.

She is very excited by looking these questions. She decided to solve K questions while maximizing their score value. Could you please help Milly to determine the exact time she needs to solve the questions.


HackerEarth Scoring in Exam problem solution


HackerEarth Scoring in Exam problem solution.

#include <bits/stdc++.h>

using namespace std;

long long t[100001];
long long scr[100001];
long long s[100001];

int main()
{
int n,q;
cin >> n >> q;
for(int i=0;i<n;i++) {
cin>>t[i];
}
for(int i=0;i<n;i++) {
cin>>scr[i];
}
sort(t,t+n);
s[n-1]=t[n-1];
for(int i=n-2;i>=0;i--) {
s[i]=t[i]+s[i+1];
}
for(int i=0;i<q;i++) {
int k;
cin>>k;
cout<<s[n-k]<<"\n";
}
return 0;
}

Second solution

#include<bits/stdc++.h>
using namespace std;
#define ll long long int
#define MOD 1000000007
#define si(a) scanf("%d", &a)
#define sl(a) scanf("%lld", &a)
#define pi(a) printf("%d", a)
#define pl(a) printf("%lld", a)
#define pn printf("\n")
ll pow_mod(ll a, ll b) {
ll res = 1;
while(b) {
if(b & 1)
res = (res * a) % MOD;
a = (a * a) % MOD;
b >>= 1;
}
return res;
}
ll ar[1000005], ar1[1000005];
ll res[1000005];
bool compare(pair<ll, ll> p1, pair<ll, ll> p2) {
if(p1.first == p2.first) {
return p1.second < p2.second;
}
return p1.first < p2.first;
}
void test(int n) {
vector<pair<ll, ll> > v;
for(int i = 0; i < n; ++i) {
v.push_back(make_pair(ar[i], ar1[i]));
}
sort(v.begin(), v.end(), compare);
for(int i = 1; i < n; ++i) {
assert(v[i].first >= v[i - 1].first && v[i].second >= v[i - 1].second);
}
}
int main() {
int n, q;
si(n); si(q);
for(int i = 0; i < n; ++i) {
sl(ar[i]);
}
for(int i = 0; i < n; ++i) {
sl(ar1[i]);
}
test(n);
sort(ar, ar + n);
reverse(ar, ar + n);
res[0] = ar[0];
for(int i = 1; i < n; ++i) {
res[i] = res[i - 1] + ar[i];
}
int k;
while(q--) {
si(k);
pl(res[k - 1]);
pn;
}
return 0;
}