Submission #43178109
Source Code Expand
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
#define Ls(i,l,r) for(int i=l;i<r;++i)
#define Rs(i,l,r) for(int i=l;i>r;--i)
#define Le(i,l,r) for(int i=l;i<=r;++i)
#define Re(i,l,r) for(int i=l;i>=r;--i)
#define L(i,l) for(int i=0;i<l;++i)
#define E(i,l) for(int i=1;i<=l;++i)
#define W(t) while(t--)
const int N=300010,M=2*N;
int n,h[N],e[M],ne[M],d[N],w[M],idx,k;
typedef long long ll;
const ll inf=1e18;
ll f[2][N],a[N];
void add(int a,int b,int c){
w[idx]=c,e[idx]=b,ne[idx]=h[a],h[a]=idx++;
}
void dfs(int x,int fa){
ll &sum=f[0][x];
for(int i=h[x];~i;i=ne[i]){
int j=e[i];
if(j==fa)continue;
dfs(j,x);
}
int cur=0;
for(int i=h[x];~i;i=ne[i]){
int j=e[i];
ll tmp=f[1][j]+w[i]-f[0][j];
if(j==fa)continue;
sum+=f[0][j];
if(tmp>0)a[++cur]=tmp;
}
f[1][x]=d[x]?sum:-inf;
if(!d[x])return;
k=cur;
if(d[x]-1<cur)k=d[x]-1;
if(k&&k!=cur)nth_element(a+1,a+(cur-k+1),a+1+cur);
Le(i, cur-k+1, cur)f[1][x]+=a[i];
sum=f[1][x];
int maxn=0;
E(i, cur-k)
if(a[i]>maxn)maxn=a[i];
sum+=maxn;
}
int main(){
#ifndef ONLINE_JUDGE
freopen("1.in","r",stdin);
// freopen("1.out","w",stdout);
// ios::sync_with_stdio(0);
// cin.tie(0);
// cout.tie(0);
#endif
// Insert Code Here
scanf("%d",&n);
memset(h,-1,n*4+4);
E(i, n)scanf("%d",d+i);
E(i, n-1){
int a,b,c;
scanf("%d%d%d",&a,&b,&c);
add(a, b, c);
add(b,a, c);
}
dfs(1,-1);
printf("%lld",f[0][1]);
return 0;
}
Submission Info
| Submission Time |
|
| Task |
F - Select Edges |
| User |
WUSICHENG |
| Language |
C++ (GCC 9.2.1) |
| Score |
500 |
| Code Size |
1672 Byte |
| Status |
AC |
| Exec Time |
164 ms |
| Memory |
41664 KiB |
Compile Error
./Main.cpp: In function ‘int main()’:
./Main.cpp:56:10: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
56 | scanf("%d",&n);
| ~~~~~^~~~~~~~~
./Main.cpp:58:17: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
58 | E(i, n)scanf("%d",d+i);
| ~~~~~^~~~~~~~~~
./Main.cpp:61:14: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
61 | scanf("%d%d%d",&a,&b,&c);
| ~~~~~^~~~~~~~~~~~~~~~~~~
Judge Result
| Set Name |
Sample |
All |
| Score / Max Score |
0 / 0 |
500 / 500 |
| Status |
|
|
| Set Name |
Test Cases |
| Sample |
example0.txt, example1.txt |
| All |
000.txt, 001.txt, 002.txt, 003.txt, 004.txt, 005.txt, 006.txt, 007.txt, 008.txt, 009.txt, 010.txt, 011.txt, 012.txt, 013.txt, 014.txt, 015.txt, 016.txt, 017.txt, 018.txt, 019.txt, 020.txt, 021.txt, 022.txt, 023.txt, 024.txt, 025.txt, 026.txt, 027.txt, 028.txt, 029.txt, 030.txt, 031.txt, 032.txt, 033.txt, 034.txt, 035.txt, 036.txt, 037.txt, 038.txt, 039.txt, 040.txt, 041.txt, 042.txt, 043.txt, 044.txt, 045.txt, 046.txt, 047.txt, 048.txt, 049.txt, 050.txt, example0.txt, example1.txt |
| Case Name |
Status |
Exec Time |
Memory |
| 000.txt |
AC |
5 ms |
1652 KiB |
| 001.txt |
AC |
1 ms |
1704 KiB |
| 002.txt |
AC |
6 ms |
1676 KiB |
| 003.txt |
AC |
145 ms |
15732 KiB |
| 004.txt |
AC |
129 ms |
16928 KiB |
| 005.txt |
AC |
125 ms |
16900 KiB |
| 006.txt |
AC |
129 ms |
18064 KiB |
| 007.txt |
AC |
130 ms |
18028 KiB |
| 008.txt |
AC |
130 ms |
18024 KiB |
| 009.txt |
AC |
131 ms |
18024 KiB |
| 010.txt |
AC |
122 ms |
13440 KiB |
| 011.txt |
AC |
149 ms |
41664 KiB |
| 012.txt |
AC |
153 ms |
30620 KiB |
| 013.txt |
AC |
149 ms |
36092 KiB |
| 014.txt |
AC |
140 ms |
15716 KiB |
| 015.txt |
AC |
143 ms |
15716 KiB |
| 016.txt |
AC |
152 ms |
15692 KiB |
| 017.txt |
AC |
145 ms |
15780 KiB |
| 018.txt |
AC |
139 ms |
15728 KiB |
| 019.txt |
AC |
134 ms |
15780 KiB |
| 020.txt |
AC |
150 ms |
34920 KiB |
| 021.txt |
AC |
130 ms |
14528 KiB |
| 022.txt |
AC |
72 ms |
8772 KiB |
| 023.txt |
AC |
42 ms |
5656 KiB |
| 024.txt |
AC |
112 ms |
12520 KiB |
| 025.txt |
AC |
116 ms |
12828 KiB |
| 026.txt |
AC |
150 ms |
15696 KiB |
| 027.txt |
AC |
145 ms |
15764 KiB |
| 028.txt |
AC |
143 ms |
15732 KiB |
| 029.txt |
AC |
148 ms |
15712 KiB |
| 030.txt |
AC |
145 ms |
15708 KiB |
| 031.txt |
AC |
144 ms |
15688 KiB |
| 032.txt |
AC |
161 ms |
15728 KiB |
| 033.txt |
AC |
164 ms |
15732 KiB |
| 034.txt |
AC |
144 ms |
15712 KiB |
| 035.txt |
AC |
142 ms |
15684 KiB |
| 036.txt |
AC |
142 ms |
15732 KiB |
| 037.txt |
AC |
148 ms |
15712 KiB |
| 038.txt |
AC |
147 ms |
15696 KiB |
| 039.txt |
AC |
147 ms |
15688 KiB |
| 040.txt |
AC |
148 ms |
15712 KiB |
| 041.txt |
AC |
128 ms |
18116 KiB |
| 042.txt |
AC |
135 ms |
15980 KiB |
| 043.txt |
AC |
134 ms |
15776 KiB |
| 044.txt |
AC |
141 ms |
15728 KiB |
| 045.txt |
AC |
140 ms |
15700 KiB |
| 046.txt |
AC |
142 ms |
15792 KiB |
| 047.txt |
AC |
157 ms |
15788 KiB |
| 048.txt |
AC |
151 ms |
15684 KiB |
| 049.txt |
AC |
153 ms |
15696 KiB |
| 050.txt |
AC |
163 ms |
15700 KiB |
| example0.txt |
AC |
1 ms |
1652 KiB |
| example1.txt |
AC |
2 ms |
1700 KiB |