Statistics

Problem Statement for "RobotHerbDiv2"

Problem Statement

Robot Herb is playing on an infinite square grid. At any moment, Herb stands on one of the squares and he faces one of the four cardinal directions. In his memory, Herb has a program. The program is a sequence of commands. For each i, the i-th of these commands has the following form:

  • First move forward a[i] tiles.
  • Then turn 90 degrees to the right, a[i] times in a row.

Herb has decided to run the program T times. You are given the int T and the int[] a that describes Herb's program. Let A be the initial position of Herb and B be his position after the program was executed T times. Return the Manhattan distance between tiles A and B.

Definition

Class:
RobotHerbDiv2
Method:
getdist
Parameters:
int, int[]
Returns:
int
Method signature:
int getdist(int T, int[] a)
(be sure your method is public)

Notes

  • Let's introduce a Cartesian coordinate system on the grid so that each cardinal direction is parallel to one of the axes. The Manhattan distance between two tiles with centers at points (x1, y1) and (x2, y2) is defined as |x1-x2| + |y1-y2|.

Constraints

  • T will be between 1 and 100, inclusive.
  • a will contain between 1 and 50 elements, inclusive.
  • Each element of a will be between 1 and 400,000, inclusive.

Examples

  1. 1

    {1,2,3}

    Returns: 2

    Suppose that initially Herb stands on the tile with center at (0, 0) and faces the positive y direction. The program will get executed as follows: tile direction After 1st command: (0, 1) positive x After 2nd command: (2, 1) negative x After 3rd command: (-1, 1) negative y The manhattan distance between the initial and the final positions is |-1| + |1| = 2.

  2. 100

    {1}

    Returns: 0

  3. 5

    {1,1,2}

    Returns: 10

  4. 100

    {400000}

    Returns: 40000000

  5. 100

    {100000,100000,100000,100000,100000,100000,100000,100000,100000,100000,100000,100000,100000,100000,100000,100000,100000,100000,100000,100000,100000,100000,100000,100000,100000,100000,100000,100000,100000,100000,100000,100000,100000,100000,100000,100000,100000,100000,100000,100000,100000,100000,100000,100000,100000,100000,100000,100000,100000,100000}

    Returns: 500000000

  6. 1

    {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50}

    Returns: 99

  7. 87

    {220403}

    Returns: 220403

  8. 46

    {302003,193029,310424,230014,296521,36349}

    Returns: 35663708

  9. 99

    {172596,249834,134241,102651,324984,165366,290995,304384,346831,128544,357883}

    Returns: 1151459

  10. 25

    {214327,118860,114647,162670,375836,358973,387692,129373,187218,257232,329149,243510,42686,241249,67260,11176}

    Returns: 1832824

  11. 2

    {101857,15626,25871,301305,87883,234884,137543,11019,256631,372267,16186,43356,77269,336445,211599,8963,256263,149805,10087,359324,187915}

    Returns: 0

  12. 83

    {353250,214794,326733,295151,97008,330776,254365,382667,139733,249223,356578,247431,49033,33024,212620,381934,380599,332355,315937,93209,224800,43370,394082,114749,225073,265950}

    Returns: 110537076

  13. 57

    {362586,94703,40427,225404,300267,36144,125255,117292,364450,19556,103737,61095,340785,20161,385954,332562,306767,323140,55296,65230,124163,128117,50739,191476,227442,56393,336216,244750,395031,360601,342445}

    Returns: 28958850

  14. 60

    {384611,34417,253351,142478,310189,339823,163477,300337,251067,136709,168356,383112,242163,92954,172937,362424,20485,280885,241597,28200,295333,260804,381611,89359,133283,217917,338113,44513,308682,259998,197244,42804,136879,259676,43397,244476}

    Returns: 0

  15. 20

    {262346,37496,230777,142715,188934,27667,392246,118645,251487,198766,258766,230842,87404,258579,180047,90237,158089,283512,237684,29534,368798,87779,228972,7673,339325,155258,391014,155415,310812,15757,243352,123846,53009,185386,355035,283041,138421,312082,179013,361801,209186}

    Returns: 31715640

  16. 16

    {361348,216521,150328,162362,307266,85764,271053,186644,339114,334725,359234,214045,382942,116821,28294,150802,287707,292862,42380,234650,265345,55467,52755,8222,362889,238573,191832,394562,116774,342564,82320,71661,104311,240353,66280,120911,101472,157624,112055,307320,221591,82305,228593,90116,66466,121046}

    Returns: 0

  17. 76

    {289995,296645,16549,170037,199748,146421,393191,377645,390346,99443,327229,228067,45770,368691,359183,290791,148527,304178,473,220939,218564,372647,110501,100476,23756,340306,310046,66569,225288,165488,223895,363513,309961,328310,64617,231263,126473,148823,5740,121232,31500,215563,210574,270546,263917,6024,54209,258939}

    Returns: 163029728

  18. 83

    {89789,160170,22640,222789,66977,376436,168501,259699,314098,194365,234014,80084,377858,304702,287801,100746,236418,139995,17780,350306,109577,55671,365423,229828,127229,148484,197880,151784,268731,358512,184013,256555,361640,7558,128877,315650,367826,53280,211214,266821,72344,239706,393721,168009,162414,391006,166762}

    Returns: 1695545

  19. 99

    {51194,71952,291596,137739,122217,55278,301272,262775,301819,131307,363295,34050,74791,94680,129177,313113,258829,363739,89977,387553,83728,350935,43132,397680,5340,309074,23772,182970,290184,310562,15477,73835,84641,11286,131607,226701,316196,325743,203286,28584,68438,3333,133057,43505,391249,259561,272652,302449,43983,285735}

    Returns: 363009834

  20. 3

    {203694,237555,350812,268739,350613,185294,244945,276160,290601,240514,145720,371589,107381,64945,171685,29601,310647,89540,271867,226808,278175,286437,160996,232651,92914,80067,280062,56282,158681,313308,71667,70727,75387,184438,67090,321549,36138,199420,329326,260353,118361,243630,257409,389190,381254,50804,260308,87243,342569}

    Returns: 378548

  21. 83

    {89578,131331,203310,371451,74264,320576,268033,372182,161820,222173,186143,261677,224810,394868,51834,87594,38893,361720,300954,16483,35982,331313,255762,381786,15859,19861,30395,381218,375791,98890,74254,86032,13646,55774,205169,348882,213987,134088,369836,190613,132961,245158,343462,324142,385192,345595,282837,48653}

    Returns: 81704536

  22. 84

    {112932,231510,254587,189994,377544,385053,394397,264637,107464,17614,347643,20788,364716,304004,146108,383664,9255,39849,130294,295753,86061,173327,366625,222474,100302,120652,19883,147538,51173,379006,40103,294634,349389,67084,309242,188533,41722,174913,385449,49222,111568,227764,169829,120911,260099,296013,109639}

    Returns: 0

  23. 70

    {105452,332908,46755,241217,369062,54550,264275,222599,149189,309725,26377,11312,210401,210412,215125,362867,108006,87482,217734,379778,19733,193289,253565,240627,191592,285740,324884,278685,137828,372749,348342,24048,162979,49898,53633,171811,233,296700,394839,380195,226882,29979,316820,283288,12630,296261,248171,68432,13841,223490}

    Returns: 0

  24. 80

    {386475,203938,344759,337315,349342,219479,268498,121132,106902,79296,75866,87618,218515,53726,182108,268101,121578,82645,27936,85588,250403,173905,389565,351231,305507,218252,113122,119063,391834,318650,233047,208256,118540,246610,38178,262708,76554,301475,358736,189000,214249,96772,351700,31796,244863,194095,343070,341502,290431}

    Returns: 0

  25. 25

    {276227,209958,170353,89631,247550,201350,236274,283863,69204,9204,386079,138798,270505,368349,351958,171273,92624,315593,100894,308336,269569,263676,317604,170471,260995,98048,354689,193655,231506,336010,392160,75991,262649,358174,240845,217693,226287,345482,195723,136702,189367,243650,207004,284258,253549,90208,142133,231283}

    Returns: 11091550

  26. 72

    {64418,265433,174902,247526,319123,290105,74307,35926,344622,182376,299575,20710,48405,9945,155280,331272,69527,383237,357340,344609,394320,267038,189755,326418,100942,25252,378009,379841,260193,379231,198463,331268,41586,6899,79443,207858,299938,257370,125927,274622,273571,153316,390176,151509,333470,221920,60655}

    Returns: 129665808

  27. 3

    {152359}

    Returns: 152359

  28. 1

    {88697,391616,206200,217329,279143,220301}

    Returns: 1225892

  29. 1

    {399040,11811,284182,36818,181434,168355,200843,168683,38875,347244,126895}

    Returns: 617832

  30. 2

    {244325,136835,115973,149479,363280,284489,29019,234519,116047,152124,339744,158327,287688,378563,128297,183970}

    Returns: 2099014

  31. 2

    {196142,395538,109368,87449,318162,22071,308438,146482,382310,73683,46253,65551,31299,67191,226515,109926,342759,259689,51889,260195,76502}

    Returns: 2561148

  32. 1

    {303595,148834,136628,226194,392838,290134,177028,136249,66540,216336,15409,9834,153999,336801,214858,17306,165322,365573,271564,375252,75286,156269,169159,3599,307741,93135}

    Returns: 686061

  33. 3

    {212082,152053,300340,64625,88815,326826,144435,383415,88377,78380,142856,284341,5979,214894,35821,176423,327141,358811,371574,311667,111686,198074,310868,211985,19045,204641,57444,81041,253775,171775,62433}

    Returns: 2126800

  34. 1

    {18322,321048,131307,262013,356125,111811,206196,213558,218100,340369,95362,1419,236114,136005,23932,239415,66976,189456,212783,191113,210525,43008,42696,295460,166636,372967,219003,205232,82179,147150,203174,375119,133439,178577,11465,343216}

    Returns: 1431182

  35. 3

    {247530,8704,68928,110171,244733,392066,218521,358963,94,125989,239105,290636,93863,288833,282148,165570,340937,245733,388875,144941,274094,158002,391052,39341,229385,386566,212904,200990,108950,148464,376846,340655,282328,340397,25728,246963,229664,388100,36531,29972,182591}

    Returns: 2421321

  36. 2

    {46129,318580,120514,88663,141280,305211,365129,285745,322970,343055,1555,244146,326365,167520,80896,392382,290030,375834,322634,318379,144301,297555,212401,335854,350078,250040,191026,22888,144918,89003,160598,214781,25049,181429,34626,94783,339627,223390,24814,17721,327911,5570,123912,93388,326737,313839}

    Returns: 5186460

  37. 98

    {358008,11711,147060,268160,322012,192778,237778,122912,332164,26587,347941,325506,13923,287482,314651,37795,278046,54461,262998,126430,272137,314280,95913,107136,387780,52818,182192,320954,314879,190832,309803,1696,244907,282978,97338,70258,147536,97611,39549,95437,220355,174080,83744,204098,257202,193010,54517,284543,33684}

    Returns: 0

  38. 97

    {367599,231179,114785,288848,354397,399770,66027,385416,173589,180874,192451,214775,151756,310208,241114,16088,68548,169849,224591,53626,285867,52630,331301,286430,202790,264163,389478,52244,149137,42281,372027,20192,71471,80946,186402,354166,77450,213689,324124,362495,44156,110653,105781,369450,113487,35014,348155}

    Returns: 1834985

  39. 99

    {134075,352800,297751,387349,79262,294560,15974,201613,157111,361095,15060,271305,385061,253026,301740,25399,382164,266607,134900,154302,834,284818,52876,389542,58870,113189,372115,178437,79483,14572,387466,290441,49920,382237,40029,307538,113897,25264,147241,18013,126861,42390,156082,152294,32973,117561,227318}

    Returns: 1354763

  40. 98

    {266434,66361,255168,312818,289934,164479,329728,100491,315995,52905,395661,386320,214486,193567,12360,248199,339559,264273,46014,227056,5361,16814,251218,384422,344943,234345,143913,286003,225585,162501,365088,282999,51824,99765,263580,91624,360273,219242,348034,178270,384164,87327,72103,230184,187094,94605,335257,238910}

    Returns: 180966016

  41. 100

    {326109,29451,171400,312523,321054,45873,254021,267025,52979,172882,289088,258038,137162,212270,369014,308323,150051,384766,105029,378579,166443,13148,212586,124147,81550,212533,388716,97421,120914,118579,95048,45870,46020,94684,25709,18582,220822,37543,133218,249089,76160,329742,68137,277390,42409,29200,323424}

    Returns: 0

  42. 100

    {287128,253931,141887,294275,96979,150081,284258,228009,233355,75787,79297,385526,352421,260161,265361,116397,213337,77290,233602,390269,41435,121792,234814,71456,187368,138879,119391,394835,312244,129180,309021,168709,74687,192867,257528,82484,397020,188462,343095,12079,366582,225848,99226,259035,43400,203099,179198,298806,298667}

    Returns: 0

  43. 99

    {214811,317290,71644,308189,121105,348383,21421,210028,381012,82051,43531,4000,111305,229707,26153,149496,189637,216848,160384,326893,4338,355840,290311,324972,13684,154225,200210,258528,138023,6116,7473,122566,383239,102088,262112,312009,72804,97393,89790,71847,90206,262296,193782,153179,211467,233390,76848}

    Returns: 235735830

  44. 97

    {350635,138237,70766,199559,133859,255167,57686,21652,395705,48832,127734,223826,274638,167792,358577,48296,358762,58765,330057,114126,183208,239417,160029,216714,370364,54202,179515,181696,74833,342813,121660,97374,82769,399315,284908,341552,50521,200567,351180,70295,33976,192517,81409,67323,15096,350813,337442,43081}

    Returns: 424545720

  45. 97

    {25318,172873,66594,276668,336204,144517,295923,293657,52785,60321,22603,365630,216547,170807,74641,210616,89229,340168,372499,151226,389913,43549,170252,237931,132539,206693,368923,82046,160043,148136,323627,340509,321228,385432,70858,338360,334286,114487,362069,170492,125700,95549,205776,216682,268856,250435,281063,196257,240532,230088}

    Returns: 1102245

  46. 98

    {119510,281225,166684,43668,319783,383872,193977,382520,28062,228055,85259,129313,143490,164026,101971,230789,83890,212429,4988,203070,317604,91158,172640,32501,319407,223806,60131,31043,347996,72691,140826,341554,137062,234099,166540,344470,134628,56831,115463,141970,124325,218699,9440,372067,2036,6995,120856,92224}

    Returns: 3660664

  47. 100

    {399999,400000,400000,399998,399998,399999,399999,399999,399998,399997,399997,399999,399998,399999,399998,399999,399999,400000,399997,399997,399999,399998,400000,399999,399999,400000,400000,399999,399997,400000,399997,399998,399999,400000,399997,399997,399997,400000,399998,399997,399998,399999,399997,400000,399998,399997,399997,399997}

    Returns: 600

  48. 98

    {399997,400000,399999,399998,400000,399999,399999,399998,399997,399997,399998,399998,399997,400000,399997,399998,400000,399998,400000,399999,400000,399997,399997,399998,399998,399997,399997,399997,399998,400000,400000,400000,399998,399997,399998,399998,399999,399999,400000,399997,399997,400000,399998,399999,399998,400000,400000,399997,399999}

    Returns: 0

  49. 99

    {400000,399999,400000,399999,400000,399998,399998,399999,399999,399998,399998,399999,399998,399998,399998,400000,399999,400000,399998,399997,399998,399998,399999,399997,399997,399999,399997,399999,399997,399997,399997,400000,399997,399998,399999,399997,399999,400000,399998,399997,399997,399997,400000,400000,399998,399997,399999,399998,400000,399997}

    Returns: 2400006

  50. 97

    {399998,400000,399999,399999,399999,399998,399997,399998,399999,399999,399997,400000,399999,399997,400000,400000,399997,399998,400000,399999,399998,399997,399997,399997,399999,399999,399999,400000,399997,399998,399999,399998,399999,399998,399999,399999,399999,399998,399999,399999,399998,399997,399999,399999,399999,399997,399999,399997}

    Returns: 3999998

  51. 98

    {399999,400000,399998,400000,399998,399999,399999,399998,400000,399997,399997,399998,399999,399998,400000,400000,400000,400000,400000,400000,399998,400000,399997,399998,399997,399998,400000,399998,399998,399997,399999,399999,399999,399999,399998,399999,399998,399998,399997,400000,399999,399998,399998,400000,399999,399997,400000,399999,399997,399997}

    Returns: 1600012

  52. 58

    {570,2013,2,13,1414,1732}

    Returns: 65888

  53. 5

    {1, 1, 2 }

    Returns: 10

  54. 100

    {400000, 400000, 400000, 400000, 400000, 400000, 400000, 400000, 400000, 400000, 400000, 400000, 400000, 400000, 400000, 400000, 400000, 400000, 400000, 400000, 400000, 400000, 400000, 400000, 400000, 400000, 400000, 400000, 400000, 400000, 400000, 400000, 400000, 400000, 400000, 400000, 400000, 400000, 400000, 400000, 400000, 400000, 400000, 400000, 400000, 400000, 400000, 400000, 400000, 400000 }

    Returns: 2000000000

  55. 100

    {400000 }

    Returns: 40000000

  56. 100

    {400000, 400000, 400000, 400000, 400000, 400000, 400000, 400000, 400000, 400000, 400000, 400000, 400000, 400000, 400000, 400000, 400000, 400000, 400000, 400000, 400000, 400000, 400000, 400000, 400000, 400000, 400000, 400000, 400000, 400000, 400000, 400000, 400000, 400000, 400000, 400000, 400000, 400000, 400000 }

    Returns: 1560000000

  57. 2

    {2 }

    Returns: 0

  58. 5

    {1, 2, 2 }

    Returns: 1

  59. 50

    {1, 1, 2 }

    Returns: 100

  60. 1

    {3, 2, 3 }

    Returns: 4

  61. 100

    {399999, 399999, 399999, 399999, 399999, 399999, 399999, 399999, 399999, 399999, 399999, 399999, 399999, 399999, 399999, 399999, 399999, 399999, 399999, 399999, 399999, 399999, 399999, 399999, 399999, 399999, 399999, 399999, 399999, 399999, 399999, 399999, 399999, 399999, 399999, 399999, 399999, 399999, 399999, 399999, 399999, 399999, 399999, 399999, 399999, 399999, 399999, 399999, 399999, 399999 }

    Returns: 0

  62. 11

    {2, 3, 5, 7, 11, 13 }

    Returns: 21

  63. 100

    {1, 1, 2, 45, 23, 23, 23, 4, 65, 76, 45, 34, 23, 45, 45, 65, 34, 23 }

    Returns: 0

  64. 1

    {1 }

    Returns: 1

  65. 1

    {6, 6 }

    Returns: 0

  66. 1

    {2, 2 }

    Returns: 0

  67. 100

    {1, 1 }

    Returns: 0

  68. 1

    {1, 2, 3 }

    Returns: 2

  69. 2

    {1, 2, 3 }

    Returns: 0

  70. 5

    {10, 6 }

    Returns: 20

  71. 100

    {1 }

    Returns: 0

  72. 100

    {399998, 399998, 399998, 399998, 399998, 399998, 399998, 399998, 399998, 399998, 399998, 399998, 399998, 399998, 399998, 399998, 399998, 399998, 399998, 399998, 399998, 399998, 399998, 399998, 399998, 399998, 399998, 399998, 399998, 399998, 399998, 399998, 399998, 399998, 399998, 399998, 399998, 399998, 399998, 399998, 399998, 399998, 399998, 399998, 399998, 399998, 399998, 399998, 399998, 399998 }

    Returns: 0

  73. 4

    {1 }

    Returns: 0


This problem statement is the exclusive and proprietary property of TopCoder, Inc. Any unauthorized use or reproduction of this information without the prior written consent of TopCoder, Inc. is strictly prohibited. (c)2024, TopCoder, Inc. All rights reserved.
This problem was used for: