Codeforces Round #567 (Div. 2) B. Split a Number

Codeforces Round #567 (Div. 2)

 

B. Split a Number

 

Dima worked all day and wrote down on a long paper strip his favorite number n consisting of l digits. Unfortunately, the strip turned out to be so long that it didn't fit in the Dima's bookshelf.

To solve the issue, Dima decided to split the strip into two non-empty parts so that each of them contains a positive integer without leading zeros. After that he will compute the sum of the two integers and write it down on a new strip.

Dima wants the resulting integer to be as small as possible, because it increases the chances that the sum will fit it in the bookshelf. Help Dima decide what is the minimum sum he can obtain.

Input

The first line contains a single integer l (2≤l≤100000) — the length of the Dima's favorite number.

The second line contains the positive integer n initially written on the strip: the Dima's favorite number.

The integer n consists of exactly l digits and it does not contain leading zeros. Dima guarantees, that there is at least one valid way to split the strip.

Output

Print a single integer — the smallest number Dima can obtain.

Examples
input
7
1234567
output
1801
input
3
101
output
11

Note

In the first example Dima can split the number 1234567 into integers 1234 and 567. Their sum is 1801.

In the second example Dima can split the number 101 into integers 10 and 1. Their sum is 11. Note that it is impossible to split the strip into "1" and "01" since the numbers can't start with zeros.

 

Meaning of the title: the title is meant to give you a string of length n, so you cut it in the middle, into two strings,

Then as digital summation to find the minimum, but can not have cut out leading zero string.

Ideas: Obviously should first, when similar two strings to increase the minimum number of bits,

Then you can begin to cut from the middle of the string, but taking into account the number of bits of character parity and 0,

Therefore, extending from the middle to both sides of a truncated string, until a successful cut out two strings, and then summing the minimum refresh

 

 

  1 #include<iostream>
  2 #include<cstring>
  3 #include<cstdio>
  4 #include<cmath>
  5 #include<algorithm>
  6 #include<map>
  7 #include<vector>
  8 #include<set>
  9 #include<queue>
 10 using namespace std;
 11 #define ll long long 
 12 
 13 string Sum(string a,string b)//大数加法 
 14 {
 15     //补前导零 
 16     while(a.size()<b.size())
 17         a.insert(0,"0");
 18     while(b.size()<a.size())
 19         b.insert(0,"0");
 20     
 21     string ans="";//记录结果 
 22     
 23     int jinwei=0,sum=0,yu;//运算所需 
 24     
 25     for(int i=a.size()-1;i>=0; i--) // from the end of counting 
26 is      {
 27          SUM = (A [I] - ' 0 ' ) + (B [I] - ' 0 ' ) + Jinwei;
 28          Jinwei = SUM / 10 ;
 29          Yu = % SUM 10 ;
 30          ANS + = (Yu + ' 0 ' ); // add this bit remainder 
31 is      }
 32      IF (Jinwei) // may be a multi 
33 is          ANS + = (Jinwei + ' 0 ' );
 34 is      
35     Reverse (ans.begin (), ans.end ()); // because it is from the end, need string backward 
36      
37 [      return ANS;
 38 is  }
 39  
40  int main ()
 41 is  {
 42 is      iOS :: sync_with_stdio ( to false ); cin.tie ( 0 ); cout.tie ( 0 );
 43 is      
44 is      int len, now;
 45      String STR, S;
 46 is      String A, B;
 47      the while (CIN >> len)
 48      {
 49          CIN >> STR;
 50          
51 is          String minn="0";
 52         
 53         now=len/2;
 54         for(int i=now;i>=0;i--)
 55         {
 56             if(str[i]!='0')
 57             {
 58                 a=str.substr(0,i);
 59                 b=str.substr(i,len-i);
 60                 s=Sum(a,b);
 61                     
 62                 if(minn=="0")
63 is                      Minn = S;
 64                  the else 
65                  { // Here string not directly comparable size, to look at the length, only a small number of digits less 
66                      IF (s.size () < minn.size ()) 
 67                          Minn = S ;
 68                      the else  IF (s.size () == minn.size ())
 69                      {
 70                          IF (S < Minn)
 71 is                              Minn = S;
 72                      }
 73 is                  }
 74                  BREAK ;
 75              }
 76         }
 77             
 78             
 79         for(int i=now+1;i<len;i++)
 80         {
 81             if(str[i]!='0')
 82             {
 83                 a=str.substr(0,i);
 84                 b=str.substr(i,len-i);
 85                 s=Sum(a,b);
 86             
 87                 if(minn=="0")
 88                     minn=s;
 89                 else 
90                  {
 91                      ow (s.size () < minn.size ())
 92                          a = s;
93                      else  ow (s.size () == minn.size ())
 94                      {
 95                          ow (s < a)
 96                              a = s;
97                      }
 98                  }
 99                  break ;
100              }
 101          }
 102          COUTS << a << endl;
103      }
104     
105     return 0;
106 }

 

 

Guess you like

Origin www.cnblogs.com/xwl3109377858/p/11041560.html