-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathActive Infants.cpp
118 lines (100 loc) · 2.96 KB
/
Active Infants.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
// Kevin Mathew T
// Birla Institute of Technology, Mesra
// LinkedIn - https://www.linkedin.com/in/KevinMathewT/
// GitHub - https://github.com/KevinMathewT
// CodeForces - https://codeforces.com/profile/KevinMathew
// CodeChef - https://www.codechef.com/users/KevinMathew
// HackerRank - https://www.hackerrank.com/KevinMathew
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef double ld;
#define F first
#define S second
#define fi(a, b) for(ll i=(a);i<=b;i++)
#define fj(a, b) for(ll j=(a);j<=b;j++)
#define fid(a, b) for(ll i=(a);i>=b;i--)
#define fjd(a, b) for(ll j=(a);j>=b;j--)
// #include <ext/pb_ds/assoc_container.hpp> // Common file
// #include <ext/pb_ds/tree_policy.hpp>
// #include <functional> // for less
// #include <iostream>
// using namespace __gnu_pbds;
// using namespace std;
// typedef tree<ll, null_type, less<ll>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;
// find_by_order(k) : Value at kth index in sorted array.
// order_of_key(k) : Index of the value k.
template<class T> ostream& operator<<(ostream &os, vector<T> V) {
os << "[ ";
for(auto v : V) os << v << " ";
os << "]";
return os;
}
template<class T> ostream& operator<<(ostream &os, set<T> S){
os << "{ ";
for(auto s:S) os<<s<<" ";
return os<<"}";
}
template<class T> ostream& operator<<(ostream &os, multiset<T> S){
os << "{ ";
for(auto s:S) os<<s<<" ";
return os<<"}";
}
template<class L, class R> ostream& operator<<(ostream &os, pair<L,R> P) {
return os << "(" << P.first << "," << P.second << ")";
}
template<class L, class R> ostream& operator<<(ostream &os, map<L,R> M) {
os << "{ ";
for(auto m:M) os<<"("<<m.F<<":"<<m.S<<") ";
return os<<"}";
}
template<class L, class R> ostream& operator<<(ostream &os, unordered_map<L,R> M) {
os << "{ ";
for(auto m:M) os<<"("<<m.F<<":"<<m.S<<") ";
return os<<"}";
}
const ll N = 2010, M = 1e9 + 7;
ll n, a[N];
vector<pair<ll, pair<ll, ll> > > v;
void solve(){
cin >> n;
fi(0, n - 1) cin >> a[i];
fi(0, n - 1) v.push_back({a[i], {i + 1, max(i, n - i - 1)}});
set<ll> s;
fi(1, n) s.insert(i);
sort(v.begin(), v.end());
ll prize = 0;
fid(n - 1, 0) {
ll val = v[i].F;
ll pos = v[i].S.F;
ll front = *s.begin();
ll back = *prev(s.end());
cout << front << ' ' << back << "\n";
if(abs(front - pos) >= abs(back - pos)) {
s.erase(s.begin());
prize += val * abs(front - pos);
} else {
s.erase(prev(s.end()));
prize += val * abs(back - pos);
}
if(s.size() == 0) break;
front = *s.begin();
back = *prev(s.end());
fj(0, i) v[j].S.S = max(abs(front - v[j].S.F), abs(back - v[j].S.F));
sort(v.begin(), v.end());
cout << prize << '\n';
}
cout << prize << "\n";
}
int main(){
freopen("input.txt", "r", stdin); //Comment
freopen("output.txt", "w", stdout); //this out.
ios::sync_with_stdio(false); //Not
cin.tie(NULL); //this.
cout.tie(0); //or this.
// ll T;
// cin >> T;
// while(T--)
solve();
return 0;
}