B. Om Nom and Dark Park

Source: Internet
Author: User

B. Om Nom and Dark Park

Add some values to some edges on a tree full of two forks. The weights and values on the path of the root node to the leaf node are equal. Minimum number of additions required.

We use nature to solve problems. Investigate sibling nodes.  Since they are the same path from the node to the parent node, the value that needs to be added is 2*max (lch,rch)-lch-rch; Add Max (Lch,rch) to the parent node at the same time.

The idea is the most important. Sometimes it's not a question of cleverness, but a question of whether you think.

1#include <iostream>2#include <cstdio>3#include <cstring>4#include <cmath>5#include <algorithm>6#include <string>7#include <vector>8#include <Set>9#include <map>Ten#include <stack> One#include <queue> A#include <sstream> -#include <iomanip> - using namespacestd; thetypedefLong Longll; - Const intinf=0x4fffffff; - Const intexp=1e-5; - Const intms= A; +  - intnum[1<<MS]; +  A intMain () at { -       intN; -memset (NUM,0,sizeof(num)); -scanf"%d",&n); -        for(intI=2;i< (1<< (n+1)); i++) -scanf"%d",&num[i]); in       intans=0; -        for(intI= (1<<n)-1;i>0; i--) to       { +             intlch=num[i<<1]; -             intrch=num[i<<1|1]; the             intt=Max (lch,rch); *num[i]+=T; $ans+=2*t-lch-rch;Panax Notoginseng       } -printf"%d\n", ans); the       return 0; +}

B. Om Nom and Dark Park

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.