//
// main.cpp
// Job sequencing
//
// Created by Himanshu on 14/08/21.
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
const int N = 5;
struct node {
int p,d;
};
bool cmp (const struct node &a, const struct node &b) {
return a.p> b.p;
}
void solve (int p[], int d[]) {
struct node q[N];
int slot[N] = {0};
int ans = 0;
vector<int> sol;
for (int i=0; i<N; i++) {
q[i].p = p[i];
q[i].d = d[i];
}
sort(q, q+N, &cmp);
for (int i=0; i<N; i++) {
if (slot[q[i].d-1] == 0) {
ans += q[i].p;
sol.push_back(i);
slot[q[i].d-1] = 1;
} else {
int j = q[i].d -1;
while (j>=0 && slot[j] != 0) {
j--;
}
if (j >= 0) {
ans += q[i].p;
sol.push_back(i);
slot[j] = 1;
}
}
}
cout<<"Job selected\n";
for (int i=0; i<sol.size(); i++) {
cout<<q[sol[i]].p<<" ";
}
cout<<"\nProfit: "<<ans<<"\n";
}
int main() {
int p[] = {10, 5, 20, 15, 1};
int d[] = {1, 3, 2, 2, 3};
solve(p, d);
return 0;
}
Ly8KLy8gIG1haW4uY3BwCi8vICBKb2Igc2VxdWVuY2luZwovLwovLyAgQ3JlYXRlZCBieSBIaW1hbnNodSBvbiAxNC8wOC8yMS4KCiNpbmNsdWRlIDxpb3N0cmVhbT4KI2luY2x1ZGUgPGFsZ29yaXRobT4KI2luY2x1ZGUgPHZlY3Rvcj4KCnVzaW5nIG5hbWVzcGFjZSBzdGQ7Cgpjb25zdCBpbnQgTiA9IDU7CgpzdHJ1Y3Qgbm9kZSB7CiAgICBpbnQgcCxkOwp9OwoKYm9vbCBjbXAgKGNvbnN0IHN0cnVjdCBub2RlICZhLCBjb25zdCBzdHJ1Y3Qgbm9kZSAmYikgewogICAgcmV0dXJuIGEucD4gYi5wOwp9Cgp2b2lkIHNvbHZlIChpbnQgcFtdLCBpbnQgZFtdKSB7CiAgICBzdHJ1Y3Qgbm9kZSBxW05dOwogICAgaW50IHNsb3RbTl0gPSB7MH07CiAgICBpbnQgYW5zID0gMDsKICAgIHZlY3RvcjxpbnQ+IHNvbDsKICAgIGZvciAoaW50IGk9MDsgaTxOOyBpKyspIHsKICAgICAgICBxW2ldLnAgPSBwW2ldOwogICAgICAgIHFbaV0uZCA9IGRbaV07CiAgICB9CiAgICBzb3J0KHEsIHErTiwgJmNtcCk7CiAgICAKICAgIGZvciAoaW50IGk9MDsgaTxOOyBpKyspIHsKICAgICAgICBpZiAoc2xvdFtxW2ldLmQtMV0gPT0gMCkgewogICAgICAgICAgICBhbnMgKz0gcVtpXS5wOwogICAgICAgICAgICBzb2wucHVzaF9iYWNrKGkpOwogICAgICAgICAgICBzbG90W3FbaV0uZC0xXSA9IDE7CiAgICAgICAgfSBlbHNlIHsKICAgICAgICAgICAgaW50IGogPSBxW2ldLmQgLTE7CiAgICAgICAgICAgIHdoaWxlIChqPj0wICYmIHNsb3Rbal0gIT0gMCkgewogICAgICAgICAgICAgICAgai0tOwogICAgICAgICAgICB9CiAgICAgICAgICAgIGlmIChqID49IDApIHsKICAgICAgICAgICAgICAgIGFucyArPSBxW2ldLnA7CiAgICAgICAgICAgICAgICBzb2wucHVzaF9iYWNrKGkpOwogICAgICAgICAgICAgICAgc2xvdFtqXSA9IDE7CiAgICAgICAgICAgIH0KICAgICAgICB9CiAgICB9CiAgICBjb3V0PDwiSm9iIHNlbGVjdGVkXG4iOwogICAgZm9yIChpbnQgaT0wOyBpPHNvbC5zaXplKCk7IGkrKykgewogICAgICAgIGNvdXQ8PHFbc29sW2ldXS5wPDwiICI7CiAgICB9CiAgICBjb3V0PDwiXG5Qcm9maXQ6ICI8PGFuczw8IlxuIjsKfQogCmludCBtYWluKCkgewogICAgaW50IHBbXSA9IHsxMCwgNSwgMjAsIDE1LCAxfTsKICAgIGludCBkW10gPSB7MSwgMywgMiwgMiwgM307CiAgICBzb2x2ZShwLCBkKTsKICAgIHJldHVybiAwOwp9Cg==