#include <bits/stdc++.h>
#define fo(i, d, c) for (int i = d; i <= c; i++)
#define fod(i, c, d) for (int i = c; i >= d; i--)
#define maxn 1000010
#define N 1010
#define fi first
#define se second
#define pb emplace_back
#define en cout << "\n";
#define int long long
#define inf (int)1e18
#define pii pair<int, int>
#define vii vector<pii>
#define lb(x) x & -x
#define bit(i, j) ((i >> j) & 1)
#define offbit(i, j) (i ^ (1LL << j))
#define onbit(i, j) (i | (1LL << j))
#define vi vector<int>
#define all(x) x.begin(),x.end()
template <typename T1, typename T2>
bool minimize(T1 &a, T2 b)
{
if (a > b)
{
a = b;
return true;
}
return false;
}
template <typename T1, typename T2>
bool maximize(T1 &a, T2 b)
{
if (a < b)
{
a = b;
return true;
}
return false;
}
using namespace std;
const int nsqrt = 450;
const int mod = 1e9 + 7;
vii line;
int pos;
int eval(pii a,int x)
{
return a.fi * x + a.se;
}
bool bad(pii a,pii b,pii c)
{
return (double)(b.se - a.se) / (a.fi - b.fi) >= (double)(c.se - a.se) / (a.fi - c.fi);
}
void add(pii newline)
{
line.pb(newline);
while(line.size() > 2 && bad(line[line.size() - 3],line[line.size() - 2],line.back()))
line.erase(line.end() - 2);
}
int get(int x)
{
if(pos >= line.size()) pos = line.size() - 1;
while(pos < line.size() - 1 && eval(line[pos],x) > eval(line[pos + 1],x)) pos++;
return eval(line[pos],x);
}
int f[maxn];
int n,a[maxn];
main()
{
#define name "TASK"
if (fopen(name ".inp", "r"))
{
freopen(name ".inp", "r", stdin);
freopen(name ".out", "w", stdout);
}
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> n;
fo(i,1,n)
{
cin >> a[i];
maximize(a[i],a[i - 1]);
f[i] = inf;
}
add({0,0});
fo(i,1,n)
{
f[i] = get(a[i]) + n * a[i];
add({-i,f[i]});
}
cout << f[n];
}
Compilation message
Discharging.cpp: In function 'long long int get(long long int)':
Discharging.cpp:61:12: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
61 | if(pos >= line.size()) pos = line.size() - 1;
| ~~~~^~~~~~~~~~~~~~
Discharging.cpp:62:15: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
62 | while(pos < line.size() - 1 && eval(line[pos],x) > eval(line[pos + 1],x)) pos++;
| ~~~~^~~~~~~~~~~~~~~~~
Discharging.cpp: At global scope:
Discharging.cpp:67:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
67 | main()
| ^~~~
Discharging.cpp: In function 'int main()':
Discharging.cpp:72:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
72 | freopen(name ".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
Discharging.cpp:73:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
73 | freopen(name ".out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
0 ms |
2396 KB |
Output is correct |
5 |
Correct |
0 ms |
2392 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
0 ms |
2396 KB |
Output is correct |
8 |
Correct |
0 ms |
2504 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
11 |
Correct |
1 ms |
2396 KB |
Output is correct |
12 |
Correct |
0 ms |
2396 KB |
Output is correct |
13 |
Correct |
1 ms |
2396 KB |
Output is correct |
14 |
Correct |
0 ms |
2512 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
11 |
Correct |
1 ms |
2392 KB |
Output is correct |
12 |
Correct |
1 ms |
2396 KB |
Output is correct |
13 |
Correct |
1 ms |
2396 KB |
Output is correct |
14 |
Correct |
1 ms |
2520 KB |
Output is correct |
15 |
Correct |
1 ms |
2396 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
11 |
Correct |
1 ms |
2392 KB |
Output is correct |
12 |
Correct |
1 ms |
2396 KB |
Output is correct |
13 |
Correct |
1 ms |
2396 KB |
Output is correct |
14 |
Correct |
1 ms |
2520 KB |
Output is correct |
15 |
Correct |
1 ms |
2396 KB |
Output is correct |
16 |
Correct |
73 ms |
21144 KB |
Output is correct |
17 |
Correct |
77 ms |
21884 KB |
Output is correct |
18 |
Correct |
70 ms |
20308 KB |
Output is correct |
19 |
Correct |
75 ms |
21840 KB |
Output is correct |
20 |
Correct |
76 ms |
21588 KB |
Output is correct |
21 |
Correct |
78 ms |
21844 KB |
Output is correct |
22 |
Correct |
75 ms |
21364 KB |
Output is correct |
23 |
Correct |
80 ms |
22352 KB |
Output is correct |
24 |
Correct |
75 ms |
21840 KB |
Output is correct |
25 |
Correct |
79 ms |
22356 KB |
Output is correct |
26 |
Correct |
84 ms |
22612 KB |
Output is correct |
27 |
Correct |
77 ms |
21256 KB |
Output is correct |
28 |
Correct |
84 ms |
21876 KB |
Output is correct |
29 |
Correct |
80 ms |
22100 KB |
Output is correct |
30 |
Correct |
86 ms |
22256 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
116 ms |
25668 KB |
Output is correct |
2 |
Correct |
86 ms |
25576 KB |
Output is correct |
3 |
Correct |
89 ms |
25596 KB |
Output is correct |
4 |
Correct |
90 ms |
25708 KB |
Output is correct |
5 |
Correct |
86 ms |
25708 KB |
Output is correct |
6 |
Correct |
93 ms |
25700 KB |
Output is correct |
7 |
Correct |
87 ms |
25684 KB |
Output is correct |
8 |
Correct |
89 ms |
25708 KB |
Output is correct |
9 |
Correct |
94 ms |
25684 KB |
Output is correct |
10 |
Correct |
94 ms |
25840 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
0 ms |
2396 KB |
Output is correct |
5 |
Correct |
0 ms |
2392 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
0 ms |
2396 KB |
Output is correct |
8 |
Correct |
0 ms |
2504 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
11 |
Correct |
1 ms |
2396 KB |
Output is correct |
12 |
Correct |
0 ms |
2396 KB |
Output is correct |
13 |
Correct |
1 ms |
2396 KB |
Output is correct |
14 |
Correct |
0 ms |
2512 KB |
Output is correct |
15 |
Correct |
1 ms |
2392 KB |
Output is correct |
16 |
Correct |
1 ms |
2396 KB |
Output is correct |
17 |
Correct |
1 ms |
2396 KB |
Output is correct |
18 |
Correct |
1 ms |
2396 KB |
Output is correct |
19 |
Correct |
1 ms |
2396 KB |
Output is correct |
20 |
Correct |
1 ms |
2396 KB |
Output is correct |
21 |
Correct |
1 ms |
2396 KB |
Output is correct |
22 |
Correct |
1 ms |
2396 KB |
Output is correct |
23 |
Correct |
1 ms |
2396 KB |
Output is correct |
24 |
Correct |
1 ms |
2396 KB |
Output is correct |
25 |
Correct |
1 ms |
2392 KB |
Output is correct |
26 |
Correct |
1 ms |
2396 KB |
Output is correct |
27 |
Correct |
1 ms |
2396 KB |
Output is correct |
28 |
Correct |
1 ms |
2520 KB |
Output is correct |
29 |
Correct |
1 ms |
2396 KB |
Output is correct |
30 |
Correct |
1 ms |
2396 KB |
Output is correct |
31 |
Correct |
0 ms |
2396 KB |
Output is correct |
32 |
Correct |
0 ms |
2396 KB |
Output is correct |
33 |
Correct |
1 ms |
2396 KB |
Output is correct |
34 |
Correct |
1 ms |
2396 KB |
Output is correct |
35 |
Correct |
1 ms |
2396 KB |
Output is correct |
36 |
Correct |
0 ms |
2396 KB |
Output is correct |
37 |
Correct |
1 ms |
2396 KB |
Output is correct |
38 |
Correct |
1 ms |
2396 KB |
Output is correct |
39 |
Correct |
0 ms |
2396 KB |
Output is correct |
40 |
Correct |
1 ms |
2396 KB |
Output is correct |
41 |
Correct |
1 ms |
2524 KB |
Output is correct |
42 |
Correct |
1 ms |
2524 KB |
Output is correct |
43 |
Correct |
1 ms |
2396 KB |
Output is correct |
44 |
Correct |
1 ms |
2396 KB |
Output is correct |
45 |
Correct |
1 ms |
2396 KB |
Output is correct |
46 |
Correct |
1 ms |
2396 KB |
Output is correct |
47 |
Correct |
0 ms |
2396 KB |
Output is correct |
48 |
Correct |
0 ms |
2396 KB |
Output is correct |
49 |
Correct |
0 ms |
2396 KB |
Output is correct |
50 |
Correct |
1 ms |
2520 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
0 ms |
2396 KB |
Output is correct |
5 |
Correct |
0 ms |
2392 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
0 ms |
2396 KB |
Output is correct |
8 |
Correct |
0 ms |
2504 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
11 |
Correct |
1 ms |
2396 KB |
Output is correct |
12 |
Correct |
0 ms |
2396 KB |
Output is correct |
13 |
Correct |
1 ms |
2396 KB |
Output is correct |
14 |
Correct |
0 ms |
2512 KB |
Output is correct |
15 |
Correct |
1 ms |
2392 KB |
Output is correct |
16 |
Correct |
1 ms |
2396 KB |
Output is correct |
17 |
Correct |
1 ms |
2396 KB |
Output is correct |
18 |
Correct |
1 ms |
2396 KB |
Output is correct |
19 |
Correct |
1 ms |
2396 KB |
Output is correct |
20 |
Correct |
1 ms |
2396 KB |
Output is correct |
21 |
Correct |
1 ms |
2396 KB |
Output is correct |
22 |
Correct |
1 ms |
2396 KB |
Output is correct |
23 |
Correct |
1 ms |
2396 KB |
Output is correct |
24 |
Correct |
1 ms |
2396 KB |
Output is correct |
25 |
Correct |
1 ms |
2392 KB |
Output is correct |
26 |
Correct |
1 ms |
2396 KB |
Output is correct |
27 |
Correct |
1 ms |
2396 KB |
Output is correct |
28 |
Correct |
1 ms |
2520 KB |
Output is correct |
29 |
Correct |
1 ms |
2396 KB |
Output is correct |
30 |
Correct |
73 ms |
21144 KB |
Output is correct |
31 |
Correct |
77 ms |
21884 KB |
Output is correct |
32 |
Correct |
70 ms |
20308 KB |
Output is correct |
33 |
Correct |
75 ms |
21840 KB |
Output is correct |
34 |
Correct |
76 ms |
21588 KB |
Output is correct |
35 |
Correct |
78 ms |
21844 KB |
Output is correct |
36 |
Correct |
75 ms |
21364 KB |
Output is correct |
37 |
Correct |
80 ms |
22352 KB |
Output is correct |
38 |
Correct |
75 ms |
21840 KB |
Output is correct |
39 |
Correct |
79 ms |
22356 KB |
Output is correct |
40 |
Correct |
84 ms |
22612 KB |
Output is correct |
41 |
Correct |
77 ms |
21256 KB |
Output is correct |
42 |
Correct |
84 ms |
21876 KB |
Output is correct |
43 |
Correct |
80 ms |
22100 KB |
Output is correct |
44 |
Correct |
86 ms |
22256 KB |
Output is correct |
45 |
Correct |
116 ms |
25668 KB |
Output is correct |
46 |
Correct |
86 ms |
25576 KB |
Output is correct |
47 |
Correct |
89 ms |
25596 KB |
Output is correct |
48 |
Correct |
90 ms |
25708 KB |
Output is correct |
49 |
Correct |
86 ms |
25708 KB |
Output is correct |
50 |
Correct |
93 ms |
25700 KB |
Output is correct |
51 |
Correct |
87 ms |
25684 KB |
Output is correct |
52 |
Correct |
89 ms |
25708 KB |
Output is correct |
53 |
Correct |
94 ms |
25684 KB |
Output is correct |
54 |
Correct |
94 ms |
25840 KB |
Output is correct |
55 |
Correct |
1 ms |
2396 KB |
Output is correct |
56 |
Correct |
0 ms |
2396 KB |
Output is correct |
57 |
Correct |
0 ms |
2396 KB |
Output is correct |
58 |
Correct |
1 ms |
2396 KB |
Output is correct |
59 |
Correct |
1 ms |
2396 KB |
Output is correct |
60 |
Correct |
1 ms |
2396 KB |
Output is correct |
61 |
Correct |
0 ms |
2396 KB |
Output is correct |
62 |
Correct |
1 ms |
2396 KB |
Output is correct |
63 |
Correct |
1 ms |
2396 KB |
Output is correct |
64 |
Correct |
0 ms |
2396 KB |
Output is correct |
65 |
Correct |
1 ms |
2396 KB |
Output is correct |
66 |
Correct |
1 ms |
2524 KB |
Output is correct |
67 |
Correct |
1 ms |
2524 KB |
Output is correct |
68 |
Correct |
1 ms |
2396 KB |
Output is correct |
69 |
Correct |
1 ms |
2396 KB |
Output is correct |
70 |
Correct |
1 ms |
2396 KB |
Output is correct |
71 |
Correct |
1 ms |
2396 KB |
Output is correct |
72 |
Correct |
0 ms |
2396 KB |
Output is correct |
73 |
Correct |
0 ms |
2396 KB |
Output is correct |
74 |
Correct |
0 ms |
2396 KB |
Output is correct |
75 |
Correct |
1 ms |
2520 KB |
Output is correct |
76 |
Correct |
78 ms |
22096 KB |
Output is correct |
77 |
Correct |
94 ms |
22128 KB |
Output is correct |
78 |
Correct |
72 ms |
21328 KB |
Output is correct |
79 |
Correct |
77 ms |
21744 KB |
Output is correct |
80 |
Correct |
82 ms |
22216 KB |
Output is correct |
81 |
Correct |
77 ms |
21500 KB |
Output is correct |
82 |
Correct |
79 ms |
21372 KB |
Output is correct |
83 |
Correct |
78 ms |
21844 KB |
Output is correct |
84 |
Correct |
76 ms |
21844 KB |
Output is correct |
85 |
Correct |
85 ms |
22128 KB |
Output is correct |
86 |
Correct |
81 ms |
21332 KB |
Output is correct |
87 |
Correct |
74 ms |
21332 KB |
Output is correct |
88 |
Correct |
93 ms |
21588 KB |
Output is correct |
89 |
Correct |
73 ms |
21632 KB |
Output is correct |
90 |
Correct |
75 ms |
21440 KB |
Output is correct |
91 |
Correct |
70 ms |
21328 KB |
Output is correct |
92 |
Correct |
83 ms |
22620 KB |
Output is correct |
93 |
Correct |
79 ms |
21844 KB |
Output is correct |
94 |
Correct |
86 ms |
20972 KB |
Output is correct |
95 |
Correct |
79 ms |
22384 KB |
Output is correct |
96 |
Correct |
72 ms |
21236 KB |
Output is correct |
97 |
Correct |
72 ms |
21584 KB |
Output is correct |
98 |
Correct |
75 ms |
22096 KB |
Output is correct |
99 |
Correct |
74 ms |
21340 KB |
Output is correct |
100 |
Correct |
72 ms |
21588 KB |
Output is correct |