#define DEBUG 0
#include <bits/stdc++.h>
using namespace std;
#if DEBUG
// basic debugging macros
int __i__,__j__;
#define printLine(l) for(__i__=0;__i__<l;__i__++){cout<<"-";}cout<<endl
#define printLine2(l,c) for(__i__=0;__i__<l;__i__++){cout<<c;}cout<<endl
#define printVar(n) cout<<#n<<": "<<n<<endl
#define printArr(a,l) cout<<#a<<": ";for(__i__=0;__i__<l;__i__++){cout<<a[__i__]<<" ";}cout<<endl
#define print2dArr(a,r,c) cout<<#a<<":\n";for(__i__=0;__i__<r;__i__++){for(__j__=0;__j__<c;__j__++){cout<<a[__i__][__j__]<<" ";}cout<<endl;}
#define print2dArr2(a,r,c,l) cout<<#a<<":\n";for(__i__=0;__i__<r;__i__++){for(__j__=0;__j__<c;__j__++){cout<<setw(l)<<setfill(' ')<<a[__i__][__j__]<<" ";}cout<<endl;}
// advanced debugging class
// debug 1,2,'A',"test";
class _Debug {
public:
template<typename T>
_Debug& operator,(T val) {
cout << val << endl;
return *this;
}
};
#define debug _Debug(),
#else
#define printLine(l)
#define printLine2(l,c)
#define printVar(n)
#define printArr(a,l)
#define print2dArr(a,r,c)
#define print2dArr2(a,r,c,l)
#define debug
#endif
// define
#define MAX_VAL 999999999
#define MAX_VAL_2 999999999999999999LL
#define EPS 1e-6
#define mp make_pair
#define pb push_back
// typedef
typedef unsigned int UI;
typedef long long int LLI;
typedef unsigned long long int ULLI;
typedef unsigned short int US;
typedef pair<int,int> pii;
typedef pair<LLI,LLI> plli;
typedef vector<int> vi;
typedef vector<LLI> vlli;
typedef vector<pii> vpii;
typedef vector<plli> vplli;
// ---------- END OF TEMPLATE ----------
int A[200000];
vpii stars[200000];
int l[200000],r[200000];
vi S;
bool comp(int a,int b) {
return r[a]-l[a] < r[b]-l[b];
}
int order[200000];
LLI dp[200000];
int main() {
int i;
int N,M,X,Y,C;
LLI sum = 0;
scanf("%d",&N);
for (i = 0; i < N; i++) scanf("%d",&A[i]);
scanf("%d",&M);
for (i = 0; i < M; i++) scanf("%d %d %d",&X,&Y,&C),stars[X-1].pb(mp(Y,C)),sum += C;
int j;
for (i = 0; i < N; i++) {
while (!S.empty() && (A[S.back()] <= A[i])) S.pop_back();
if (!S.empty()) l[i] = S.back();
else l[i] = -1;
S.pb(i);
}
S.clear();
for (i = N-1; i >= 0; i--) {
while (!S.empty() && (A[S.back()] < A[i])) S.pop_back();
if (!S.empty()) r[i] = S.back();
else r[i] = N;
S.pb(i);
}
for (i = 0; i < N; i++) order[i] = i,sort(stars[i].begin(),stars[i].end(),greater<pii>());
sort(order,order+N,comp);
LLI ans = 0;
for (i = 0; i < N; i++) {
int u = order[i];
LLI maxl = 0,maxr = 0;
for (j = l[u]+1; j < u; j++) {
while (!stars[j].empty() && (stars[j].back().first <= A[u])) {
maxl = max(maxl,dp[j]+stars[j].back().second);
stars[j].pop_back();
}
maxl = max(maxl,dp[j]);
}
for (j = u+1; j < r[u]; j++) {
while (!stars[j].empty() && (stars[j].back().first <= A[u])) {
maxr = max(maxr,dp[j]+stars[j].back().second);
stars[j].pop_back();
}
maxr = max(maxr,dp[j]);
}
for (j = l[u]+1; j <= u; j++) dp[j] += maxr;
for (j = u; j < r[u]; j++) dp[j] += maxl;
ans = max(ans,dp[u]);
}
for (i = 0; i < N; i++) {
while (!stars[i].empty()) ans = max(ans,stars[i].back().second+dp[i]),stars[i].pop_back();
}
printf("%lld\n",sum-ans);
return 0;
}
Compilation message
constellation3.cpp: In function 'int main()':
constellation3.cpp:71:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d",&N);
~~~~~^~~~~~~~~
constellation3.cpp:72:34: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
for (i = 0; i < N; i++) scanf("%d",&A[i]);
~~~~~^~~~~~~~~~~~
constellation3.cpp:73:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d",&M);
~~~~~^~~~~~~~~
constellation3.cpp:74:78: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
for (i = 0; i < M; i++) scanf("%d %d %d",&X,&Y,&C),stars[X-1].pb(mp(Y,C)),sum += C;
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
4992 KB |
Output is correct |
2 |
Correct |
7 ms |
5120 KB |
Output is correct |
3 |
Correct |
7 ms |
4992 KB |
Output is correct |
4 |
Correct |
8 ms |
5120 KB |
Output is correct |
5 |
Correct |
8 ms |
5120 KB |
Output is correct |
6 |
Correct |
7 ms |
5120 KB |
Output is correct |
7 |
Correct |
7 ms |
5120 KB |
Output is correct |
8 |
Correct |
7 ms |
5120 KB |
Output is correct |
9 |
Correct |
7 ms |
4992 KB |
Output is correct |
10 |
Correct |
8 ms |
5120 KB |
Output is correct |
11 |
Correct |
8 ms |
5120 KB |
Output is correct |
12 |
Correct |
8 ms |
5120 KB |
Output is correct |
13 |
Correct |
7 ms |
4992 KB |
Output is correct |
14 |
Correct |
8 ms |
4992 KB |
Output is correct |
15 |
Correct |
7 ms |
5120 KB |
Output is correct |
16 |
Correct |
8 ms |
5120 KB |
Output is correct |
17 |
Correct |
9 ms |
5120 KB |
Output is correct |
18 |
Correct |
8 ms |
5120 KB |
Output is correct |
19 |
Correct |
7 ms |
5120 KB |
Output is correct |
20 |
Correct |
7 ms |
5120 KB |
Output is correct |
21 |
Correct |
7 ms |
4992 KB |
Output is correct |
22 |
Correct |
7 ms |
5120 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
4992 KB |
Output is correct |
2 |
Correct |
7 ms |
5120 KB |
Output is correct |
3 |
Correct |
7 ms |
4992 KB |
Output is correct |
4 |
Correct |
8 ms |
5120 KB |
Output is correct |
5 |
Correct |
8 ms |
5120 KB |
Output is correct |
6 |
Correct |
7 ms |
5120 KB |
Output is correct |
7 |
Correct |
7 ms |
5120 KB |
Output is correct |
8 |
Correct |
7 ms |
5120 KB |
Output is correct |
9 |
Correct |
7 ms |
4992 KB |
Output is correct |
10 |
Correct |
8 ms |
5120 KB |
Output is correct |
11 |
Correct |
8 ms |
5120 KB |
Output is correct |
12 |
Correct |
8 ms |
5120 KB |
Output is correct |
13 |
Correct |
7 ms |
4992 KB |
Output is correct |
14 |
Correct |
8 ms |
4992 KB |
Output is correct |
15 |
Correct |
7 ms |
5120 KB |
Output is correct |
16 |
Correct |
8 ms |
5120 KB |
Output is correct |
17 |
Correct |
9 ms |
5120 KB |
Output is correct |
18 |
Correct |
8 ms |
5120 KB |
Output is correct |
19 |
Correct |
7 ms |
5120 KB |
Output is correct |
20 |
Correct |
7 ms |
5120 KB |
Output is correct |
21 |
Correct |
7 ms |
4992 KB |
Output is correct |
22 |
Correct |
7 ms |
5120 KB |
Output is correct |
23 |
Correct |
8 ms |
5120 KB |
Output is correct |
24 |
Correct |
9 ms |
5120 KB |
Output is correct |
25 |
Correct |
9 ms |
5120 KB |
Output is correct |
26 |
Correct |
9 ms |
5120 KB |
Output is correct |
27 |
Correct |
9 ms |
5248 KB |
Output is correct |
28 |
Correct |
10 ms |
5120 KB |
Output is correct |
29 |
Correct |
9 ms |
5248 KB |
Output is correct |
30 |
Correct |
9 ms |
5248 KB |
Output is correct |
31 |
Correct |
9 ms |
5120 KB |
Output is correct |
32 |
Correct |
13 ms |
5248 KB |
Output is correct |
33 |
Correct |
13 ms |
5120 KB |
Output is correct |
34 |
Correct |
12 ms |
5120 KB |
Output is correct |
35 |
Correct |
11 ms |
5120 KB |
Output is correct |
36 |
Correct |
13 ms |
5248 KB |
Output is correct |
37 |
Correct |
13 ms |
5120 KB |
Output is correct |
38 |
Correct |
16 ms |
5120 KB |
Output is correct |
39 |
Correct |
9 ms |
5120 KB |
Output is correct |
40 |
Correct |
14 ms |
5120 KB |
Output is correct |
41 |
Correct |
9 ms |
5120 KB |
Output is correct |
42 |
Correct |
9 ms |
5120 KB |
Output is correct |
43 |
Correct |
14 ms |
5120 KB |
Output is correct |
44 |
Correct |
9 ms |
5120 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
4992 KB |
Output is correct |
2 |
Correct |
7 ms |
5120 KB |
Output is correct |
3 |
Correct |
7 ms |
4992 KB |
Output is correct |
4 |
Correct |
8 ms |
5120 KB |
Output is correct |
5 |
Correct |
8 ms |
5120 KB |
Output is correct |
6 |
Correct |
7 ms |
5120 KB |
Output is correct |
7 |
Correct |
7 ms |
5120 KB |
Output is correct |
8 |
Correct |
7 ms |
5120 KB |
Output is correct |
9 |
Correct |
7 ms |
4992 KB |
Output is correct |
10 |
Correct |
8 ms |
5120 KB |
Output is correct |
11 |
Correct |
8 ms |
5120 KB |
Output is correct |
12 |
Correct |
8 ms |
5120 KB |
Output is correct |
13 |
Correct |
7 ms |
4992 KB |
Output is correct |
14 |
Correct |
8 ms |
4992 KB |
Output is correct |
15 |
Correct |
7 ms |
5120 KB |
Output is correct |
16 |
Correct |
8 ms |
5120 KB |
Output is correct |
17 |
Correct |
9 ms |
5120 KB |
Output is correct |
18 |
Correct |
8 ms |
5120 KB |
Output is correct |
19 |
Correct |
7 ms |
5120 KB |
Output is correct |
20 |
Correct |
7 ms |
5120 KB |
Output is correct |
21 |
Correct |
7 ms |
4992 KB |
Output is correct |
22 |
Correct |
7 ms |
5120 KB |
Output is correct |
23 |
Correct |
8 ms |
5120 KB |
Output is correct |
24 |
Correct |
9 ms |
5120 KB |
Output is correct |
25 |
Correct |
9 ms |
5120 KB |
Output is correct |
26 |
Correct |
9 ms |
5120 KB |
Output is correct |
27 |
Correct |
9 ms |
5248 KB |
Output is correct |
28 |
Correct |
10 ms |
5120 KB |
Output is correct |
29 |
Correct |
9 ms |
5248 KB |
Output is correct |
30 |
Correct |
9 ms |
5248 KB |
Output is correct |
31 |
Correct |
9 ms |
5120 KB |
Output is correct |
32 |
Correct |
13 ms |
5248 KB |
Output is correct |
33 |
Correct |
13 ms |
5120 KB |
Output is correct |
34 |
Correct |
12 ms |
5120 KB |
Output is correct |
35 |
Correct |
11 ms |
5120 KB |
Output is correct |
36 |
Correct |
13 ms |
5248 KB |
Output is correct |
37 |
Correct |
13 ms |
5120 KB |
Output is correct |
38 |
Correct |
16 ms |
5120 KB |
Output is correct |
39 |
Correct |
9 ms |
5120 KB |
Output is correct |
40 |
Correct |
14 ms |
5120 KB |
Output is correct |
41 |
Correct |
9 ms |
5120 KB |
Output is correct |
42 |
Correct |
9 ms |
5120 KB |
Output is correct |
43 |
Correct |
14 ms |
5120 KB |
Output is correct |
44 |
Correct |
9 ms |
5120 KB |
Output is correct |
45 |
Correct |
218 ms |
19424 KB |
Output is correct |
46 |
Correct |
222 ms |
19448 KB |
Output is correct |
47 |
Correct |
231 ms |
19452 KB |
Output is correct |
48 |
Correct |
218 ms |
19192 KB |
Output is correct |
49 |
Correct |
222 ms |
19320 KB |
Output is correct |
50 |
Correct |
222 ms |
19056 KB |
Output is correct |
51 |
Correct |
219 ms |
19064 KB |
Output is correct |
52 |
Correct |
230 ms |
19580 KB |
Output is correct |
53 |
Correct |
226 ms |
19320 KB |
Output is correct |
54 |
Execution timed out |
1587 ms |
19828 KB |
Time limit exceeded |
55 |
Halted |
0 ms |
0 KB |
- |